Process Communication COMPUTER NETWORKING Part 2

Slides:



Advertisements
Similar presentations
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r Sockets are explicitly created, used, released by applications.
Advertisements

Network Programming and Java Sockets
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Application Layer – Lecture.
2: Application Layer 1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
9/23/2003-9/25/2003 Sockets & DNS September 23-25, 2003.
1 Creating a network app Write programs that  run on different end systems and  communicate over a network.  e.g., Web: Web server software communicates.
2: Application Layer1 Data Communication and Networks Lecture 12 Java Sockets November 30, 2006.
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
Internet and Intranet Protocols and Applications Lecture 4: Application Layer 3: Socket Programming February 8, 2005 Arthur Goldberg Computer Science Department.
Networking Overview February 2, /2/2004 Assignments Due – Homework 0 Due – Reading and Warmup questions Work on Homework 1.
2: Application Layer1 Chapter 2 (continued) Application Layer – part 2 Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim.
Socket programming with UDP and TCP. Socket Programming with TCP Connection oriented – Handshaking procedure Reliable byte-stream.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
TCP Socket Programming CPSC 441 Department of Computer Science University of Calgary.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
1 Network Layers Application Transport Network Data-Link Physical bits.
Protocols Rules for communicating between two entities (e.g., a client and a server) “A protocol definition specifies how distributed system elements interact.
Welcome to CIS 235 Computer Networks Fall, 2007 Prof Peterson.
2: Application Layer 1 Socket Programming Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley,
JAVA Socket Programming Joonbok Lee KAIST.
2: Application Layer 1 Socket Programming TCP and UDP.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
ECE5650: Network Programming
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
1 1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of.
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach Featuring the Internet, 2 nd edition. Jim Kurose, Keith Ross.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
CS 3830 Day 11 Introduction : Application Layer 2 Server-client vs. P2P: example Client upload rate = u, F/u = 1 hour, u s = 10u, d min ≥ u s.
Chapter 2 Application Layer Computer Networking: A Top Down Approach, 5 th edition. Jim Kurose, Keith Ross Addison-Wesley, April A note on the use.
Winter 2002Suprakash Datta1 Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
Socket Programming Lee, Sooyong
Network Programming and Sockets CPSC 363 Computer Networks Ellen Walker Hiram College (Includes figures from Computer Networking by Kurose & Ross, © Addison.
Socket Programming Tutorial. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Java Socket programming. Socket programming with TCP.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
2: Application Layer1 Socket programming Socket API Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
1 CSCD 330 Network Programming Spring 2014 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 7 Application.
1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 11 Omar Meqdadi Department of Computer Science and Software Engineering University.
1 All rights reserved to Chun-Chuan Yang Upon completion you will be able to: The OSI Model and the TCP/IP Protocol Suite Understand the architecture of.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
Socket programming in C
Socket Programming Socket Programming Overview
Transport layer (last part) Application layer
CS 1652 Jack Lange University of Pittsburgh
DNS: Domain Name System
Socket programming with TCP
Chapter 2: outline 2.1 principles of network applications
Socket programming - Java
CSCD 330 Network Programming
Socket Programming Socket Programming Overview
Socket Programming.
CSCD 330 Network Programming
Socket Programming 2: Application Layer.
DNS: Domain Name System
CPSC 441 UDP Socket Programming
Chapter 2: Application layer
DNS: Domain Name System
DNS: Domain Name System
Socket Programming with UDP
Socket programming in C
Presentation transcript:

Process Communication COMPUTER NETWORKING Part 2 Client-server paradigm and Socket Programming ch 18 Thanks to the authors of the textbook [USP] and [KR] for providing the base slides. I made several changes/additions. These slides may incorporate materials kindly provided by Prof. Dakai Zhu. So I would like to thank him, too. Turgay Korkmaz korkmaz@cs.utsa.edu

Computer Networking Layered Protocols Grand tour of computer networking, the Internet Client-server paradigm, Socket Programming

Objectives To understand how processes communicate (the heart of distributed systems) To understand computer networks and their layers (part 1) To understand client-server paradigm and Use low-level message passing using sockets

Client-Server Communication models The client/server model underlies almost every distributed system (networked applications). Hence it is important to understand the principles of client/server communication. In this second part, we are going to use the knowledge about communication primitives that we just acquired to explain the principles of client/server communication. In client/server communication many things can go wrong and a good client/server communication protocol takes this into account and delivers reliable results even in the presence of failures. Messages are occasionally dropped. Network may become partitioned. Processes may fail. No corruption of data, as ensured by error-checking mechanisms at the network level. We are going to discuss different degrees of quality of service as a basis for the discussion of three client server protocols. The simplest of these protocols is the request protocol, which only achieves a limited quality and cannot generally be applied. The most important protocol is the request/reply protocol. It delivers either the result of a service execution or a failure to the client. An optimization is the request/reply/acknowledgement protocol. Client-Server Communication models

Client-server architecture communicate with server may have dynamic IP addresses and port do not communicate directly with each other may be intermittently connected to the network servers: always-on host permanent IP address, well-known port server farms for scaling Clients and servers communicate through Socket, RPC, RMI From Computer Networking by Kurose and Ross.

Request Protocol (R) If service OPT Request Protocol (R) If service does not have output parameters and does not have a return type client may not want to wait for server to finish. request send(...) Client Server receive(...) exec op; Continue execution The R protocol may be used when there is no value to be returned from the procedure and the client requires no confirmation that the procedure has been executed. The client may proceed immediately after the request message is sent as there is no need to wait for a reply message. Perfect system: Sites and network never fail, no message loss, no corrupted messages The R protocol is ok TCP/IP: No corrupted message; message loss, site and communication failures may occur The R protocol only offers best-effort Client does not detect any communication or site failure Server fails before execution service Communication fails before delivering message Message loss Example: Notification kind message/service; RIP (Routing Information protocol)

Request-Reply protocol (RR) To be applied if client expects result from server Client requests service execution from server through request message, and Delivery of service result in reply message Most client-server interactions are built on RR protocol send(...) receive(...) Client Server exec op; request reply blocked The general message exchange protocol for client-server applications The RR protocol is useful for most client-server exchanges because it is based on the request-reply protocol. Special acknowledgement messages are not required because a server’s reply message is regarded as an acknowledgement of the client’s request message. Similarly, a subsequent call from a client may be regarded as an acknowledgement of a server’s reply message. message exchange protocol : format, order, action

Request-Reply-Acknowledge Protocol (RRA) OPT Request-Reply-Acknowledge Protocol (RRA) In addition to RR protocol, client sends acknowledgement after it received reply Acknowledgement sent asynchronously send(...) receive(...) send (...) Client Server exec op; request reply history The RRA protocol is based on the exchange of three messages: request-reply-acknowledge reply. The acknowledge reply message contains the requestId from the reply message being acknowledged. The arrival of a requestId in an acknowledgement message will be interpreted as acknowledging the receipt of all reply messages with lower requestIds, so the loss of an acknowledgement message is harmless. Although the exchange involves an additional message, it need not block the client as acknowledgement may be transmitted after the reply has been given to the client, but it does use processing and network resources.

Issues in Client-Server Communication Addressing (IP address, port number) Blocking versus non-blocking Buffered versus unbuffered Reliable versus unreliable Server architecture: concurrent versus sequential Scalability

Addressing Issues OPT Question: how does the client know where the server is located? Hard-wired address Machine address and process address are known a priori (www.cs.utsa.edu port 80) DNS converts www… to 32-bit IP Broadcast-based Server chooses address from a sparse address space Client broadcasts request Can cache response for future Locate address via name server user server user server NS user server

Blocking versus Non-blocking OPT Blocking versus Non-blocking Blocking communication (synchronous) Sender blocks until message is actually sent Receiver blocks until message is actually received Non-blocking communication (asynchronous) Sender returns immediately Receiver does not block either Examples:

Buffering Issues Unbuffered communication Buffered communication OPT Buffering Issues Unbuffered communication Server must call receive before client can call send Buffered communication Client send to a mailbox Server receives from a mailbox user server user server

Reliability Unreliable channel Reliable channel OPT Reliability Unreliable channel Need acknowledgements (ACKs) Applications handle ACKs ACKs for both request and reply Reliable channel Reply acts as ACK for request Explicit ACK for response Reliable communication on unreliable channels Transport protocol (TCP) handles lost messages request ACK User reply Server ACK request User reply Server ACK

Server Architecture Sequential Concurrent Thus servers could be Serve one request at a time Can service multiple requests by employing events and asynchronous communication Concurrent Server spawns a process (or thread) to service each request Can also use a pre-spawned pool of threads/processes (apache) Thus servers could be Pure-sequential, event-based, thread-based, process-based Discussion: which architecture is most efficient?

Scalability Question: How can you scale the server capacity? OPT Scalability Question: How can you scale the server capacity? Buy bigger machine! Replicate Distribute data and/or algorithms Ship code instead of data Cache

Learn how to build client/server applications that communicate with each other using sockets Socket programming From Computer Networking by Kurose and Ross.

What is a socket? Socket API a host-local, application-created, OS-controlled interface (a “door”) that hides the details of all the layers and provides transport services to application process so it can send and receive messages to/from another application process socket Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm two types of transport service via socket API: UDP: unreliable datagram TCP: reliable, in-order byte-stream From Computer Networking by Kurose and Ross.

Socket programming C From Computer Networking by Kurose and Ross.

Socket-programming using TCP TCP service: connection-oriented, reliable, in-order byte-stream controlled by application developer controlled by application developer process TCP with buffers, variables socket process TCP with buffers, variables socket (i.e., transfer bytes from one process to another, no structure!) controlled by operating system controlled by operating system internet server client From Computer Networking by Kurose and Ross.

Socket programming with TCP Client must know server’s IP and port Client then creates a TCP socket connects it to server by specifying IP address and port number of server process read/write data Server process must first be running Server creates a TCP welcome socket and binds it to a known port and waits for requests When contacted by client, server creates new TCP socket for server process to communicate with client read/write data Server can talk with multiple clients TCP socket is identified by 4-tuple: source IP address source port number dest IP address dest port number From Computer Networking by Kurose and Ross.

Client/server socket interaction: TCP ***** Client/server socket interaction: TCP Server (running on hostid, port x) Client (running on hostname ?, port ?) // create a TCP socket, // bind to port x, welcomeSocket = socket(…,SOCK_STREAM,0); bind(welcomeSocket, x); listen(welcomeSocket); // create socket, // connect to hostid, port x clientSocket = socket( …, SOCK_STREAM, 0) connect(clientSocket, hostid, x) TCP connection setup wait for incoming connection request connectionSocket = accept(welcomeSocket) write request using clientSocket read request from connectionSocket write reply to read reply from clientSocket close close connectionSocket

Connection-oriented demux: Threaded Web Server Server can fork a child process to handle each request from different clients Too costly, use threads! P1 P4 P2 P1 P3 SP: 9157 DP: 80 S-IP: A D-IP:C connect SP: 5775 DP: 80 D-IP:C S-IP: B SP: 9157 DP: 80 D-IP:C S-IP: B SP: 9157 DP: 80 S-IP: A D-IP:C client IP: A Client IP:B server IP: C From Computer Networking by Kurose and Ross.

TCP Socket Primitives ***** Primitive Function Socket Connection socket Primitive Function Socket Create a new communication endpoint Bind Attach a local address to a socket Listen Announce willingness to accept connections Accept Block caller until a connection request arrives Connect Actively attempt to establish a connection Send Send some data over the connection Recv Receive some data over the connection Close Release the connection

USP ch 18: Socket Implementation of UICI OPT USP ch 18: Socket Implementation of UICI

USP ch 18: UICI: Client/Server Interaction OPT USP ch 18: UICI: Client/Server Interaction

Create welcoming socket at port Complete example is posted at the class web site. See the socket programming link under “Online Materials” Example: C server (TCP) /* server.c */ void main(int argc, char *argv[]) { struct sockaddr_in sad; /* structure to hold an IP address */ struct sockaddr_in cad; int welcomeSocket, connectionSocket; /* socket descriptor */ struct hostent *ptrh; /* pointer to a host table entry */ char clientSentence[128]; char capitalizedSentence[128]; port = atoi(argv[1]); welcomeSocket = socket(PF_INET, SOCK_STREAM, 0); memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */ sad.sin_port = htons((u_short)port);/* set the port number */ bind(welcomeSocket, (struct sockaddr *)&sad, sizeof(sad)); Create welcoming socket at port & Bind a local address

Example: C server (TCP), cont /* Specify the maximum number of clients that can be queued */ listen(welcomeSocket, 10) while(1) { connectionSocket=accept(welcomeSocket, (struct sockaddr *)&cad, &alen); n=read(connectionSocket, clientSentence, sizeof(clientSentence)); /* capitalize Sentence and store the result in capitalizedSentence*/ n=write(connectionSocket, capitalizedSentence, strlen(capitalizedSentence)+1); close(connectionSocket); } Wait, on welcoming socket for contact by a client Write out the result to socket End of while loop, loop back and wait for another client connection

Example: C client (TCP) /* client.c */ void main(int argc, char *argv[]) { struct sockaddr_in sad; /* structure to hold an IP address */ int clientSocket; /* socket descriptor */ struct hostent *ptrh; /* pointer to a host table entry */ char Sentence[128]; char modifiedSentence[128]; host = argv[1]; port = atoi(argv[2]); clientSocket = socket(PF_INET, SOCK_STREAM, 0); memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_port = htons((u_short)port); ptrh = gethostbyname(host); /* Convert host name to IP address */ memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length); connect(clientSocket, (struct sockaddr *)&sad, sizeof(sad)); Create client socket, connect to server

Example: C client (TCP), cont Get input stream from user gets(Sentence); n=write(clientSocket, Sentence, strlen(Sentence)+1); n=read(clientSocket, modifiedSentence, sizeof(modifiedSentence)); printf("FROM SERVER: %s\n”,modifiedSentence); close(clientSocket); } Send line to server Read line from server Close connection

Other related functions if ( (pid=fork()) == 0) { /* CHILD PROC */ close(welcomeSocket); /* give service */ exit(0); } /* PARENT PROC */ close(connectionSocket); getpeername( ) gethostbyname( ) gethostbyaddr( ) getsockopt( ) setsockopt ( ) sigaction(SIGINT,…); Too costly, use threads!

Waiting something from both socket and stdin OPT Waiting something from both socket and stdin FD_ZERO(&rset); FD_SET(welcomeSocket, &rset); FD_SET(fileno(stdin), &rset); maxfd =max(welcomeSocket,fileno(stdin)) + 1; select(maxfd, &rset, NULL, NULL, NULL); if (FD_ISSET(fileno(stdin), &rset)){ /* read something from stdin */ }

Exercise: implement a client-server program using TCP sockets. The client will first establish a TCP connection with the server. It then sends the name of a customer and expects server to send back the balance of that customer. Upon receiving the balance, the client will print it and quit. The server always waits for connection requests in an infinite loop. Upon receiving a connection request, the server (parent process) creates a child process, which performs the task(s) expected by the client. The parent process (server) will continue to wait for another client request. Suppose Server keeps accounts in an array of struct account { char name[20]; double balance;} (create a static array of 10 customers with random names, balance in your program) Make sure you close the socket descriptors that are not needed in the parent and child.  When implementing client and server programs, please focus on the key system calls and in which order to call them while using generic parameters (e.g., IPaddress, portnum) rather than actual structures. Also ignore error checking for clarity and assume all the necessary libraries are included.

Socket programming with UDP OPT Socket programming with UDP 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 application viewpoint UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server From Computer Networking by Kurose and Ross.

Client/server socket interaction: UDP OPT Client/server socket interaction: UDP Server (running on hostid, port x) create a UDP socket, clientSocket = socket(…, SOCK_DGRAM, 0); Client (running on hostname ?, port ?) Create, address (hostid, port=x) send datagram request using clientSocket Create a UDP socket, port=x, for incoming request: serverSocket = socket(…, SOCK_DGRAM, 0); bind(serverSocket, x); receive request from serverSocket close clientSocket recv reply from clientSocket send reply to serverSocket specifying client host address, port number

Connectionless demux (cont) OPT Connectionless demux (cont) Server create UDP Socket and binds it to port 6428 Clients create a UDP socket (port number will be dynamically assigned) P2 client IP: A P1 P1 P3 SP: 6428 DP: 9157 SP: 6428 DP: 5775 SP: 9157 DP: 6428 SP: 5775 DP: 6428 Client IP:B server IP: C SP provides “return address” From Computer Networking by Kurose and Ross.

UDP: Connectionless demultiplexing OPT UDP: Connectionless demultiplexing When host receives UDP segment: checks destination port number in segment directs UDP segment to socket with that port number IP datagrams with different source IP addresses and/or source port numbers directed to same socket UDP socket is identified by two-tuple: (dest IP address, dest port number) From Computer Networking by Kurose and Ross.

Create welcoming socket at port OPT Example: C server (UDP) Complete example is posted at the class web site. See the socket programming link under “Online Materials” /* server.c */ void main(int argc, char *argv[]) { struct sockaddr_in sad; /* structure to hold an IP address */ struct sockaddr_in cad; int serverSocket; /* socket descriptor */ struct hostent *ptrh; /* pointer to a host table entry */ char clientSentence[128]; char capitalizedSentence[128]; port = atoi(argv[1]); serverSocket = socket(PF_INET, SOCK_DGRAM, 0); memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */ sad.sin_port = htons((u_short)port);/* set the port number */ bind(serverSocket, (struct sockaddr *)&sad, sizeof(sad)); Create welcoming socket at port & Bind a local address

Example: C server (UDP), cont OPT Example: C server (UDP), cont while(1) { n=recvfrom(serverSocket, clientSentence, sizeof(clientSentence), 0 (struct sockaddr *) &cad, &addr_len ); /* capitalize Sentence and store the result in capitalizedSentence*/ n=sendto(serverSocket, capitalizedSentence, strlen(capitalizedSentence)+1,0 (struct sockaddr *) &cad, &addr_len); } close(serverSocket); Receive messages from clients End of while loop, loop back and wait for another client connection Write out the result to socket

NO connection to server OPT Example: C client (UDP) /* client.c */ void main(int argc, char *argv[]) { struct sockaddr_in sad; /* structure to hold an IP address */ int clientSocket; /* socket descriptor */ struct hostent *ptrh; /* pointer to a host table entry */ char Sentence[128]; char modifiedSentence[128]; host = argv[1]; port = atoi(argv[2]); clientSocket = socket(PF_INET, SOCK_DGRAM, 0); /* determine the server's address */ memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_port = htons((u_short)port); ptrh = gethostbyname(host); /* Convert host name to IP address */ memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length); Create client socket, NO connection to server

Example: C client (UDP), cont. OPT Example: C client (UDP), cont. Get input stream from user gets(Sentence); addr_len =sizeof(struct sockaddr); n=sendto(clientSocket, Sentence, strlen(Sentence)+1, (struct sockaddr *) &sad, addr_len); n=recvfrom(clientSocket, modifiedSentence, sizeof(modifiedSentence). (struct sockaddr *) &sad, &addr_len); printf("FROM SERVER: %s\n”,modifiedSentence); close(clientSocket); } Send line to server Read line from server Close connection

Socket programming JAVA EXTRAS for self study Socket programming JAVA From Computer Networking by Kurose and Ross.

Client/server socket interaction: TCP Server (running on hostid, port x) Client (running on hostname ?, port ?) create socket, port=x, for incoming request: welcomeSocket = ServerSocket() TCP connection setup close connectionSocket read reply from clientSocket create socket, connect to hostid, port=x clientSocket = Socket() wait for incoming connection request connectionSocket = welcomeSocket.accept() send request using clientSocket read request from connectionSocket write reply to From Computer Networking by Kurose and Ross.

Socket programming with TCP Example client-server app: 1) client reads line from standard input (inFromUser stream) , sends to server via socket (outToServer 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 (inFromServer stream) Client process Stream jargon 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 TCP socket From Computer Networking by Kurose and Ross.

Example: Java server (TCP) Complete example is posted at the class web site. See the socket programming link under “Online Materials” import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket From Computer Networking by Kurose and Ross.

Example: Java server (TCP), cont DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } Create output stream, attached to socket Read in line from socket Write out line to socket End of while loop, loop back and wait for another client connection From Computer Networking by Kurose and Ross.

Example: Java client (TCP) import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket(“localhost", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); Create input stream Create client socket, connect to server Create output stream attached to socket From Computer Networking by Kurose and Ross.

Example: Java client (TCP), cont. Create input stream attached to socket BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } Send line to server Read line from server From Computer Networking by Kurose and Ross.

Client/server socket interaction: UDP Server (running on hostid) create socket, clientSocket = DatagramSocket() Client (running on hostname ?, port ?) Create datagram with server IP and port=x; send datagram via clientSocket create socket, port= x. serverSocket = DatagramSocket() read datagram from serverSocket close clientSocket read datagram from write reply to serverSocket specifying client address, port number From Computer Networking by Kurose and Ross.

Example: Java client (UDP) process Input: receives packet (recall thatTCP received “byte stream”) Output: sends packet (recall that TCP sent “byte stream”) client UDP socket From Computer Networking by Kurose and Ross.

Example: Java server (UDP) Complete example is posted at the class web site. See the socket programming link under “Online Materials” import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); Create datagram socket at port 9876 Create space for received datagram Receive datagram From Computer Networking by Kurose and Ross.

Example: Java server (UDP), cont String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } Get IP addr port #, of sender Create datagram to send to client Write out datagram to socket End of while loop, loop back and wait for another datagram From Computer Networking by Kurose and Ross.

Example: Java client (UDP) import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName(“localhost"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes(); Create input stream Create client socket Translate hostname to IP address using DNS From Computer Networking by Kurose and Ross.

Example: Java client (UDP), cont. Create datagram with data-to-send, length, IP addr, port DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } Send datagram to server Read datagram from server From Computer Networking by Kurose and Ross.

Multi threaded import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } Multi threaded