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[];
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();
server_add.sin_addr.s_addr=htonl(INADDR_ANY); socketfid=socket(AF_INET,SOCK_STREAM,);
printf("socket start \n");
if (socketfid < ) {
printf("socket error \n");
return -;
}
printf("bind start \n");
if(bind(socketfid,(struct sockaddr *)&server_add,sizeof(struct sockaddr))<){
printf("bind error \n");
return -;
}
//监听套接子
printf("listen start \n");
if(listen(socketfid,)<){
printf("listen error \n");
return -;
}
printf("accept start \n");
while(){
memset(&client_add, , sizeof(client_add));
int leng=sizeof(client_add);
int acceptresult=accept(socketfid,(struct sockaddr *) &client_add,&(leng)); if (acceptresult < ) {
printf("accept error %d %d is sub\n", acceptresult,pid);
//子进程结束了
exit();
}
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 < ) {
printf("pid<0\n");
close(acceptresult);
}
else if(pid == ){
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[];
int readresult;
while(){
readresult=read(acceptpoint,buf,sizeof(buf));
if(readresult<){
printf("read error \n");
close(acceptpoint);
break;
}
else if (readresult==){
printf("client exit \n");
close(acceptpoint);
break;
}
else{
printf("client:%s\n", buf);
if (strcmp("exit", buf) == ) {
printf("exit \n");
close(acceptpoint);
return ;
} } }
return ;
}

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[];
struct sockaddr_in server_add;
char * localaddr = "127.0.0.1";//
server_add.sin_family=AF_INET;
server_add.sin_port=htons();
server_add.sin_addr.s_addr=htonl(INADDR_ANY);
socketfid=socket(AF_INET,SOCK_STREAM,);
while (connect(socketfid,(struct sockaddr*)&server_add,sizeof(server_add))==-){
printf("Connect Error!\n");
}
char *data="hello word";
printf("length %d!\n",sizeof(data));
send( socketfid,data,sizeof(data),);
printf("bind start \n"); close(socketfid); return EXIT_SUCCESS;
}
05-11 20:47