Program for Client And Server :
import java.io.*;
import java.net.*;
import java.util.*;
// This Class "NetTwo" is for Server
public class NetTwo{
public static void main(String arr[]){
try{
ServerSocket server=new ServerSocket(NetTwoConstants.PORT_NUMBER);
System.out.println(" Waiting for client to connect .... ");
Socket socket=server.accept();
System.out.println(" Client connected.... ");
new NetTwoSendThread(socket,"Server");
new NetTwoReceiveThread(socket,"Client");
}catch(Exception e){
System.out.println(e);
}
}
}
// This class for client.
class NetTwoClient{
public static void main(String arr[]){
try{
Socket socket=new Socket(NetTwoConstants.HOST_NAME,NetTwoConstants.PORT_NUMBER);
System.out.println(" Server Connected. \n");
new NetTwoSendThread(socket,"Client");
new NetTwoReceiveThread(socket,"Server");
}catch(Exception e){
System.out.println(e);
}
}
}
//****************
// This class NetTwoSendThread is thread class use for the Send Message from One End to other.
class NetTwoSendThread extends Thread{
private Socket socket;
private String name;
public NetTwoSendThread(Socket socket,String name){
this.socket=socket;
this.name=name;
start();
}
public void run(){
try{
Scanner in=new Scanner(System.in);
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
String sendMessage="";
do{
sendMessage=in.nextLine();
out.println(sendMessage);
}while(!sendMessage.equalsIgnoreCase("exit"));
socket.close();
System.out.println(" *************** Connection close by "+name+" **************** ");
System.exit(0);
}catch(Exception e){
System.out.println(e);
}
}
}
//************************************************
// This class is of Thread Manage receive message from other end.
class NetTwoReceiveThread extends Thread{
private Socket socket;
private String name;
public NetTwoReceiveThread(Socket socket,String name){
this.socket=socket;
this.name=name;
start();
}
public void run(){
try{
Scanner in=new Scanner(socket.getInputStream());
String receiveMessage="";
do{
receiveMessage=in.nextLine();
System.out.println(name+" : "+receiveMessage);
}while(!receiveMessage.equalsIgnoreCase("exit"));
socket.close();
System.out.println(" **************** connection close ******************** ");
System.exit(0);
}catch(Exception e){
System.out.println(e);
}
}
}
//***************
// This class hold static constants.
class NetTwoConstants{
public static int PORT_NUMBER=1412;
public static String HOST_NAME="localhost";
}
Server Compile by CMD : ----->>>>>
Client Compile by CMD : ----- >>>>>
Client Server Communications : -->
Message "Exit" Type by Server to end Communications :-->
This is total overall :- *****************************>>>>>>>>>>>>>>>>>>>
import java.io.*;
import java.net.*;
import java.util.*;
// This Class "NetTwo" is for Server
public class NetTwo{
public static void main(String arr[]){
try{
ServerSocket server=new ServerSocket(NetTwoConstants.PORT_NUMBER);
System.out.println(" Waiting for client to connect .... ");
Socket socket=server.accept();
System.out.println(" Client connected.... ");
new NetTwoSendThread(socket,"Server");
new NetTwoReceiveThread(socket,"Client");
}catch(Exception e){
System.out.println(e);
}
}
}
// This class for client.
class NetTwoClient{
public static void main(String arr[]){
try{
Socket socket=new Socket(NetTwoConstants.HOST_NAME,NetTwoConstants.PORT_NUMBER);
System.out.println(" Server Connected. \n");
new NetTwoSendThread(socket,"Client");
new NetTwoReceiveThread(socket,"Server");
}catch(Exception e){
System.out.println(e);
}
}
}
//****************
// This class NetTwoSendThread is thread class use for the Send Message from One End to other.
class NetTwoSendThread extends Thread{
private Socket socket;
private String name;
public NetTwoSendThread(Socket socket,String name){
this.socket=socket;
this.name=name;
start();
}
public void run(){
try{
Scanner in=new Scanner(System.in);
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
String sendMessage="";
do{
sendMessage=in.nextLine();
out.println(sendMessage);
}while(!sendMessage.equalsIgnoreCase("exit"));
socket.close();
System.out.println(" *************** Connection close by "+name+" **************** ");
System.exit(0);
}catch(Exception e){
System.out.println(e);
}
}
}
//************************************************
// This class is of Thread Manage receive message from other end.
class NetTwoReceiveThread extends Thread{
private Socket socket;
private String name;
public NetTwoReceiveThread(Socket socket,String name){
this.socket=socket;
this.name=name;
start();
}
public void run(){
try{
Scanner in=new Scanner(socket.getInputStream());
String receiveMessage="";
do{
receiveMessage=in.nextLine();
System.out.println(name+" : "+receiveMessage);
}while(!receiveMessage.equalsIgnoreCase("exit"));
socket.close();
System.out.println(" **************** connection close ******************** ");
System.exit(0);
}catch(Exception e){
System.out.println(e);
}
}
}
//***************
// This class hold static constants.
class NetTwoConstants{
public static int PORT_NUMBER=1412;
public static String HOST_NAME="localhost";
}
Server Compile by CMD : ----->>>>>
Client Compile by CMD : ----- >>>>>
Typing Hello From Server CMD for CLient and message Hello from server receive by client.
Client Server Communications : -->
Message "Exit" Type by Server to end Communications :-->
This is total overall :- *****************************>>>>>>>>>>>>>>>>>>>