[置顶] UNIX 网络编程测试代码 -- UDP通信

运行环境:centos 6.3  

说明 :UNIX 网络编程测试代码

将客户端、服务端、以及广播消息的服务端都集成一段代码里面,个人感觉还不错呵呵呵

里面的注释不多,但是应该不难读懂的,还算比较容易

可以把一面代码直接贴下来,用gcc  编译跑一下,会发现在效果还不错吐舌头

#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 {//default
		is_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 == 1
		char 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){//broadcast
		char 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);
	}

}











你可能感兴趣的:([置顶] UNIX 网络编程测试代码 -- UDP通信)