Test code for UNIX network programming-UDP Communication

Source: Internet
Author: User

Running Environment: centos 6.3

(UNIX network programming test code)

Integrating the client, server, and broadcast message server into a piece of code, I personally feel pretty good.

There are not many comments in it, but it should be hard to understand and easy to understand.

You can directly paste a piece of code and use GCC to compile and run it. It will find that the effect is good.

#include <stdio.h>#include <sys/types.h>          /* See NOTES */#include <sys/socket.h>#include <netinet/in.h>#include <string.h>#include <errno.h>#include <stdlib.h>#include <arpa/inet.h>#include <unistd.h>typedef struct sockaddr_in sockaddr_in;typedef struct sockaddr sockaddr;int main(int argc, char ** argv) {sockaddr_in srv, cli;int fd, sfd, is_server;if (argc < 3) {printf("please put the arguments ,like ./a.out addr_ip  addr_port [server/client/broad]");return -1;}if (argc >= 4) {if (strcmp("server", argv[3]) == 0) {is_server = 1;printf("i am server \n");} else if (strcmp("client", argv[3]) == 0) {is_server = 0;printf("i am client \n");} else if(strcmp("broad",argv[3]) == 0){is_server = 2;printf("i want broad msg ,broad ip:X.X.X.255 \n");} else {printf("arguments error\n");return -1;}} else {//defaultis_server = -1 ;printf("i could recv and send msg  ,muti process\n");}memset(&srv, 0, sizeof(srv));srv.sin_port = htons(atoi(argv[2]));srv.sin_family = AF_INET;if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {perror("server socket error");}if(is_server != -1){fd = 1-is_server ;}else{fd = fork();if (fd < 0) {perror("fork error");return -1;}}if (0 == fd) { //service is_server == 1char recvbuf[1024];char ip[50];socklen_t len;int rnu = 0;srv.sin_addr.s_addr = INADDR_ANY ;int bfd;if ((bfd = bind(sfd, (sockaddr*) &srv, sizeof(sockaddr))) < 0) {perror("bind error");}while (1) {memset(recvbuf, 0, sizeof(recvbuf));len = sizeof(sockaddr);if((rnu = recvfrom(sfd, recvbuf, sizeof(recvbuf), 0, (sockaddr*) &cli,&len))<0) {printf("recvfrom error");} else {printf("from:%s,recv:%s",inet_ntop(AF_INET, &cli.sin_addr, ip, sizeof(ip)),recvbuf);if (is_server!=-1 && sendto(sfd, recvbuf, strlen(recvbuf), 0, (sockaddr*) &srv, len) < 0) {perror("sendto error");}//printf("send:%s\n",inet_ntop(AF_INET, &srv.sin_addr, ip, sizeof(ip)));sleep(2);}}close(sfd);}else if(-1 == fd){//broadcastchar recvbuf[1024];char ip[50];socklen_t len;int rnu = 0;srv.sin_addr.s_addr = INADDR_ANY ;int bfd;if ((bfd = bind(sfd, (sockaddr*) &srv, sizeof(sockaddr))) < 0) {perror("bind error");}while (1) {memset(recvbuf, 0, sizeof(recvbuf));len = sizeof(sockaddr);int on_broadcast = 1;int rs = setsockopt(sfd, SOL_SOCKET, SO_BROADCAST, &on_broadcast,sizeof(on_broadcast));if ((rs = inet_pton(AF_INET, "10.33.28.255", &srv.sin_addr)) <= 0) {perror("inet_pton error:");}strcpy(recvbuf, "this is broad\n");if (sendto(sfd, recvbuf, strlen(recvbuf), 0, (sockaddr*) &srv, len)< 0) {perror("sendto error");}printf("send:%s\n",inet_ntop(AF_INET, &srv.sin_addr, ip, sizeof(ip)));sleep(2);}close(sfd);}else {char sendbuf[1024], *p = sendbuf;ssize_t bs;char ip[50];socklen_t len = sizeof(sockaddr);inet_aton(argv[1], &srv.sin_addr);while (1) {memset(sendbuf, 0, sizeof(sendbuf));bs = sizeof(sendbuf);bs = getline(&p, &bs, stdin);bs = sendto(sfd, sendbuf, bs, 0, (sockaddr*) &srv, len);printf("sendto:%s", sendbuf);if(is_server!=-1 && (bs = recvfrom(sfd, sendbuf, sizeof(sendbuf), 0, (sockaddr*) &cli,&len))>0){printf("from:%s,recv:%s",inet_ntop(AF_INET, &cli.sin_addr, ip, sizeof(ip)),sendbuf);}}close(sfd);}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.