Aim:- Write down a program which
demonstrate the Socket programming for passing the message from server to
client.
JAVA Code :-
Client file :
import
java.lang.*;
import
java.io.*;
import
java.net.*;
public class
Client
{
public static void main(String[] args)
{
System.out.println("Sending
request....");
try
{
Socket s=new Socket("127.0.0.1",2510);
System.out.println("Connected Successfully.");
BufferedReader br=new BufferedReader(new
InputStreamReader(s.getInputStream()));
System.out.println("Response
from server...");
System.out.println("Client side: "+br.readLine());
s.close();
}
catch(UnknownHostException e)
{
System.out.println("Not find the IP-Address for: "+e);
}
catch(IOException e)
{
System.out.println("Not found data for
Socket: "+e);
}
}
}
Server file :
import
java.lang.*;
import
java.io.*;
import
java.net.*;
public class
Server
{
public static void main(String[] args)
{
try
{
ServerSocket ss=new ServerSocket(2510);
System.out.println("Wating for request.....");
Socket s=ss.accept();
System.out.println("Request accepted.");
PrintStream ps=new PrintStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Input the data at server: ");
ps.print(br.readLine());
s.close();
ss.close();
}
catch(Exception e)
{
System.out.println("Not found data for Socket: "+e);
}
}
}
Output:-
[Server]
[Client]
No comments:
Post a Comment