Presentation is loading. Please wait.

Presentation is loading. Please wait.

TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.

Similar presentations


Presentation on theme: "TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery."— Presentation transcript:

1 TCP Sockets Reliable Communication

2 TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery No duplicates Rate-limiting It allows you to write to another host on the network as if it were a file stream TCP only passes bytes; it is completely agnostic about what they mean. Making sense of the bytes is up to you, the programmer (or the protocol receiving the bytes)

3 Socket A “socket” describes a connection between two hosts To set up a stream between two hosts you need a “socket pair” that describes the connection between the client and the server Server 172.20.40.12 Client 172.20.40.88 Port TCP 4567Port TCP 4485

4 Socket The socket pair can be described as (( 172.20.40.12, 4567), (172.20.40.88, 4485)) The client IP, a client port, the server IP, and the well- known port we connect to on the server

5 Server Socket The standard technique is: client initiates connection to server Before we can establish a connection, we have to have something waiting for the connection on the server side This is a “server socket”, a piece of software that waits for clients to connect.

6 Server Socket Waiting for a connection at 172.20.40.88, 4485 Server 172.20.40.12 Client 172.20.40.88 Port TCP 4567Port TCP 4485

7 Multiple Connections What if two people want to connect to a web server at the same time? Notice that the socket connection has a unique ID based on the socket pair if two hosts connect, or even if two programs from the same host connect You can see this with “netstat -an”

8 Two Connections from Same Host Server 172.20.40.12 Client 172.20.40.88 Port TCP 4567Port TCP 4485 Port TCP 4568Port TCP 4485 What are the socket pairs here?

9 Example Code See TcpServer.java, TcpClient.java This simply establishes a connection and sends a simple message and response

10 Message Format? So what type message should you send? Since you get to make it up, it’s good for you if you pick something simple and robust For TCP sockets, this is usually ASCII text Why? Binary is different from host to host You can easily debug ASCII it is usually fast enough for what TCP does

11 Examples of TCP Message Formats You can establish your own interactive TCP connection to a server with “telnet ” (usually typing blind on windows machines) HTTP (web servers): GET /index.html SMTP (email) fake mail

12 SMTP Fakemail telnet mustang.nps.edu 25 MAIL FROM: admiral@nps.eduadmiral@nps.edu RCPT TO: yourBuddy@nps.eduyourBuddy@nps.edu DATA You are instructed to appear at the next inspection in a speedo. QUIT Note the period on a line by itself

13 Stateless vs. Stateful Protocols Note a subtle difference between the HTTP and SMTP protocols HTTP is stateless--you send one text command, the server processes that, and then the socket connection can be torn down and the server completely forgets that it ever talked to you before A stateful protocol depends on prior commands sent from the client. The RCPT TO and DATA commands depend on the prior MAIL FROM command. Stateless protocols are good. They scale well, and are simple.

14 Sequential vs Parallel What happens to our simple ping-pong example if the server takes a long time to process a command? How can we fix this? Use a thread per client connection--then we can go back and do another accept() wile the first command is still processing This is a parallel server

15 TCP Protocols You want to use text for the protocol commands if you possibly can Keep it simple. Simple can be debugged and may actually work. Complex will not. If you possibly can, start off with a sequential, stateless server

16 Assignment Write a client and server that accepts a simple position command for an entity. Include an entity identifier and position (x,y,z) Write a simple HTTP client program that sends a request for a URL to a server and gets a response back


Download ppt "TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery."

Similar presentations


Ads by Google