Java Source code to investigate client server communication

Aim:

To   write   a program to implement Client-Server Applications for chat.

Algorithm:

Step 1: Start the program.

Step 2: To implement the class and class name.

Step 3: To implement the main class and initiate the variables.

Step 4: Statements are used in inside the try block.

Step 5: Creating server program in runtime values are getting the handling exceptions

Step 6: Creating client program in runtime values are getting the handling exceptions

Step 7: Print the client and server process.

Source Code:
tcpserver1.java

import java.net.*;

import java.io.*;

public class tcpserver1

{

public static void main(String arg[])

{

ServerSocket s= null;

String line;

DataInputStream is = null,is1=null;

PrintStream os=null;

Socket c=null;

try

{

s=new ServerSocket(9999);

}

catch(IOException e)

{

System.out.println(e);

}

try

{

c=s.accept();

is=new DataInputStream(c.getInputStream());

is1=new DataInputStream(System.in);

os=new PrintStream(c.getOutputStream());

do

{

line=is.readLine();

System.out.println(“client : “+line);

System.out.println(“server : “);

line=is1.readLine();

os.println(line);

}

while(line.equalsIgnoreCase(“quit”)==false);

is.close();

os.close();

}

catch(IOException e)

{

System.out.println(e);

}

}}

tcpclient1.java

import java.net.*;

import java.io.*;

public class tcpclient1

{

public static void main(String arg[])

{

Socket c= null;

String line;

DataInputStream is ,is1;

PrintStream os;

try

{

c=new Socket(“LocalHost”,9999);

}

catch(IOException e)

{

System.out.println(e);

}

try

{

os=new PrintStream(c.getOutputStream());

is=new DataInputStream(System.in);

is1=new DataInputStream(c.getInputStream());

do

{

System.out.println(“client :”);

line=is.readLine();

os.println(line);

System.out.println(“server: “+is1.readLine());

}

while(line.equalsIgnoreCase(“quit”)==false);

is1.close();

os.close();

}

catch(IOException e)

{

System.out.println(“socket closed message passing is over”);

}

}

}

 

 

 

Result:

Thus the program has been successfully complied and Executed.