UDP Linux编程(客户端&服务器端)
Posted yuanmh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UDP Linux编程(客户端&服务器端)相关的知识,希望对你有一定的参考价值。
- 服务器端
服务器不用绑定地址,他只需要进行绑定相应的监听端口即可。
#include <sys/types.h> #include <sys/socket.h> #include<pthread.h> #include <netinet/in.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include<arpa/inet.h> //#include<cstdlib.h> char* PORT="8899"; //server‘s port char* SOCKET_SERVER_IP="192.168.1.105"; // int main() { printf("Welcome! This is a UDP server, I can only received message from client and reply with same message "); struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(atoi(PORT)); addr.sin_addr.s_addr = htonl(INADDR_ANY); int sock; if ( (sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("socket"); exit(1); } //port bind to server if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); exit(1); } char buff[512]; struct sockaddr_in clientAddr; memset(&clientAddr,0,sizeof(clientAddr)); size_t n; socklen_t len = sizeof(clientAddr); while (1) { n = recvfrom(sock, buff, 511, 0, (struct sockaddr*)&clientAddr, &len); if (n>0) { buff[n] = 0; printf("%s %u says: %s ", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port), buff); n = sendto(sock, buff, n, 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr)); if (n < 0) { perror("sendto"); break; } } else { perror("recv"); break; } } return 0; }
- 客户端
客户端进行(服务器)地址和端口与自己创建的套接字绑定。
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> int CLIENT_PORT=8899; char* CLIENT_IP="192.168.1.105"; int main() { /*1 创建socket*/ int socket_fd = socket(AF_INET, SOCK_DGRAM, 0); if(socket_fd == -1) { perror("udp_socket创建失败!退出客户端系统"); exit(-1); } /*2 准备地址:需要设计一个地址结构体用于存储sock地址结构 addr 的数据*/ struct sockaddr_in addr; //设置family地址格式类型 addr.sin_family = AF_INET; /*绑定地址和端口; * htons是一个转换函数,因为操作系统中的字节排序和网络中字节排序中的字节流顺序是不统一的,所以在进行由系统转化为网络传送。 * htons->host to net short 由主机转换为网络顺序。 */ addr.sin_port = htons(CLIENT_PORT); addr.sin_addr.s_addr = inet_addr(CLIENT_IP); if (addr.sin_addr.s_addr == INADDR_NONE) { printf("Incorrect ip address!"); close(socket_fd); exit(1); } /*3 进行通信*/ char buff[512]; socklen_t len = sizeof(addr); while (1) { scanf("%s",buff); int n; //addr是sockaddr_in类型需要进行强制类型转换 n = sendto(socket_fd, buff, strlen(buff), 0, (struct sockaddr *)&addr, sizeof(addr)); if (n < 0)//发送了n个字节 { perror("sendto"); close(socket_fd); break; } memset(buff,0,sizeof (buff)); n = recvfrom(socket_fd, buff, 512, 0, (struct sockaddr *)&addr, &len); if (n>0) { buff[n] = 0; printf("received:"); puts(buff); } else if (n==0) { printf("server closed "); close(socket_fd); break; } else if (n == -1) { perror("recvfrom"); close(socket_fd); break; } } return 0; }
以上是关于UDP Linux编程(客户端&服务器端)的主要内容,如果未能解决你的问题,请参考以下文章