You are on page 1of 6

NP LAB

NAME: CHIMATA SRILATHA


ROLL NO:20181CSE0149
SECTION:6CSE03
Client:
import java.net.*;
import java.io.*;
import java.util.*;
public class MyClient4
{
public static void main(String args[])
{
try
{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new
DataOutputStream(s.getOutputStream());
Scanner sc=new Scanner(System.in);
System.out.println("enter the values of number1 and number2");
int num1=sc.nextInt();
int num2=sc.nextInt();
dout.writeInt(num1);
dout.writeInt(num2);
DataInputStream in=new DataInputStream(s.getInputStream());
int re=in.readInt();//here read will return something so we should
keep in some variable
System.out.println("received from client");
System.out.println("the result is "+re);
s.close();
}
catch(Exception e)
{
System.out.println("client unable to send message");
}
}
}

SERVER:
import java.net.*;
import java.io.*;
import java.util.*;
public class MyServer4
{
public static void main(String args[])
{
try
{
ServerSocket ss=new ServerSocket(6666);//socket port
Socket s=ss.accept();//it will accept and return the socket
DataInputStream dis=new
DataInputStream(s.getInputStream());//for receiving we use
datainputstream
int n1=dis.readInt();//here read function will return something so we
are storing that in the variable
int n2=dis.readInt();
int result=n1+n2;
System.out.println("result of the addition is "+result);
DataOutputStream out=new
DataOutputStream(s.getOutputStream());
out.writeInt(result);
System.out.println("sent the result to the client");
ss.close();
}
catch(Exception e)
{
System.out.println("message not received and server is busy");
}
}
}

You might also like