Socket socket (SOCKET)
Socket SOCKET:IP address + port port number. In the TCP/IP protocol, it uniquely identifies a process in network traffic. sockets are used to describe a one-to-one relationship between network connections.
The TCP/IP protocol specifies that the network traffic should take a big endian byte order, i.e. (memory) low address high byte (data).
Second, UDP_socket related
UDP protocol----User Datagram Protocol (for non-connected)---sock_dgram
H means that host,n indicates that network,l represents a 32-bit long integer and s represents a 16-bit short integer.
IPV4 address format defined in Netinet/in.h, IPv4 address: sockaddr_in struct, including 16-bit port number and 32-bit IP address
struct sockaddr_in {uint8_t sin_len; sa_family_t sin_family; in_port_t Sin_port; struct IN_ADDR sin_addr; Char sin_zero[8];};
Third, UDP socket instance:
Udp_server.c
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h># include<arpa/inet.h> #include <netinet/in.h> #include <unistd.h> #include <string.h> #include <errno.h>void usage (Const char *proc) {printf ("%s:[ip][port]\n", proc);} Int main (int argc,char *argv[]) {if (argc != 3) {usage (argv[0]); return 1;} Char *_ip=argv[1];int _port=atoi (argv[2]); Int sock=socket (af_inet,sock_dgram,0); if (sock < 0) {perror ("socket"); exit (1);} Struct sockaddr_in local;local.sin_family=af_inet;local.sin_port=htons (_port); local.sin_addr.s_addr= INET_ADDR (_IP); if (Bind (sock, (struct sockaddr*) &local,sizeof (local) < 0) {perror ("bind"); Exit (2);} int done=0;char buf[1024];struct sockaddr_in client;socklen_t len=sizeof (client); while (!done) {Ssize_t _size=recvfrom (sock,buf,sizeof (BUF) -1,0, (struct sockaddr*) &client,&lEN), if (_size > 0) {buf[_size]= ' ";p rintf (" [%s : %d]: %s\n ", Inet_ntoa (Client.sin_ Addr), Ntohs (Client.sin_port), buf);} Else if (_size == 0) {printf ("client close...\n");} else{}}return 0;}
//udp_client.c
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h># include<arpa/inet.h> #include <netinet/in.h> #include <unistd.h> #include <string.h> #include <errno.h>void usage (Const char *proc) {printf ("%s:[ip][port]\n", proc);} Int main (int argc,char *argv[]) {if (argc != 3) {usage (argv[0]); return 1;} Char *_ip=argv[1];int _port=atoi (argv[2]); Int sock=socket (af_inet,sock_dgram,0); if (sock < 0) {perror ("socket"); exit (1);} Struct sockaddr_in local;local.sin_family=af_inet;local.sin_port=htons (_port); local.sin_addr.s_addr= INET_ADDR (_IP); if (Bind (sock, (struct sockaddr*) &local,sizeof (local) < 0) {perror ("bind"); Exit (2);} int done=0;char buf[1024];struct sockaddr_in client;socklen_t len=sizeof (client); while (!done) {Ssize_t _size=recvfrom (sock,buf,sizeof (BUF) -1,0, (struct sockaddr*) &client,&lEN), if (_size > 0) {buf[_size]= ' ";p rintf (" [%s : %d]: %s\n ", Inet_ntoa (Client.sin_ Addr), Ntohs (Client.sin_port), buf);} Else if (_size == 0) {printf ("client close...\n");} else{}}return 0;}
Operation Result:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/80/wKiom1dDEiHCDdLdAACp5l383O0771.jpg "title=" Udp.jpg "alt=" Wkiom1ddeihcddldaacp5l383o0771.jpg "/>
This article is from the "Flower Open Shore" blog, please be sure to keep this source http://zxtong.blog.51cto.com/10697148/1782311
Socket Programming---UDP protocol