0% found this document useful (0 votes)
635 views

Lab Assignment: EXPERIMENT 1: Study of Socket Programming and Client - Server Model

This document describes an experiment on socket programming and the client-server model. It discusses an echo client and server program written in Java using TCP sockets. The client program establishes a connection with the server, sends a message, and receives the echoed message back. The server program creates a listening socket, accepts client connections, receives messages from clients, echoes them back, and can handle multiple clients concurrently. Pseudocode algorithms are provided for the client and server programs. The full Java code for an echo client and server program is also listed.

Uploaded by

Akash Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
635 views

Lab Assignment: EXPERIMENT 1: Study of Socket Programming and Client - Server Model

This document describes an experiment on socket programming and the client-server model. It discusses an echo client and server program written in Java using TCP sockets. The client program establishes a connection with the server, sends a message, and receives the echoed message back. The server program creates a listening socket, accepts client connections, receives messages from clients, echoes them back, and can handle multiple clients concurrently. Pseudocode algorithms are provided for the client and server programs. The full Java code for an echo client and server program is also listed.

Uploaded by

Akash Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB ASSIGNMENT

EXPERIMENT 1: Study of Socket Programming and Client – Server model

EXPERIMENT 2: Write a java program for application using TCP Sockets Links:

(1) Echo client and echo server


 In the TCP Echo client a socket is created. Using the socket a connection is made to the server using
the connect() function. After a connection is established, we send messages input from the user and
display the data received from the server using send() and read() functions.
 In the TCP Echo server, we create a socket and bind to a advertized port number. After binding the
process listens for incoming connections. Then an infinite loop is started to process the client requests
for connections. After a connection is requested, it accepts the connection from the client machine
and forks a new process.
 The new process receives data from the lient using recv() function and echoes the same data using the
send() function. Please note that this server is capable of handling multiple clients as it forks a new
process for every client trying to connect to the server. TCP socket routines enable reliable IP
communication using the transmission control protocol (TCP).
 The implementation of the Transmission Control Protocol (TCP) in the Network Component. TCP
runs on top of the Internet Protocol (IP). TCP is a connectionoriented and reliable, full duplex
protocol supporting a pair of byte streams, one for each direction.
 A TCP connection must be established before exchanging data. TCP retransmits data that do not
reach the final destination due to errors or data corruption. Data is delivered in the sequence of its
transmission

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

import java.net.*; // for network connection

import java.io.*; // for input and output stream

public class EServer {

public static void main(String args[]) {

ServerSocket s=null;

String line;

DataInputStream is;

PrintStream ps;

Socket c=null;

try {

//starts server and wait for the connection

s=new ServerSocket(9000);

catch(IOException e)

{ System.out.println(e); }

try { c=s.accept();

// takes input from the client socket

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

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

while(true) { line=is.readLine(); ps.println(line); }


}

catch(IOException e) { System.out.println(e); } } }

(B) EClient.java

import java.net.*;

import java.io.*;

public class EClient {

public static void main(String arg[]) {

// initializing socket and input output streams

Socket c=null;

String line;

DataInputStream is,is1;

PrintStream os;

try {

//establishing the connection

InetAddress ia = InetAddress.getLocalHost();

c=new Socket(ia,9000);

catch(IOException e)

{ System.out.println(e); }

try {

//send output to the socket

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

//takes input from the terminal

is=new DataInputStream(System.in);
is1=new DataInputStream(c.getInputStream());

while(true)

{ System.out.println("Client:");

//strings to read the message from input tab

line=is.readLine();

os.println(line);

System.out.println("Server:" + is1.readLine()); }}

//closing the connection

catch(IOException e) { System.out.println("Socket Closed!"); } }}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy