COMPUTER NETWORKS AND INTERNETS

25
COMPUTER NETWORKS AND INTERNETS PROJECT #1: ECHO/SERVER CLIENT Date :- 7/7/2016

Transcript of COMPUTER NETWORKS AND INTERNETS

COMPUTER NETWORKS AND INTERNETS

PROJECT #1: ECHO/SERVER CLIENT

Date :- 7/7/2016

Group Members:

(1) Jaywin Pumbhadia(305090544) GUIDED BY:-

(2) Hardik Patel (305094600) Dr. Yi Zhao

(3) Jaymin Patel (304394251)

DESIGN ENGINEER:Jaywin Pumbhadia

He has done all designing and manipulations in code design structure Various Implementations of threading and handler implementations was

successfully simulated on different client/server platform with great help from Jaymin Patel and Hardik Patel

TEST ENGINEER:

Hardik Patel

He has done all testing under different conditons as per requirement.Different client was simulated under the same host machine.

Multiple system was connected under one server and communication was established among them.

SIMULATION ENGINEER:

Jaymin Patel:

He has simulated all code in Java with different possibilities of threading to encapsulate multiple clients.

He has carried out all different coding implementing loop for client/server communications.

He has established hardware connection of different clients to single host server so that client and server can communicate easily.

INTRODUCTIONThe presented report is based on internet socket or network socket which is an endpoint of a bidirectional inter-process communication flow across an Internet Protocol based computer network, such as the Internet. In this project we have presented two programs which is for server and client. A client requests permission to communicate with the server. In this echo server client communication, the server will echo back any message given from clients. In this project we have also modified IP address and made it flexible. A server can be hosted by multiple clients through different connections.

ABSTRACT In this project we have developed two Java Programs. One is a simple client

that can send connection request to server. Server accepts the request and allows the client to send messages.

The server just echoes back the message received from the respective client, this has been achieved by multi threading.

The client asks for an server IP address to which the user wants to connect. After the request is accepted it allows the user to send the messages to server.

SOCKET The term socket is used as a name for an Application Programming

Interface. It provides the communication mechanism between two computers using TCP. A client program creates a socket on its end of the communication and attempts to connect that socket object on its end of the communication

In inter-process communication, each end will generally have its own socket, but these may use different APIs which are abstracted by network protocol.

SOCKET PROGRAMMING A socket represents a single connection between exactly two pieces of

software. More than two pieces of software can communicate in client/server or distributed systems by using multiple sockets. For e.g many web browsers can simultaneously communicate with a single Web server via a group of sockets made on the server.

In this programming we have declared server socket as a socket for listening and a client socket as normal socket for communication. We have defined input string and output string for sending and receiving messageswhile ((getString = in.readLine()) != null) {

System.out.println("Message from " + clientSocket.getInetAddress() + " : " +getString);

out.println(getString); Thus above mentioned line of codes is used for the echo sent by the server.

Whenever the user will provide any message from client to the server, server will echo back the same message received.

In this project we have used IP 127.0.0.1 as hard coded IP address for the server, because same server/client programming is running on the same system.It is local hosts’s IP to run both server/client on the same system.

The Class A network number 127 is assigned the “loopback” function,that is a datagram sent by a higher level protocol to a network 127 address should ever appear on any network anywhere.

127.0.0.1 is designated as local host in IPv4 communications.Thus we have used this IP as hard coded address.

Figure 2: Flow Diagram

Create Socket

Listen Client

Connection Bind Server Address and Port

Receive Send Data

Receive Send Data

Connection Close

Socket

Bind Port And Server Address

Send Data

Receive Data

Close

SOURCE CODESIMPLE ECHO SERVER

package com.jaywin;

/**

* EE544 Socket Programming

* Simple Java Echo Server

import java.io.*;

import java.net.*;

public class SimpleEchoServer {

public static void main(String args[]) throws IOException {

// Declaration

// Declare serverSocket as server socket for listening

// Declare clientSocket as normal socket for communication

// Declare in as input stream

// Declare out as output stream

ServerSocket serverSocket = null;

// Open ServerSocket

// Note: We can't choose a port less than 1023 if we are not

// privileged users (root).

try {

serverSocket = new ServerSocket(9088);

while (true) {

new Handler(serverSocket.accept()).start();

}

} catch (IOException e) {

System.err.println(e);

} finally {

serverSocket.close();

}

}

private static class Handler extends Thread {

Socket clientSocket = null;

BufferedReader in = null;

PrintWriter out = null;

public Handler(Socket socket) {

this.clientSocket = socket;

}

public void run() {

// Task 4: Please revise the following block so that the server can

// provide echo service to multiple clients without re-starting.

try {

// Open input and output streams

in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

out = new PrintWriter(clientSocket.getOutputStream(), true);

// Get the IP address of the client

System.out.println("Request received from " + clientSocket.getInetAddress());

// Task 1 : Please add appropriate codes below so that the

// server

// can echo back any received message to the client.

String getString;

while ((getString = in.readLine()) != null) {

System.out.println("Message from " + clientSocket.getInetAddress() + " : " +getString);

out.println(getString);

}

// Close streams and sockets

out.close();

in.close();

clientSocket.close();

} catch (IOException e) {

System.err.println(e);

}

}

}

}

Thus in above mentioned source code we have made multiple changes for echoing back the messages from clients to server.

We have changed the main function to add multi threading functionality and moved the client handling code to a new class named Handler.

We have made the threading function to distinguish and handle different clients simultaneously.

CLIENT PROGRAMpackage com.jaywin;

/**

* EE544 Socket Programming

* Simple Java Client

* By Dr. Yi Zhao

*/

import java.io.*;

import java.net.*;

public class SimpleEchoClient {

public static void main(String[] args) {

// Declaration

// Declare socket as client socket for communication

// Declare in as input stream

// Declare out as output stream

Socket socket = null;

BufferedReader in = null;

PrintWriter out = null;

// Open socket and streams

try {

// Task 3: Allow flexible server IP address that can be specified in

// the command line arguments.

System.out.println("Please enter the server IP.");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

socket = new Socket(br.readLine(), 9088);

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

out = new PrintWriter(socket.getOutputStream(), true);

} catch (UnknownHostException e) {

System.err.println("Don't know about host.");

} catch (IOException e) {

System.err.println("Couldn't get I/O for the connection.");

}

try {

// Task 2: Please revise the codes below so that the client can be

// interactive: read user's input from keyboard, send to echo

// server, and display received message from echo server.

// Send fixed message to server

System.out.println("Please enter message for Server. Enter 'exit' to close chat.");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String input = "";

while (!input.equals("exit")){

System.out.print(">");

input = br.readLine();

if (input != null) {

out.println(input);

String responseLine = in.readLine();

System.out.println("Echo: " + responseLine);

}

}

out.close();

in.close();

socket.close();

System.out.println("Connection Closed.");

// Close streams and socket

} catch (IOException e) {

System.err.println(e);

}

}

}

The above mentioned code is for client program. The client simply sends the request to server for connection and the communication starts therein.

We have done changes in coding so that user can use different IP address for communication.

As soon as the user will input the strings, it will take the input and will send the request for communication and echo back the message from server.catch (UnknownHostException e)

System.err.println("Don't know about host."); catch (IOException e) {

System.err.println("Couldn't get I/O for the connection.");

The above line of code is use to print the command or different input from the keyboard.Thus with the buffered Read command the input is read from the keyboard and is echoed back from the server to the client.

Thus with the help of threading we can join multiple clients to the server for the communication

Test Results

Thus in this above shown results we have used different IP address for the connection and program was executed successfully.

As shown in the figure message request is shown from the the different computer(IP Address)

Practical Applications This project can be used for small scale companies with networks

linking upto 5000 computers , this client/server project is the optimum solutions for messaging and file transfer,practically replacing corporate E-mail.

Fast replies Fast Messages