- UDP server
#include <stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<string.h> #include<netinet/in.h> #include<stdlib.h> void service(int sock){ char buf[128]; struct sockaddr_in client; socklen_t len=sizeof(client); while(1){ ssize_t s = recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&client,&len); if(s > 0){ buf[s]=0; printf("[%s:%d]:%s\n",inet_ntoa(client.sin_addr)); } } } int main(int argc,char* argv[]){ if(argc != 3){ printf("Usage:%s [ip] [port]\n",argv[0]); return 3; } int sock = socket (AF_INET, SOCK_DGRAM, 0); if(sock < 0){ perror("socket"); return 1; } struct sockaddr_in local; local.sin_family = AF_INET; local.sin_port = htons(atoi(argv[2])); local.sin_addr.s_addr = inet_addr(argv[1]); if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0){ perror("bind"); return 2; } service(sock); }
- UDP client
#include <stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdlib.h> #include<string.h> int main(int argc,char* argv[]){ if(argc != 3){ printf("Usage:%s [ip] [port]\n",argv[0]); return 3; } int sock = socket (AF_INET, SOCK_DGRAM, 0); if(sock < 0){ perror("socket"); return 2; } struct sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons(atoi(argv[2])); server.sin_addr.s_addr = inet_addr(argv[1]); char buf[128]; struct sockaddr_in peer; socklen_t len = sizeof(peer); while(1){ buf[0] = 0; ssize_t s = read(0,buf,sizeof(buf)-1); if(s > 0){ buf[s-1] = 0; sendto(sock,buf,strlen(buf),0,(struct sockaddr*)&server,sizeof(server)); ssize_t _s = recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&peer,&len); if(_s > 0){ buf[_s] = 0; printf("server echo# %s\n",buf); } } } }
- Introduction to Correlation Functions
1.socket function
Function: used to create a socket capable of network communication.
Related parameters:
domain: Specifies the protocol family of the communication protocol used by the application. Usually PF_INET/AF_INET, which means the Internet protocol suite (TCP/IP protocol suite).
type: Specifies the type of socket to create. The stream socket type is SOCK_STREAM; the datagram socket type is SOCK_DGEAM.
Protocol: Specifies the communication protocol used by the application, usually 0.
Return value: The descriptor of the newly created socket is returned on success; -1 is returned on failure.
2. bind function
Function: When a Socket is created, there is a default IP address and a default port number in the socket data structure. A service program must call the bind function to bind it with an IP address and a specific port number.
Related parameters:
sockfd: Specifies the socket descriptor to be bound.
addr: Specify a sockaddr structure. The structure definition has been introduced in the blog of network programming socket (2), and will not be repeated here.
addrlen: The size of the structure.
Return value: 0 for success; -1 for failure.
3. recvfrom function
Function: Receive a datagram and save the source address.
Related parameters:
sockfd: A description word representing a connected socket.
buf: Receive data buffer.
len: Buffer length.
flags: call operation mode, usually 0.
src_addr: Points to the buffer containing the source address.
addrlen: Points to the length value of the buffer.
Return value: Returns the number of bytes read on success; returns -1 on failure.
4.sendto function
Function: Send data to the other host from the specified socket.
Related parameters:
sockfd: Index the socket from which data will be sent.
buf: Points to the buffer where the data will be sent.
len: The buffer length of the data to be sent.
flags: call operation mode, usually 0.
dest_addr: Points to the area where the address of the receiver is stored.
addrlen: Points to the length value of the area where the address of the receiver is stored.
Return value: Returns the actual number of characters sent on success; returns -1 on failure.