udp_server.c
#include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <ctype.h> #include <string.h> #define SERV_PORT 49130 #define MAXLINE 80 int main() { int sockfd; struct sockaddr_in servaddr, cliaddr; char buf[MAXLINE]; char str[INET_ADDRSTRLEN]; int cliaddr_len; int n, i; if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("socket"); exit(1); } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); // 一定要使用htons函数将端口转换为大端格式 if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { perror("bind"); exit(1); } printf("Accepting connections... "); while (1) { cliaddr_len = sizeof(cliaddr); n = recvfrom(sockfd, buf, MAXLINE, 0, (struct sockaddr *)&cliaddr, &cliaddr_len); if (n == -1) { perror("recvfrom err"); } // print source address's ip,port printf("Received from %s at PORT %d, recvlen is: %d ", inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port), n); for (i = 0; i < n; i++) buf[i] = toupper(buf[i]); n = sendto(sockfd, buf, n, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr)); // printf("send length is: %d ", n); if (n == -1) { perror("sendto err"); } } close(sockfd); return 0; }
udp_client.c
#include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <unistd.h> #define MAXLINE 80 #define SERV_PORT 49130 static const char *SERV_ADDR = "127.0.0.1"; int main() { int sockfd; char buf[MAXLINE]; struct sockaddr_in servaddr; int n, i; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; inet_pton(AF_INET, SERV_ADDR, &servaddr.sin_addr); servaddr.sin_port = htons(SERV_PORT); if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("socket"); exit(1); } while (fgets(buf, MAXLINE, stdin) != NULL) { n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); if (n == -1) { perror("sendto err"); } n = recvfrom(sockfd, buf, MAXLINE, 0, NULL, 0); if (n == -1) { perror("recvfrom err"); } //printf("recv len is: %d ", n); write(STDOUT_FILENO, buf, n); } close(sockfd); return 0; }
先运行Server, 再运行Client,在client输入:
yongdaimi@ubuntu:~/Documents/code$ gcc udp_client.c -o client
yongdaimi@ubuntu:~/Documents/code$ ./client
java
JAVA
j2sdk
J2SDK
jdbc
JDBC
cpp
CPP
jni
JNI
Server:
Accepting connections...
Received from 127.0.0.1 at PORT 45162, recvlen is: 3
Received from 127.0.0.1 at PORT 51366, recvlen is: 3
Received from 127.0.0.1 at PORT 51366, recvlen is: 5
Received from 127.0.0.1 at PORT 51366, recvlen is: 5
Received from 127.0.0.1 at PORT 57741, recvlen is: 5
Received from 127.0.0.1 at PORT 51904, recvlen is: 5
Received from 127.0.0.1 at PORT 51904, recvlen is: 6
Received from 127.0.0.1 at PORT 51904, recvlen is: 5
Received from 127.0.0.1 at PORT 51904, recvlen is: 4
Received from 127.0.0.1 at PORT 51904, recvlen is: 4