Socket Programming Outline Socket programming with TCP Socket

  • Slides: 24
Download presentation
Socket Programming

Socket Programming

Outline: Socket programming with TCP. Socket programming with UDP. Evaluation.

Outline: Socket programming with TCP. Socket programming with UDP. Evaluation.

1. Socket Programming Goal: Learn how to build client/server application that communicate using sockets.

1. Socket Programming Goal: Learn how to build client/server application that communicate using sockets.

1. Socket Programming Socket: - A host-local, application-created, OS-controlled interface (a “door”) into which

1. Socket Programming Socket: - A host-local, application-created, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another application processes. - A door between application process and end-transport protocol (UCP or TCP)

1. Socket Programming Socket Types: 1. Datagram socket 2. Stream socket 3. Raw socket

1. Socket Programming Socket Types: 1. Datagram socket 2. Stream socket 3. Raw socket connectionless connection oriented Socket API: introduced in BSD 4. 1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm two types of transport service via socket API: unreliable datagram - reliable, byte stream-oriented UDP TCP

2. Socket-programming using TCP service: Reliable transfer of bytes from one process to another.

2. Socket-programming using TCP service: Reliable transfer of bytes from one process to another. controlled by application developer process socket TCP with buffers, variables host or server internet host or server controlled by application developer controlled by operating system

2. Socket-programming using TCP Client must contact server process must first be running server

2. Socket-programming using TCP Client must contact server process must first be running server must have created socket (door) that welcomes client’s contact Client contacts server by: creating client-local TCP socket specifying IP address, port number of server process When client creates socket: client TCP establishes connection to server TCP When contacted by client, server TCP creates new socket for server process to communicate with client • allows server to talk with multiple clients • source port numbers used to distinguish clients

2. Socket-programming using TCP Server (running on hostid) Client create socket, port=x, for incoming

2. Socket-programming using TCP Server (running on hostid) Client create socket, port=x, for incoming request: welcome. Socket = Server. Socket(x) TCP wait for incoming connection request connection. Socket = welcome. Socket. accept() read request from connection. Socket write reply to connection. Socket close connection. Socket setup create socket, connect to hostid, port=x client. Socket = new Socket(host, x) send request using client. Socket read reply from client. Socket close client. Socket

2. Socket-programming using TCP Stream: • A stream is a sequence of characters that

2. Socket-programming using TCP Stream: • A stream is a sequence of characters that flow into or out of a process. • An input stream is attached to some input source for the process, e. g. , keyboard or socket. • An output stream is attached to an output source, e. g. , monitor or socket. Client process client TCP socket

2. Socket-programming using TCP Example client-server app: 1) client reads line from standard input

2. Socket-programming using TCP Example client-server app: 1) client reads line from standard input (in. From. User stream) , sends to server via socket (out. To. Server stream) 2) server reads line from socket 3) server converts line to uppercase, sends back to client 4) client reads, prints modified line from socket (in. From. Server stream)

2. Socket-programming using TCP Java Client import java. io. *; import java. net. *;

2. Socket-programming using TCP Java Client import java. io. *; import java. net. *; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modified. Sentence; Create input stream Create client socket, connect to server Create output stream attached to socket Buffered. Reader in. From. User = new Buffered. Reader(new Input. Stream. Reader(System. in)); Socket client. Socket = new Socket("hostname", 6789); Data. Output. Stream out. To. Server = new Data. Output. Stream(client. Socket. get. Output. Stream());

2. Socket-programming using TCP Java Client (Cont) Buffered. Reader in. From. Server = new

2. Socket-programming using TCP Java Client (Cont) Buffered. Reader in. From. Server = new Buffered. Reader(new Input. Stream. Reader(client. Socket. get. Input. Stream())); Create input stream attached to socket sentence = in. From. User. read. Line(); Send line to server out. To. Server. write. Bytes(sentence + 'n'); Read line from server modified. Sentence = in. From. Server. read. Line(); System. out. println("FROM SERVER: " + modified. Sentence); out. To. Server. close(); client. Socket. close(); } }

2. Socket-programming using TCP Java Server import java. io. *; import java. net. *;

2. Socket-programming using TCP Java Server import java. io. *; import java. net. *; class TCPServer { public static void main(String argv[]) throws Exception { String client. Sentence; String capitalized. Sentence; Create welcoming socket at port 6789 Server. Socket welcome. Socket = new Server. Socket(6789); while(true) Wait, on welcoming socket for contact by client Create input stream, attached to socket { Socket connection. Socket = welcome. Socket. accept(); Buffered. Reader in. From. Client = new Buffered. Reader(new Input. Stream. Reader(connection. Socket. get. Input. Stream()));

2. Socket-programming using TCP Java Server(Cont) Crete output stream Attached to socket Read in

2. Socket-programming using TCP Java Server(Cont) Crete output stream Attached to socket Read in line from socket Data. Output. Stream out. To. Client = new Data. Output. Stream(connection. Socket. get. Output. Stream()); client. Sentence = in. From. Client. read. Line(); capitalized. Sentence = client. Sentence. to. Upper. Case() + 'n'; Write out line to socket out. To. Client. write. Bytes(capitalized. Sentence); } } } End of while loop, loop back and wait for another client connection

3. Socket-programming using UDP: no “connection” between client and server no handshaking sender explicitly

3. Socket-programming using UDP: no “connection” between client and server no handshaking sender explicitly attaches IP address and port of destination to each packet server must extract IP address, port of sender from received packet UDP: transmitted data may be received out of order, or lost UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server

3. Socket-programming using UDP Server (running on hostid) create socket, port=x, for incoming request:

3. Socket-programming using UDP Server (running on hostid) create socket, port=x, for incoming request: server. Socket = Datagram. Socket() read request from server. Socket write reply to server. Socket specifying client host address, port number Client create socket, client. Socket = Datagram. Socket() Create, address (hostid, port=x, send datagram request using client. Socket read reply from client. Socket close client. Socket

3. Socket-programming using UDP Client process Input: receives packet (recall that. TCP received “byte

3. Socket-programming using UDP Client process Input: receives packet (recall that. TCP received “byte stream”) Output: sends packet (recall that TCP sent “byte stream”) client UDP socket

3. Socket-programming using UDP Java Client import java. io. *; import java. net. *;

3. Socket-programming using UDP Java Client import java. io. *; import java. net. *; class UDPClient { public static void main(String args[]) throws Exception Create input stream Create client socket Translate hostname to IP address using DNS { Buffered. Reader in. From. User = new Buffered. Reader(new Input. Stream. Reader(System. in)); Datagram. Socket client. Socket = new Datagram. Socket(); Inet. Address IPAddress = Inet. Address. get. By. Name("hostname"); byte[] send. Data = new byte[1024]; byte[] receive. Data = new byte[1024]; String sentence = in. From. User. read. Line(); send. Data = sentence. get. Bytes(); 18

3. Socket-programming using UDP Java Client(Cont) Create datagram with data-to-send, length, IP addr, port

3. Socket-programming using UDP Java Client(Cont) Create datagram with data-to-send, length, IP addr, port Datagram. Packet send. Packet = new Datagram. Packet(send. Data, send. Data. length, IPAddress, 9876); client. Socket. send(send. Packet); Send datagram to server Datagram. Packet receive. Packet = new Datagram. Packet(receive. Data, receive. Data. length); Read datagram from server client. Socket. receive(receive. Packet); String modified. Sentence = new String(receive. Packet. get. Data()); System. out. println("FROM SERVER: " + modified. Sentence); client. Socket. close(); } } 19

3. Socket-programming using UDP Java Server import java. io. *; import java. net. *;

3. Socket-programming using UDP Java Server import java. io. *; import java. net. *; class UDPServer { public static void main(String args[]) throws Exception Create datagram socket at port 9876 { Datagram. Socket server. Socket = new Datagram. Socket(9876); byte[] receive. Data = new byte[1024]; byte[] send. Data = new byte[1024]; while(true) Create space for received datagram Receive datagram { Datagram. Packet receive. Packet = new Datagram. Packet(receive. Data, receive. Data. length); server. Socket. receive(receive. Packet); 20

3. Socket-programming using UDP Java Server (Cont) String sentence = new String(receive. Packet. get.

3. Socket-programming using UDP Java Server (Cont) String sentence = new String(receive. Packet. get. Data()); Get IP addr port #, of sender Inet. Address IPAddress = receive. Packet. get. Address(); int port = receive. Packet. get. Port(); String capitalized. Sentence = sentence. to. Upper. Case(); send. Data = capitalized. Sentence. get. Bytes(); Create datagram to send to client Datagram. Packet send. Packet = new Datagram. Packet(send. Data, send. Data. length, IPAddress, port); Write out datagram to socket server. Socket. send(send. Packet); } } } End of while loop, loop back and wait for another datagram 21

4. Evaluation 1. Modify the sever code (the one in the slides), so that

4. Evaluation 1. Modify the sever code (the one in the slides), so that the server process counts the number of characters in the line and returns it to the client. The client will print the number on its monitor. 2. Write a client process that will communicate with a server that you will also write. Your client will open a TCP socket to your server and send a message to your server containing your name (X). The server will accept connection from your client and will send a message (“Hello X”) back to your client, where X is replaced by your name. The client will print the message.

3. Team up with someone from the class. You should get your client and

3. Team up with someone from the class. You should get your client and their server processes to inter-operate with each other (or vice versa). Your client should send a message contains your name and your partner name and their server should eventually print out the message after changing it to lower case. 4. Repeat problem 1 using UDP sockets

References: Computer Networking: A Top Down Approach Featuring the Internet, 6 th edition. Jim

References: Computer Networking: A Top Down Approach Featuring the Internet, 6 th edition. Jim Kurose, Keith Ross Addison-Wesley, July 2013.