Friday, 13 March 2015

Socket Program in Java[Client Server Program]

Description:Client program accepts a number from the user and passes it to the server, the server performs as simple mathematical operation like addition of the number with digit 5.


Video Tutorial:



Source Code://CLASS CLI


import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;


public class cli {

public static void main(String args[]) throws UnknownHostException, IOException
{
int number,temp;
Socket s=new Socket("127.0.0.1",1001);
Scanner sc=new Scanner(System.in);
Scanner sc1=new Scanner(s.getInputStream());
System.out.println("Enter any number");
number=sc.nextInt();
PrintStream p=new PrintStream(s.getOutputStream());
p.println(number);
temp=sc1.nextInt();
System.out.println(temp);
}
}
//Class Ser

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class ser {

public static void main(String args[]) throws IOException
{
int n1,n2;
ServerSocket s1=new ServerSocket(1001);
Socket ss=s1.accept();
Scanner sc=new Scanner(ss.getInputStream());
n1=sc.nextInt();
PrintStream p=new PrintStream(ss.getOutputStream());
n2=n1+5;
p.println(n2);
}

}

No comments:

Post a Comment