c socket 开发测试
c语言异常
参照他人代码写一个tcp的 socket 开发测试
异常A,在mac osx系统下编译失败,缺库转到debian下。
异常B,include引用文件顺序不对,编译大遍异常
异常C,/usr/include/x86_64-linux-gnu/sys/types.h:34:1: error: unknown type name ‘__u_char’ 文件前注释的问题,删掉注释则通过
服务端
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <errno.h> #include <stdio.h> int accepthandle(int acceptpoint); int main(void) { //服务端socket int socketfid; char buf[1024]; struct sockaddr_in server_add; char * localaddr = "127.0.0.1";// int pid; struct sockaddr_in client_add; server_add.sin_family=AF_INET; server_add.sin_port=htons(29001); server_add.sin_addr.s_addr=htonl(INADDR_ANY); socketfid=socket(AF_INET,SOCK_STREAM,0); printf("socket start \n"); if (socketfid < 0) { printf("socket error \n"); return -1; } printf("bind start \n"); if(bind(socketfid,(struct sockaddr *)&server_add,sizeof(struct sockaddr))<0){ printf("bind error \n"); return -1; } //监听套接子 printf("listen start \n"); if(listen(socketfid,24)<0){ printf("listen error \n"); return -1; } printf("accept start \n"); while(1){ memset(&client_add, 0, sizeof(client_add)); int leng=sizeof(client_add); int acceptresult=accept(socketfid,(struct sockaddr *) &client_add,&(leng)); if (acceptresult < 0) { printf("accept error %d %d is sub\n", acceptresult,pid); //子进程结束了 exit(0); } printf("clent addr%s porit %d\n", inet_ntop(AF_INET, &client_add.sin_addr, buf, sizeof(buf)), ntohs(client_add.sin_port)); pid = fork(); printf("%d\n",pid); if (pid < 0) { printf("pid<0\n"); close(acceptresult); } else if(pid == 0){ printf("pid=0 is sub \n"); //子进程停止监听,去处理接收数据 close(socketfid); //子进程到了这里,执行完接收后,继续执行while 但是因为socketfid 已经关闭,accept 返回异常 -1 直接执行exit(0) 退出了子进程 accepthandle(acceptresult); } else{ //父进程到了这里,执行完这一步,继续while 到accept这里,又被阻塞。如此循环。 //这里错打印不出结果,可能是shell只能展示一个进程的内容。 printf("pid is parent\n"); } } return EXIT_SUCCESS; } int accepthandle(int acceptpoint){ char buf[1024]; int readresult; while(1){ readresult=read(acceptpoint,buf,sizeof(buf)); if(readresult<0){ printf("read error \n"); close(acceptpoint); break; } else if (readresult==0){ printf("client exit \n"); close(acceptpoint); break; } else{ printf("client:%s\n", buf); if (strcmp("exit", buf) == 0) { printf("exit \n"); close(acceptpoint); return 0; } } } return 0; }
client
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <stdio.h> int main(void) { int socketfid; char buf[1024]; struct sockaddr_in server_add; char * localaddr = "127.0.0.1";// server_add.sin_family=AF_INET; server_add.sin_port=htons(29001); server_add.sin_addr.s_addr=htonl(INADDR_ANY); socketfid=socket(AF_INET,SOCK_STREAM,0); while (connect(socketfid,(struct sockaddr*)&server_add,sizeof(server_add))==-1){ printf("Connect Error!\n"); } char *data="hello word"; printf("length %d!\n",sizeof(data)); send( socketfid,data,sizeof(data),0); printf("bind start \n"); close(socketfid); return EXIT_SUCCESS; }