Reasearching Socket Programming With Tcp Information Technology Essay

Published: November 30, 2015 Words: 2209

The client has the job of initiating contact with the server. In order for server to be able to react to the clients initial contact, the server has to be ready. First, the server program cannot be dormant - that is, it must be running as a process before the client attempts to initiate contact. Second, the server program must have some sort of door- more precisely, a socket- that welcomes some initial contact from a client process running on an arbitrary host. With the server process running, the client process can initiate a TCP connection to the server. This is done in the client program by creating a socket. When the client creates its socket, it specifies the address of the server process, namely, the IP address of the server host and the port number of the server process. The three way handshake, which takes place at the transport layer, is completely transparent to the client and server program. In our example below, the welcoming door is a ServerSocket object that calls the server. When a client knocks on this door, the program invokes server's accept() method, which creates anew door for the client, dedicated socket as the server's connection socket. TCP thus provides a reliable byte-stream service between the client and server processes. A stream is a sequence of characters that flow into or out of a process. Each stream is either an input stream for the process or an output stream for the process.

I use the following simple client-server application to demonstrate socket programming for both TCP. The client program is called bclient1.java and the server program is called Server.java.

3.1.0 SocketClient: bclient1.java

3.1.1 SocketClient: A Simple TCP/IP Socket Client

import java.net.*; import java.io.*; import java.util.*; public class bclient1 { (Appendix, line 21-37) are Java package contains classes for input and output stream. The java.io package contains the BufferedReader and Output Stream classes, classes that the program use to create the three streams previously illustrated. The java.net package provides classes for network support. In particular, it contains the Socket and ServerSocket classes.

3.1.2 Some House Keeping

private static InetSocketAddress readHostAndPort()

{

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

try {

System.out.print("Give host: "); …………. (Appendix, bclient1.java, line 39-71) Create the stream object in of type BufferedReader. The input stream is initialized with System.in, which attaches the stream to the standard input. Creates a socket address where the IP address is the wildcard address and the port number a specified value. A valid port value is between 0 and 65535. A port number of zero will let the system pick up an ephemeral port in a bind operation.

3.1.3 Declaring Variables

public static void main(String[] args)

{

byte [] buff = new byte[100];

int len = 0; …………… (Appendix, bclient1.java, line73-85) Variables are for representing data of a certain type. Variable declaration tells the compiler to allocate appropriate memory space for the variable based on data type. This constructor is requires only two arguments: a byte of array that contains client-specific data and the length of the byte array.

3.1.4 Requesting a Socket and Establishing a Connection

InetSocketAddress addr = readHostAndPort();

if ( addr.isUnresolved() ) {

……..………. (Appendix, bclient1.java, line 88-102) To establish a connection with a server, first to obtain the server's 32-bit IP address. To obtain the IP address by invoking the InetSocketAddress, getHostName() method and pass this method the name of the host to connect to. It returns an InetSocketAddress object address containing the host name/IP address pair (i.e. Using localhost in the getHostName() method will return localhost/127.0.0.1 in the InetAddress object).Then, ready to establish a socket connection with our server. To create a Socket called connection by instantiating a new Socket object with the InetSocketAddress object address and our previously created int port. If the server is not responding on the port we're looking for, to get a "java.netConnectException: Connection refused:." message.

3.1.5 All changes to read from the socket, send and process messages

while (choice!=5) {

outbuff = null;

System.out.println("Query the Book From the Database : 1"); ………………. (Appendix, bclient1.java, line 106-205) There 5 select the menu choice to choose, 1. "Query the Book From the Database : 1", "Please enter book title or book category:" After enter book category. A process will sends message and receives message from each select choice. The network through a software interface called a socket. Next select choice 2. "Sign in to account: 2", "Please enter username:" and "Please enter password:", 3. "Register a new user: 3", "Please enter username:" and "Please enter password:", 4."Purchasing a book: 4" and 5. "Sign out: 5".

3.2.0 SocketServer: Server.java

3.2.1 SocketServer: The run() Method

public void run () {

byte [] buff = new byte [500];

int len = 0;

String message = null; ………………… (Appendix, Server.java, line 26-170) The run() method's responsibility is to run the block of code that we want threaded. In our example the run() method executes the same block of code we built in the Server class. Once again, to read the BufferedInputStream, pretend to process the information, and write a BufferedOutputStream. At the end of the run() method, the thread dies.

3.2.2 Gets the port number

private static int getPort () {

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

try {

int p = 0; ………………… (Appendix, Server.java, line 172-200) Gets the port number, and return the port number.

3.2.3 Returns the port number

int port = getPort ();

try {

ServerSocket server = new ServerSocket (port, 5); This line creates the object server, which is of type ServerSocket. The ServerSocket is a sort of door that listens for a knock from some client, and listens on port number 5.

System.out.println ("ServerSocket created");

System.out.println ("Ready to acccept");

Socket socket = server.accept (); This line creates a new socket, called socket, when client knocks on server.This socket also has port number 5. TCP then establishes a direct virtual pipe between addr at the client and socket all the server. The client and server can then send bytes to each other over the pipe and all bytes sent arrive at the other side in order. With socket established, the server can continue to listen for request from other clients for the application using socket.

………………………… (Appendix, Server.java, line 260-275)

Figure 1.1: Processes communicating

through TCP sockets.

Figure 1.2: The client-server application,

using connection-oriented transport

services.

Figure 1.3: TCP client has three

streams through which characters flow.

Part B: Report of Protocol Design (i)

A network protocol is similar a formal set of rules, conventions and data structure of the governs how the computers and other network devices to exchange an information over the network. The definition of protocol is a standard of procedure and the format that two data communication to device. It must understand, accept and use to talk to each other. In the modern protocol design, the network protocols have "layered" according to the OSI 7 layer model or similar of layered models. Layering is a design principle which are to divide the protocol design into a number of smaller parts, each of accomplishes a particular sub-task, and interacts with the other parts of the protocol only in a small number of well-defined ways. Layering allows the parts of a protocol to design and test without a combinatorial explosion of cases, to keep each of design relatively simple. Layering is permitting familiar protocols to be adapted to unusual circumstances. The header and trailer at each layer are reflecting to the structure of a protocol. The network protocol to be detailed rules and procedures are defined by a lengthy document. For example, IETF uses RFCs (Request For Comments) to define protocols and updates to the protocols. In summary, network protocol defines the format and the order of messages exchanged between two or more communicating entities, as well as the actions taken on the transmission and/ or receipt of message or other event.

These parts are illustrated how my NuRead E-Book System protocol in my computer will send a connection request message to server and client side and to wait for a reply.

To Preview a Sample from an e-book prior to Purchase

The computer network protocol must probably familiar, consider what happens when to make a request NuRead E-Book System to server that is, when you start request To Preview a Sample from an e-book prior to Purchase. First, your computer will send a connection request message to the server and wait for a reply. The server will eventually receive your connection request message and return a connection reply message. Knowing that is now OK to request Select the Correct Computer or Device to Send the Sample, and Click send Sample Now. Knowing that is now OK to request Click Go to Kindle PC button to Open Kindle and Read your ebook Preview. Knowing that is now OK to reply the Book Category. After that, you make a request Kindle will Synchoronize and Download the Preview you Selected. Knowing that is now OK to reply the Book Title. Then you make the last request Double click View Detail your Sample to Start Reading to fetch from that the server in a GET message. Finally, the server returns the Beginning of First Chapter, can also View Index, Cover, Table Content to my computer.

In Order to provide a Student Discount on Certain Selected Categories of Book

The computer network protocol must probably familiar, consider what happens when to make a request NuRead E-Book System to server that is, when you start request In Order to provide a Student Discount on Certain Selected Categories of Book. First, your computer will send a connection request message to the server and wait for a reply. The server will eventually receive your connection request message and return a connection reply message. Knowing that is now OK to request Review & Price. Then my computer to reply Review & Price of the server it wants to fetch from that the server in a GET message. Finally, the server returns the Student ref.no, Book Category, Book Title, Sales Price to my computer.

Invalid to Preview a Sample from an e-book

When you start request To Preview a Sample from an e-book prior to Purchase. First, your computer will send a connection request message to the server and wait for a reply. If the server can't receive your connection request message. The server in my computer will reply Invalid to Preview a Sample from an e-book. Finally, the server returns the Error, to preview a sample from an e-book prior to purchase to my computer.

Invalid to Get a Student Discount on certain Categories of book

When you start request In Order to provide a Student Discount on Certain Selected Categories of Book. First, your computer will send a connection request message to the server and wait for a reply. If the server can't receive your connection request message. The server in my computer will reply Invalid to Get a Student Discount on certain Categories of book. Finally, the server returns the Error, to get a student discount on certain selected categories of book to my computer.

Part B: Protocol Design (ii)

NuRead E-Book Protocol Design

Request

Client Server

Registering a New User Request: R-------------->

<---------- OK ---------- ACK: +

N, Customer Name ---------------------------------->

<---------- OK ----------- ACK: +

A, Customer address -------------------------------->

<------ OK, n, Customer ref.no. ----- ACK: +

Sign In to NuRead Request: S, Customer ref.no. -------------->

<---------- OK ----------- ACK: +

Book Details Request: B, --------------------------->

<--------------- Book Details Reply: b, Title (30), Author (30), Category (10)

To Preview a Sample from an e-book prior to Purchase Request: C, ---------->

<---------------------------------- OK -------------------------------------- ACK: +

Select the Correct Computer or Device to Send the Sample, and Click send

Sample Now Request : D, ------------------------------------------->

<---------------------------------- OK -------------------------------------- ACK: +

Click Go to Kindle PC button to Open Kindle and Read your ebook

Preview Request: E, --------------------------------------------->

<------ OK, e, Category (10)---------------------------------------- ACK: +

Kindle will Synchoronize and Download the Preview you Selected

Request: F, ----------------------------------------->

<------ OK, f, Title (30)---------------------------------------- ACK: +

Double click View Detail your Sample to Start Reading Request: F, ------------->

<----------------Double click View Detail your Sample to Start Reading Reply: f,

Beginning of First Chapter, can also View Index, Cover, Table Content

In Order to provide a Student Discount on Certain Selected Categories

of Book Request: G, -------------------------------------------------->

<----------------------------------- OK --------------------------------- ACK: +

Review & Price Request: H, --------------------------------------------->

<---------------- Review & Price Reply: h, Student ref.no, Category (10),

Title (30), Sales price (8)

Book Purchasing Request: P, ---------------------->

<---------------- Book Purchasing Reply: p,

Customer ref.no. (4), Book ref.no (4), Sales price (8)

Sign Out to NuRead: O ----------------------------->

<---------- OK ----------- ACK: +

ERR- NuRead E-Book Protocol Design

Client Server

To Preview a Sample from an e-book prior to Purchase Request: C, ---------->

<------------, Invalid to Preview a Sample from an e-book

Error, to preview a sample from an e-book prior to purchase.

Client Server

In Order to provide a Student Discount on Certain Selected Categories

of Book Request: G, -------------------------------------------------->

<------------, Invalid to Get a Student Discount on certain Categories of book

Error, to get a student discount on certain selected categories of book.

Part C: Do three of the following

Question b