赞
踩
网络编程——udp通信
客户端:
#include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <pthread.h> typedef struct message{ char mtext[1024]; int mtype; }Message; void do_user(void *arg) { int ret; char buf[1024]; int sockfd = *(int *)arg; struct sockaddr_in peer_addr; int peer_addr_len = sizeof(peer_addr); while (1) { printf("Wait to recive...\n"); ret = recvfrom(sockfd, buf, sizeof(buf) - 1, 0, (struct sockaddr *)&peer_addr, &peer_addr_len); if (ret < 0) { perror("Fail to recvfrom..."); } buf[ret] = '\0'; printf("收到来自<%s><%d>%d字节的消息 > %s\n", inet_ntoa(peer_addr.sin_addr), ntohs(peer_addr.sin_port), ret, buf); bzero(&peer_addr, sizeof(peer_addr)); } } char *message(char *msg, int msg_len) { bzero(msg, sizeof(msg_len)); printf("input msg >"); fgets(msg, msg_len, stdin); msg[strlen(msg) - 1] = '\0'; return msg; } int main(int argc, char const *argv[]) { int ret; int sockfd; pthread_t pid; char msg[1024]; int msg_len = sizeof(msg); struct sockaddr_in self_addr; struct sockaddr_in dest_addr; sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { perror("fail to create sockfd"); exit(-1); } self_addr.sin_family = AF_INET; self_addr.sin_port = htons(8888); self_addr.sin_addr.s_addr = inet_addr("192.168.2.193"); ret = bind(sockfd, (const struct sockaddr *)&self_addr, sizeof(self_addr)); if (ret < 0) { perror("fail to bond"); exit(-1); } dest_addr.sin_family = AF_INET; dest_addr.sin_addr.s_addr = inet_addr("192.168.2.193"); dest_addr.sin_port = htons(7777); ret = pthread_create(&pid, NULL, (void *)do_user, &sockfd); while (1) { getchar(); message(msg, msg_len); printf("msg > %s,len > %ld\n", msg, strlen(msg)); ret = sendto(sockfd, msg, strlen(msg), 0, (const struct sockaddr *)&dest_addr, sizeof(dest_addr)); if (ret < 0) { perror("fail to sendto.."); } } return 0; }
服务器端:
#include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <pthread.h> char *message(char *msg, int msg_len) { bzero(msg, sizeof(msg_len)); printf("input msg >"); fgets(msg, msg_len, stdin); msg[strlen(msg) - 1] = '\0'; return msg; } void do_user(void *arg) { int ret; char msg[1024]; int msg_len = sizeof(msg); int sockfd = *(int *)arg; struct sockaddr_in dest_addr; dest_addr.sin_family = AF_INET; dest_addr.sin_addr.s_addr = inet_addr("192.168.2.193"); dest_addr.sin_port = htons(8888); //printf("msg > %s,len > %ld\n", msg, strlen(msg)); while (1) { getchar(); message(msg, msg_len); ret = sendto(sockfd, msg, strlen(msg), 0, (const struct sockaddr *)&dest_addr, sizeof(dest_addr)); if (ret < 0) { perror("fail to sendto.."); } } } int main(int argc, char const *argv[]) { int ret; int sockfd; char buf[1024]; pthread_t pid; struct sockaddr_in self_addr; struct sockaddr_in peer_addr; int peer_addr_len = sizeof(peer_addr); sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { perror("fail to create sockfd"); exit(-1); } self_addr.sin_family = AF_INET; self_addr.sin_port = htons(7777); self_addr.sin_addr.s_addr = inet_addr("192.168.2.193"); ret = bind(sockfd, (const struct sockaddr *)&self_addr, sizeof(self_addr)); if (ret < 0) { perror("fail to bind"); exit(-1); } ret = pthread_create(&pid, NULL, (void *)do_user, &sockfd); while (1) { printf("Wait to recive...\n"); ret = recvfrom(sockfd, buf, sizeof(buf) - 1, 0, (struct sockaddr *)&peer_addr, &peer_addr_len); if (ret < 0) { perror("Fail to recvfrom..."); } buf[ret] = '\0'; printf("收到来自<%s><%d>%d字节的消息 > %s\n", inet_ntoa(peer_addr.sin_addr), ntohs(peer_addr.sin_port), ret, buf); bzero(&peer_addr, sizeof(peer_addr)); } return 0; }
结果:
服务器与客户端可双向通信
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。