Linux Network programming UDP Socket Program Example _c language

Source: Internet
Author: User
Tags htons

In the network transport Protocol, the TCP protocol provides a reliable, complex, connection-oriented data stream (SOCK_STREAM) transport service that establishes the connection through a three-segment handshake process. TCP has a "retransmission acknowledgement" mechanism, that is, the receiving end of the data to send a positive confirmation of the signal, the sender if the receiver received a positive confirmation of the signal, it will continue to send other data, if not, it will resend.

In contrast, theUDP protocol is a connectionless, unreliable datagram (SOCK_DGRAM) transport service. Instead of establishing a connection using a UDP socket interface, the server can communicate (recvfrom function and SendTo function) after invoking the socket () to generate a socket and invoke bind () binding port (the client is using a socket ()) After you generate a socket, you can send and receive data to the server address.

Special note here:TCP uses a stream socket (SOCK_STREAM) and UDP uses a datagram socket (SOCK_DGRAM)

Examples of UDP socket programming:

The server-side code is as follows:

/************************************************************************* > File name:server.c > Author:song Lee ************************************************************************/#include <sys/types.h> include<sys/socket.h> #include <unistd.h> #include <netinet/in.h> #include <arpa/inet.h> # include<stdio.h> #include <stdlib.h> #include <errno.h> #include <netdb.h> #include < stdarg.h> #include <string.h> #define SERVER_PORT 8000 #define BUFFER_SIZE 1024 #define FILE_NAME_MAX_SIZE 5 
 the int main () {*/* creates the UDP socket interface * * * struct sockaddr_in server_addr; 
 Bzero (&server_addr, sizeof (SERVER_ADDR)); 
 server_addr.sin_family = af_inet; 
 SERVER_ADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any); 
 
 Server_addr.sin_port = htons (Server_port); 
 /* Create socket */int SERVER_SOCKET_FD = socket (af_inet, SOCK_DGRAM, 0); 
  if (server_socket_fd = = 1) {perror ("Create socket Failed:"); 
 Exit (1); } 
 
 /* Binding Sleeve Interface/if ( -1 = (Bind (server_socket_fd, (struct sockaddr*) &server_addr,sizeof (SERVER_ADDR))) {perror ("Serve 
  R Bind Failed: "); 
 Exit (1); 
  /* Data transfer/while (1) {/* Defines an address for capturing the client address/struct sockaddr_in client_addr; 
 
  socklen_t client_addr_length = sizeof (CLIENT_ADDR); 
  /* Receive data */char buffer[buffer_size]; 
  Bzero (buffer, buffer_size); if (Recvfrom (server_socket_fd, buffer, buffer_size,0, (struct sockaddr*) &client_addr, &client_addr_length) = = 
   -1) {perror ("Receive Data Failed:"); 
  Exit (1); 
  /* * Copy out file_name/char file_name[file_name_max_size+1 from buffer; 
  Bzero (file_name,file_name_max_size+1); strncpy (file_name, buffer, strlen (buffer) >file_name_max_size? 
  File_name_max_size:strlen (buffer)); 
 printf ("%s\n", file_name); 
 Close (SERVER_SOCKET_FD); 
return 0; 

 }

The

client-side code is as follows:

/************************************************************************* > File name:client.c > Author:song Lee ************************************************************************/#include <sys/types.h> include<sys/socket.h> #include <unistd.h> #include <netinet/in.h> #include <arpa/inet.h> # include<stdio.h> #include <stdlib.h> #include <errno.h> #include <netdb.h> #include < stdarg.h> #include <string.h> #define SERVER_PORT 8000 #define BUFFER_SIZE 1024 #define FILE_NAME_MAX_SIZE 5 
 int main () {/* Service End Address * * struct sockaddr_in server_addr; 
 Bzero (&server_addr, sizeof (SERVER_ADDR)); 
 server_addr.sin_family = af_inet; 
 SERVER_ADDR.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1"); 
 
 Server_addr.sin_port = htons (Server_port); 
 /* Create socket */int CLIENT_SOCKET_FD = socket (af_inet, SOCK_DGRAM, 0); 
  if (CLIENT_SOCKET_FD < 0) {perror ("Create socket Failed:"); 
 Exit (1); 
 
} /* input filename to buffer/char file_name[file_name_max_size+1]; 
 Bzero (file_name, file_name_max_size+1); 
 printf ("Please Input File Name on server:\t"); 
 
 scanf ("%s", file_name); 
 Char Buffer[buffer_size]; 
 Bzero (buffer, buffer_size); strncpy (buffer, file_name, strlen (file_name) >buffer_size? 
 
 Buffer_size:strlen (file_name)); /* Send file name */if (sendto (client_socket_fd, buffer, buffer_size,0, (struct sockaddr*) &server_addr,sizeof (SERVER_ADDR) 
  < 0) {perror ("Send File Name Failed:"); 
 Exit (1); 
 Close (CLIENT_SOCKET_FD); 
return 0; 

 }

Readers can refer to the previous: Linux network programming Socket File Transfer example, pay attention to the UDP and TCP workflow comparison. To deepen understanding of the principle of the program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.