Sunday 15 February 2015

UDP Sockets in C -


I am working on homework issues for class. I want to start a UDP server which listens for a file request opens the file and sends it back to the client requesting with the UDP.

Here is the server code.

  // Create UDP socket if ((sockfd = socket (AF_INET), SOCK_DGRAM, 0)) == -1) {glitch ("can not create socket"); Exit (-1); } // Configure socket memstant (& server, 0, size server); Server.can_family = AF_INET; // Use the IPv4 server. Sine_addr.s_addr = htonl (INADDR_ANY); // My IP server. Sin_port = htons (atoi (argv [1])); // server port // bind socket if ((bind (scaffold, (structure sokadar *) and server, size (server))) == -1) {closed (sockfd); Lies ("can not bind"); } Printf ("Listener: Waiting with recvfrom ... \ n"); If (listen (sockfd, 5) == -1) {lie ("can not listen for connection"); Exit (-1); } While (1) {client_len = sizeof (struct sockaddr_in); Newsockfood = Acceptable (Sokfad, (Structured Socder *) and Client, and Client_LAN); If (Newsflof <0) {false ("error on acceptance"); } // How do some parse requests // I use recv or recvfrom? // Do I send new UDP socket to send back data to client? Send File (NewsForF, Filename); Close (newsockfd); } Closed (sockfd);  

How do I lose data How do I read data from clients? And how can I return a new UDP connection to the customer?

How UDP is different from TCP:

  • Message -Inward, stream-oriented You do not read / write / send. When you sendto / recvfrom message size limited to 64k, a message is sent to each call sendto by calling a call to recvfrom. If RickForm passes a buffer that is smaller than the size of the message, then the remaining message goes for the best.

  • No connection Therefore, no listen / accept / not connect you send a message to a particular address / port when you receive the message (on the address / port on which your socket is bound), you will see the source of the incoming message from the output of RAWFOM Get in the form of the parameter.

  • No guarantee messages can be deleted in order. If I miss the right way, then they can be reduced, though.

One last word of caution - you want to find yourself again on the UDP. In that case, wait and go back to TCP.


No comments:

Post a Comment