Print IP Server
Print IP Server
Aim : Design a socket program to print the client address at the server side
Algorithm
Server:
Client:
Program:
import java.io.*;
import java.net.*;
class Sip
{
public static void main(String args[])
{
ServerSocket ss;
Socket s;
DataInputStream dis;
String ip;
try
{
ss=new ServerSocket(8020);
while(true)
{
s=ss.accept();
dis=new DataInputStream(s.getInputStream());
ip=dis.readLine();
System.out.println("Ip address of the client system is"+ip);
}
}
catch(IOException e)
{
System.out.println("The exception is: "+e);
}
}
}
import java.io.*;
import java.net.*;
class Cip
{
public static void main(String args[])
{
Socket soc;
PrintStream ps;
try
{
InetAddress ia=InetAddress.getLocalHost();
soc=new Socket(ia,8020);
ps=new PrintStream(soc.getOutputStream());
ps.println(ia);
}
catch(IOException e)
{
System.out.println("The exception is: "+e);
}
}
}
Output