Lab Assignment: EXPERIMENT 1: Study of Socket Programming and Client - Server Model
Lab Assignment: EXPERIMENT 1: Study of Socket Programming and Client - Server Model
Lab Assignment: EXPERIMENT 1: Study of Socket Programming and Client - Server Model
EXPERIMENT 2: Write a java program for application using TCP Sockets Links:
ALGORITHM:
(A) Client
1. Start
2. Create the TCP socket
3. Establish connection with the server
4. Get the message to be echoed from the user
5. Send the message to the server
6. Receive the message echoed by the server
7. Display the message received from the server
8. Terminate the connection
9. Stop
(B) Server
1. Start
2. Create TCP socket, make it a listening socket
3. Accept the connection request sent by the client for connection establishment
4. Receive the message sent by the client
5. Display the received message
6. Send the received message to the client from which it receives
7. Close the connection when client initiates termination and server becomes a listening server,
waiting for clients.
8. Stop.
Program:
(A) EchoServer.java
ServerSocket s=null;
String line;
DataInputStream is;
PrintStream ps;
Socket c=null;
try {
s=new ServerSocket(9000);
catch(IOException e)
{ System.out.println(e); }
try { c=s.accept();
is=new DataInputStream(c.getInputStream());
ps=new PrintStream(c.getOutputStream());
catch(IOException e) { System.out.println(e); } } }
(B) EClient.java
import java.net.*;
import java.io.*;
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try {
InetAddress ia = InetAddress.getLocalHost();
c=new Socket(ia,9000);
catch(IOException e)
{ System.out.println(e); }
try {
os=new PrintStream(c.getOutputStream());
is=new DataInputStream(System.in);
is1=new DataInputStream(c.getInputStream());
while(true)
{ System.out.println("Client:");
line=is.readLine();
os.println(line);
System.out.println("Server:" + is1.readLine()); }}