Client :
package client_server;
import java.net.*;
import java.io.*;
public class Client
{
static void show(String s)
{
System.out.println(s);
}
public static void main(String a[])
{
int port=1500;
String server = "localhost";
Socket s1=null;
BufferedReader input;
PrintWriter p1;
String msg = null;
try
{
s1=new Socket(server,port);
show("connected with server");
p1= new PrintWriter(s1.getOutputStream(),true);
p1.println(5);
p1.println(10);
input= new BufferedReader(new InputStreamReader(s1.getInputStream()));
msg=input.readLine();
show("from server");
show(msg);
s1.close();
}
catch(Exception e)
{
System.out.println("Exception :"+e);
}
}
}
Server :
import java.net.*;
import java.io.*;
import java.util.*;
public class Server
{
static void show(String s)
{
System.out.println(s);
}
public static void main(String a[])
{
int port=1500;
ServerSocket s2;
Socket s=null;
PrintWriter p1;
String msg = null;
int b,c,d;
BufferedReader input;
try
{
s2=new ServerSocket(port);
show("server is waiting");
s=s2.accept();
p1= new PrintWriter(s.getOutputStream(),true);
show("new connection accepted");
input= new BufferedReader(new InputStreamReader(s.getInputStream()));
c=Integer.parseInt(input.readLine());
d=Integer.parseInt(input.readLine());
b=c+d;
p1.println(b + " ");
s.close();
}
catch(Exception e)
{
System.out.println("Exception :"+e);
}
}
}