18
TCP/IP Protocol Stack IP Device Drivers TCP UDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication –Release connection –Reliable (Acknowledged, in order) UDP: –No need to setup a link –Independent packets –Not reliable (no acknowledgement)

TCP/IP Protocol Stack

Embed Size (px)

DESCRIPTION

TCP/IP Protocol Stack. Application. Sockets. (Gate to network). TCP. UDP. IP. Device Drivers. Programming with Sockets. Sockets are Berkeley software distribution UNIX interface to network protocols Endpoint of communication, has a type and one associated process - PowerPoint PPT Presentation

Citation preview

Page 1: TCP/IP Protocol Stack

TCP/IP Protocol Stack

IP

Device Drivers

TCP UDP

Application

Sockets(Gate to network)

TCP:

–Establish connection

–Maintain connection during the communication

–Release connection

–Reliable (Acknowledged, in order)

UDP:

–No need to setup a link

–Independent packets

–Not reliable (no acknowledgement)

Page 2: TCP/IP Protocol Stack

Programming with Sockets

• Sockets are Berkeley software distribution UNIX interface to network protocols

• Endpoint of communication, has a type and one associated process

• Uses file system model (open-close-read-write)• Internet vs. Unix domain

Page 3: TCP/IP Protocol Stack

Client-Server Architecture

request

response

Process request

client

Host: www.vcu.edu

Port: 80

Page 4: TCP/IP Protocol Stack

4 steps in programming a client

• Open a socket.

• Open an input and output stream to the socket.

• Read from and write to the socket according to the server's protocol.

• Clean up.

Page 5: TCP/IP Protocol Stack

Socket in Java

• Two important classes– Socket: a normal socket as communication end– ServerSocket: accept the incoming request

• ServerSocket is an analogy to the custom service phone number, it will dispatch your call to a specialized staff--- normal Socket.

Page 6: TCP/IP Protocol Stack

Client Socket Creation

the machine you are trying to open a connection to

SOCKET DESCRIPTOR

Socket MyClient;MyClient = new Socket("Machine name", PortNumber);

port (a number) on which the server you want

port numbers between 0 and 1,023 are reserved for for standard services, such as email, FTP, and HTTP. for your server, select one that is greater than 1,023

Page 7: TCP/IP Protocol Stack

Create Server Socket

• If you are programming a server, then this is how you open a socket:

•ServerSocket MyService;try {   MyServerice = new ServerSocket(PortNumber);        }        catch (IOException e) {           System.out.println(e);        }

Page 8: TCP/IP Protocol Stack

Use ServerSocket: listen, accept, and Create connections from clients.

Socket clientSocket = null;try {   serviceSocket = MyService.accept();        }catch (IOException e) {   System.out.println(e);}

A blocking call, will return when a request arrive

Page 9: TCP/IP Protocol Stack

Send Data to Socket at Client

• PrintStream output;try {   output = new PrintStream(MyClient.getOutputStream());…

String inL=“hello”; output.writeBytes(inL+"\n");• }

Page 10: TCP/IP Protocol Stack

Read data from Socket at Client

• DataInputStream input;try {   input = new DataInputStream(MyClient.getInputStream());

…..

String responseLine = input.readLine();

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

Page 11: TCP/IP Protocol Stack

Send Data to Socket at Server

• PrintStream output;try {   output = new PrintStream(serviceSocket.getOutputStream());output.println(“this is server reply”);

• }

• Use the serviceSocket: returned from the accept() call.

Page 12: TCP/IP Protocol Stack

Read Data From Socket at Server

• Same as client case• DataInputStream input;

try {      input = new DataInputStream(serviceSocket.getInputStream());

• String req=input.readLine();• }

Page 13: TCP/IP Protocol Stack

Close Socket Connection

• On the client side: • try {

        output.close();        input.close();    MyClient.close();} catch (IOException e) {   System.out.println(e);}

• On the server side:

• try {   output.close();   input.close();   serviceSocket.close();    MyService.close();} catch (IOException e) {   System.out.println(e);}

Page 14: TCP/IP Protocol Stack

A Simple Echo Server• import java.io.*;

import java.net.*;public class echo3 {    public static void main(String args[]) {        ServerSocket echoServer = null;        String line;        DataInputStream is;        PrintStream os;        Socket clientSocket = null;        try {           echoServer = new ServerSocket(9999);        }        catch (IOException e) {           System.out.println(e);        }  

•// listen and accept connections.// Open input and output streams

try {           clientSocket = echoServer.accept();           is = new DataInputStream (clientSocket.getInputStream()); os = new PrintStream (clientSocket.getOutputStream());

// As long as we receive data, echo that data back to the client.          while (true) {             line = is.readLine();             os.println(line);            }        }   catch (IOException e) {           System.out.println(e);        }    }}

Page 15: TCP/IP Protocol Stack

A simple clientsmtpSocket = new

Socket("128.172.167.167", 9998); os = new

DataOutputStream(smtpSocket.getOutputStream());

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

while (true){

inL=d.readLine();os.writeBytes(inL+"\n");if (inL.compareTo("quit")==0) break;

// keep on reading from/to the socket till we receive the "Ok" from SMTP,

// once we received that then we want to break.

responseLine = is.readLine(); System.out.println("Server: " + responseLine); responseLine = is.readLine(); System.out.println("Server: " + responseLine); responseLine = is.readLine(); System.out.println("Server: " + responseLine);

}

Page 16: TCP/IP Protocol Stack

Reference

• Class ServerSocke– A server socket waits for requests to come in over the

network. It performs some operation based on that request, and then possibly returns a result to the requester.

– accept() Listens for a connection to be made to this socket and accepts it.

– getInetAddress() • Returns the local address of this server socket.

Page 17: TCP/IP Protocol Stack

Class Socket• This class implements client sockets (also called just "sockets"). A

socket is an endpoint for communication between two machines. • Socket(InetAddress address, int port) • connect(SocketAddress endpoint)

   Connects this socket to the server. • getPort()

   Returns the remote port to which this socket is connected. • getLocalPort()

          Returns the local port to which this socket is bound.• getInputStream()

          Returns an input stream for this socket.• getOutputStream()

          Returns an output stream for this socket. • getInetAddress()

          Returns the address to which the socket is connected. • getLocalAddress()

          Gets the local address to which the socket is bound.

Page 18: TCP/IP Protocol Stack

Other Parameters in Socket

• Buffer size– Incoming buffer size– Outgoing buffer size

• Time out or not– Otherwise a bad read will block your browser

for ever.

• Status check