recv和send函数:
#include<sys/socket.h>
ssize_t recv(int sockfd, void *buff, size_t nbytes, int flags);
ssize_t recv(int sockfd, const void *buff, size_t nbytes, int flags);
flags的值中 MSG_OOB和MSG_PEEK比较重要。
read和recv函数的区别在于:
read函数读取缓冲区的数据之后,会将缓冲区的数据删除,而recv不会删除缓冲区的数据。
因此,可以将falgs设置为MSG_PEEK,在此模式下,先查看发送过来的字符的个数,然后用read函数读取数据。
例子服务端代码如下:
#include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> #include<sys/types.h> #include<sys/socket.h> #include<sys/un.h> #include<sys/wait.h> //*进程用的头文件*/ #include<netinet/in.h> #include<arpa/inet.h> #include<unistd.h> #define MAXLINE 1024 //通信内容的最大长度 ssize_t readn(int fd, void *buf, size_t count) { ssize_t nleft=count; ssize_t nread; char *charbuf=(char*) buf; while(nleft>0) { nread=read(fd,charbuf,nleft); if(nread<0) { if(errno==EINTR) continue; return -1; } else if(nread==0) return count-nleft; charbuf +=nread; nleft=count-nread; } return count; } ssize_t writen(int fd, const void *buf, size_t count) { ssize_t nleft=count; ssize_t nwrite; char *charbuf=(char*) buf; while(nleft>0) { nwrite=write(fd,charbuf,nleft); if(nwrite<0) { if(errno==EINTR) continue; return -1; } else if(nwrite==0) return count-nleft; charbuf +=nwrite; nleft=count-nwrite; } return count; } ssize_t recv_peek(int sockfd, void *buf, size_t len) { int ret; while(1) { ret=recv(sockfd,buf,len,MSG_PEEK); if(ret==-1&& errno==EINTR) continue; return ret; } } ssize_t readline(int sockfd, void *buf, size_t len) { ssize_t nleft=len,nread; int ret; char* bufchar=buf; while(1) { ret=recv_peek(sockfd,bufchar,len); if(ret<0||ret==0) return ret; nread=ret; int i; for(i=0;i<nread;i++) { if(bufchar[i]==‘\n‘) { ret=readn(sockfd,bufchar,i+1); if(ret!=i+1) exit(EXIT_FAILURE); return ret; } } if(nread>nleft) exit(EXIT_FAILURE); nleft-=nread; ret=readn(sockfd,bufchar,nread); if(ret!=nread) exit(EXIT_FAILURE); bufchar+=nread; } return -1; } int main() { int sock_fd,new_fd;//sock_fd用于监听,new_fd用于连接 struct sockaddr_in srv_addr;//服务器的地址信息 struct sockaddr_in client_addr;//客户机的地址信息 int size; //地址结构数据的长度 pid_t pid; //子进程id ssize_t n,ret; char buf[MAXLINE]; //用于存放通信的内容 char sendbuf[1024],recvbuf[1024]; memset(sendbuf,0,sizeof(sendbuf)); memset(recvbuf,0,sizeof(recvbuf)); /*创建套接字*/ sock_fd=socket(AF_INET,SOCK_STREAM,0);//采用IPv4协议 if(sock_fd==-1) { perror("creat socket failed"); exit(1); } /*服务器地址参数*/ srv_addr.sin_family=AF_INET; srv_addr.sin_port=htons(3490); srv_addr.sin_addr.s_addr=htonl(INADDR_ANY); bzero(&srv_addr.sin_zero,sizeof(struct sockaddr_in));//bzero位清零函数,将sin_zero清零,sin_zero为填充字段,必须全部为零 int on=1; //表示开启reuseaddr if(setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))<0) //打开地址、端口重用 perror("setsockopt"); /*绑定地址和端口*/ if(bind(sock_fd,(struct sockaddr*)&srv_addr,sizeof(struct sockaddr))==-1) { perror("bind failed"); exit(1); } /*连接到服务器 if(connect(sock_fd,(struct sockaddr*)&srv_addr,sizeof(sock_fd))==-1) { perror("bind failed"); exit(1); }*/ /*设置监听模式,等待客户机的监听*/ if((listen(sock_fd,5))==-1) { perror("listen failed"); exit(1); } /*接受连接,采用非阻塞是的模式调用accep*/ //while(1) //{ size=sizeof(struct sockaddr_in); new_fd=accept(sock_fd,(struct sockaddr*)&client_addr,&size); if(new_fd==-1) { perror("accept failed"); //continue;//restart accept when EINTR } printf("server:got connection from IP= %s prot= %d \n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));//连接成功,打印客户机IP地址和端口号 /*char *inet_nota(struct sockaddr_in in); 头文件: arpa/inet.h Winsock2.h 参数: 一个网络上的IP地址 返回值: 如果正确,返回一个字符指针,指向一块存储着点分格式IP地址的静态缓冲区(同一线程内共享此内存);错误,返回NULL。 uint31_t ntohs(uint32_t net32bitvalue); 头文件: #include<netinet/in.h> 把net32bitvalue有网络字节序转换为主机字节序。 */ if(send(new_fd,"Hello client,I am 192.168.229.125!\n",50,0)==-1) //192.168.229.125为子进程IP,可更改 perror("send failed"); pid=fork(); //父进程建立套接字的连接之后,创建子进程用于通信 if(pid<0) perror("fork error\n"); if(!pid)//创建新的子进程,用于发送数据 { // close(sock_fd);//子进程不需要监听,所以子进程关闭监 while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL) { writen(new_fd,sendbuf,strlen(sendbuf)); memset(sendbuf,0,sizeof(sendbuf)); //清空,以免和下一次混淆 // exit(EXIT_SUCCESS); } exit(EXIT_SUCCESS); } //close(new_fd);//父进程不需要连接,所以关闭连接套接字 else { while(1) { memset(recvbuf,0,sizeof(recvbuf)); ret=readline(new_fd,recvbuf,1024); if(ret<0) perror("read from client error"); else if(ret==0) { printf("peer closed\n"); break; } fputs(recvbuf,stdout); } exit(EXIT_SUCCESS); } //while(waitpid(-1,NULL,WNOHANG)>0);//等待子进程结束,进行新的连接 //} return 0; }
客户端代码
#include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> #include<sys/types.h> #include<sys/socket.h> #include<sys/un.h> #include<sys/wait.h> //*进程用的头文件*/ #include<netinet/in.h> #include<arpa/inet.h> #define MAXBYTEMUN 1024 ssize_t readn(int fd, void *buf, size_t count) { ssize_t nleft=count; ssize_t nread; char *charbuf=(char*) buf; while(nleft>0) { nread=read(fd,charbuf,nleft); if(nread<0) { if(errno==EINTR) continue; return -1; } else if(nread==0) return count-nleft; charbuf +=nread; nleft=count-nread; } return count; } ssize_t writen(int fd, const void *buf, size_t count) { ssize_t nleft=count; ssize_t nwrite; char *charbuf=(char*) buf; while(nleft>0) { nwrite=write(fd,charbuf,nleft); if(nwrite<0) { if(errno==EINTR) continue; return -1; } else if(nwrite==0) return count-nleft; charbuf +=nwrite; nleft=count-nwrite; } return count; } ssize_t recv_peek(int sockfd, void *buf, size_t len) { int ret; while(1) { ret=recv(sockfd,buf,len,MSG_PEEK); if(ret==-1&& errno==EINTR) continue; return ret; } } ssize_t readline(int sockfd, void *buf, size_t len) { ssize_t nleft=len,nread; int ret; char* bufchar=buf; while(1) { ret=recv_peek(sockfd,bufchar,len); if(ret<0||ret==0) return ret; nread=ret; int i; for(i=0;i<nread;i++) { if(bufchar[i]==‘\n‘) { ret=readn(sockfd,bufchar,i+1); if(ret!=i+1) exit(EXIT_FAILURE); return ret; } } if(nread>nleft) exit(EXIT_FAILURE); nleft-=nread; ret=readn(sockfd,bufchar,nread); if(ret!=nread) exit(EXIT_FAILURE); bufchar+=nread; } return -1; } int main(int argc,char *argv[]) { int sock_fd,numbytes; char buf[MAXBYTEMUN]; struct hostent *he; struct sockaddr_in client_addr;//客户机的地址信息 ssize_t n,ret; char recvbuf[1024]={‘0‘},sendbuf[1024]={‘0‘}; if(argc!=2) { fprintf(stderr,"usage: client IPAddress\n"); //执行客户端程序时,输入客户端程序名称和其IP地址 exit(1); } /*创建套接字*/ sock_fd=socket(AF_INET,SOCK_STREAM,0);//采用IPv4协议 if(sock_fd==-1) { perror("creat socket failed"); exit(1); } /*服务器地址参数*/ client_addr.sin_family=AF_INET; client_addr.sin_port=htons(3490); client_addr.sin_addr.s_addr=inet_addr(argv[1]); bzero(&client_addr.sin_zero,sizeof(struct sockaddr_in));//bzero位清零函数,将sin_zero清零,sin_zero为填充字段,必须全部为零 /*连接到服务器*/ if(connect(sock_fd,(struct sockaddr*)&client_addr,sizeof(struct sockaddr))==-1) { perror("connect failed"); exit(1); } if((numbytes=recv(sock_fd,buf,MAXBYTEMUN,0))==-1) { perror("receive failed"); exit(1); } buf[numbytes]=‘\0‘;//在字符串末尾加上\0,否则字符串无法输出 printf("Received: %s\n",buf); pid_t pid; pid=fork(); if(!pid)//创建新的子进程,用于接收数据 { while(1) { memset(recvbuf,0,sizeof(recvbuf)); ret=readline(sock_fd,recvbuf,1024); if(ret<0) perror("read from server error"); else if(ret==0) { printf("peer closed\n"); break; } fputs(recvbuf,stdout); } close(sock_fd); } else //f=父进程用于发送数据 { //close(sock_fd);//父进程不需要连接,所以关闭连接套接字 while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL) { writen(sock_fd,sendbuf,strlen(sendbuf)); memset(sendbuf,0,sizeof(sendbuf)); //清空,以免和下一次混淆 } close(sock_fd); } return 0; }
时间: 2024-11-10 02:05:04