Presentation is loading. Please wait.

Presentation is loading. Please wait.

UDP and Multi-thread Socket Programming

Similar presentations


Presentation on theme: "UDP and Multi-thread Socket Programming"— Presentation transcript:

1 UDP and Multi-thread Socket Programming

2 UDP (User Datagram Protocol)
Another transport layer protocol (recall TCP). Difference from TCP: TCP guarantees reliable data transmission by establishing connection between client and server. UDP does not establish connection between server and client. It is a connection-less protocol. UDP, thus, does not guarantee the arrival, arrival time and content of the message.

3 UDP Socket Programming Overview
UDP Server socket() sendto() recvfrom() UDP Client bind() Blocking function close()

4 UDP Socket Programming
Syntax: int socket(int family, int type, int protocol); To create a UDP socket: int sd = socket(AF_INET,SOCK_DGRAM,0);

5 UDP Socket Programming
sendto() and recvfrom() Syntax: sendto(int sd,void* buf,int bufLen,int flags,struct sockaddr* saddr,int addrlen); recvfrom(int sd,void* buf,int bufLen,int flags,struct sockaddr* saddr,int* addrlen); Example:

6 UDP Socket Programming
sendto() and recvfrom() Return value: how many bytes are successfully sent. The return value may not be equal to the argument bufLen. Use a while loop to send and receive data.

7 UDP Socket Programming
Bi-direction communication. There is a struct sockaddr* saddr argument in recvfrom() function, after executing recvfrom(), the address of the sender is thus stored in *saddr. We can then send the data using the address *saddr. See the sample codes for more information.

8 Demo: UDP-based Heartbeat
Client sends heartbeat to server every three seconds. Server sends a response after receiving a heartbeat.

9 Multi-thread Socket Programming
Recall the blocking functions in the first tutorial. The accept(), recv() and recvfrom() function. With a single thread, the program are blocked by one function and cannot proceed other operations. Consider a chat program, how can a client send out a message while the program is blocked by recv() function?

10 Example: TCP-Based Chatting Program.
We extend the server and client program in the sample codes of first tutorial. We want both server and client able to send and receive messages from the other end. Problem: How to send message if the program is blocked by a recv() function? To send a message, we first send a 4-byte integer of the message length and then the message.

11 Design For both server and client sides, we have two threads:
Main thread: read message from standard input and send to the other end. The worker thread: receive message and send to the other end.

12 Design Mater thread Establish connection Create thread Wait for input Send message Worker thread Recv message print message The recv() function just block the worker thread, instead of the whole program.

13 Basic Principle Do not let blocking functions affect other operations.
Question: In the heartbeat example, both server and client need to do send and receive. Why do not we use a multi-thread model?

14 Q&A


Download ppt "UDP and Multi-thread Socket Programming"

Similar presentations


Ads by Google