Skip to main content

Using TCP/IP sockets, write a client – server program to make the client send the file name and to make the server send back the contents of the requested file if present.


Client

import java.io.*;
import java.net.*;
import java.util.*;
public class Client
{
public static void main(String args[])
{
Socket socket=null;
try
{
socket=new Socket(InetAddress.getLocalHost().getHostName(),9912);
BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer=new PrintWriter(socket.getOutputStream(),true);
System.out.println("Enter the file name");
Scanner s=new Scanner(System.in);
String file=s.nextLine();
writer.println(file);
System.out.println("Reading Client......");
String h=null;
while((h=reader.readLine()) !=null)
System.out.println(h);
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}





















Server

import java.io.*;
import java.net.*;
public class Server
{
public static class ClientHandler extends Thread
{
private Socket socket;
ClientHandler(Socket socket)
{
System.out.println("client connected\n");
this.socket=socket;
}
public void run()
{
try{
BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer=new PrintWriter(socket.getOutputStream(),true);
String file=reader.readLine().trim();
System.out.println("requested file is"+file+"\n");
FileReader fp2=new FileReader(file);
BufferedReader rr=new BufferedReader(fp2);
String k=null;
while((k=rr.readLine())!=null)
{
writer.println(k);
}
writer.close();
rr.close();
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
public static void main(String args[])
{
final int port=9912;
try
{
ServerSocket ss=new ServerSocket(port);
System.out.println("listening.....");
while(true)
{
Socket socket=ss.accept();
new ClientHandler(socket).start();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}

Comments

Popular posts from this blog

Write a program for error detecting code using CRC-CCITT (16- bits).

import java.io.*; import java.util.*; public class CRC   {   public static void main(String args[]) { Scanner s=new Scanner(System.in); System.out.println("Enter the no of bits : "); int   n=s.nextInt(); int data[ ]=new int[n]; System.out.println("Enter the data bits : "); for(int i=0;i<n;i++) data[i]=s.nextInt(); System.out.println("Enter the no of divisor bits : "); int m=s.nextInt(); int divisor[ ]=new int[m]; System.out.println("Enter divisor bits : "); for(int j=0;j<m;j++) divisor[j]=s.nextInt(); int len=n+m-1; int div[ ]=new int[len]; int rem[ ]=new int[len]; int crc[ ]=new int[len]; int src[ ]=new int[len]; for(int i=0;i<data.length;i++) div[i]=data[i]; System.out.println("Dividend after appending zero"); for(int i=0;i<div.length;i++) System.out.print(div[i]); System.out.println(); for(int j=0;j<div.length;j++) rem[j]=div[j]; rem=divid...

Write a program on datagram socket for client/server to display the messages on client side, typed at the server side.

Sender import java.util.*; import java.net.*; public class Sender {   public static void main(String args []) throws Exception { System. out .println( "sender " ); DatagramSocket ds = new DatagramSocket(); Scanner s = new Scanner(System. in ); System. out .println( "enter the message" ); while ( true ) { String msg = s .nextLine(); InetAddress ip =InetAddress. getByName ( "127.0.0.1" ); DatagramPacket dp = new DatagramPacket( msg .getBytes(), msg .length(), ip ,3000); ds .send( dp ); } } } Receiver import java.util .*; import java.io.IOException ; import java.net.*; public class Receiver {   public static void main(String args []) throws Exception { byte [] buf = new byte [1024]; System. out .println( "receiver " ); DatagramSocket ds = new DatagramSocket(3000); while ( true ) { DatagramPacket dp = new DatagramPacket( buf ,1024); ds .receive( dp ); ...

Candidate Elimination Algorithm in Kannada | VTU Machine Learning Lab Pr...