//服務器端代碼
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<malloc.h>
#define MAXBUF 1024
int main()
{
int pid;
int sockfd,new_fd;
socklen_t len;
struct sockaddr_in my_addr, their_addr;
unsigned int myport,listnum;
char buf[MAXBUF+1];
myport=7575;
listnum=5;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socker");
exit(EXIT_FAILURE);
}
memset(&my_addr,0,sizeof(my_addr));
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(myport);
my_addr.sin_addr.s_addr=inet_addr("192.168.1.106");
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1){
perror("bind");
exit(EXIT_FAILURE);
}
if(listen(sockfd,listnum)==-1){
perror("listen");
exit(EXIT_FAILURE);
}
printf("wait for connect\n");
len=sizeof(struct sockaddr);
if(new_fd=accept(sockfd,(struct sockaddr *)&their_addr,&len)==-1){
perror("accept");
exit(EXIT_FAILURE);
}
else
printf("server:got conection!");
if((pid=fork())==-1){
perror("fork");
exit(EXIT_FAILURE);
}
else if(pid==0){
while(1){
memset(buf,0,sizeof(buf));
fgets(buf,MAXBUF,stdin);
if(!strncasecmp(buf,"quit",4)){
printf("i will close the connection");
break;
}
len=send(new_fd,buf,strlen(buf)-1,0);
if(len<0){
printf("message send failed!");
break;
}
}
}
else{
while(1){
bzero(buf,MAXBUF+1);
len=recv(new_fd,buf,MAXBUF,0);
if(len>0)
printf("message recv successful:%s \n",buf);
else if(len<0){
printf("recv failed!");
break;
}
else{
printf("the other one close quit \n");
break;
}
}
}
close(new_fd);
close(sockfd);
return 0;
}
//客戶端的代碼
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<errno.h>
#include<netinet/in.h>
#include<resolv.h>
#include<unistd.h>
#include<arpa/inet.h>
#define MAXBUF 1024
int main()
{
int sockfd,len,destport=7575;
struct sockaddr_in dest;
char buf[MAXBUF+1];
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){
perror("socket");
exit(errno);
}
printf("socket created\n");
memset(&dest,0,sizeof(dest));
dest.sin_family=AF_INET;
dest.sin_port=htons(destport);
dest.sin_addr.s_addr=inet_addr("192.168.1.106");
if((connect(sockfd,(struct sockaddr *)&dest,sizeof(dest)))==-1){
perror("connect");
exit(errno);
}
printf("server connected!\n");
pid_t pid;
if((pid=fork())==-1){
perror("fork");
exit(errno);
}
else if(pid==0){
while(1){
memset(buf,0,sizeof(buf));
len=recv(sockfd,buf,MAXBUF,0);
if(len>0)
printf("recv successful:%s \n",buf);
else if(len<0){
perror("recv");
exit(errno);
}
else{
printf("the other one close ,quit\n");
break;
}
}
}
else{
while(1){
memset(buf,0,sizeof(buf));
printf("pls send message to send:");
fgets(buf,MAXBUF,stdin);
if(!strncasecmp(buf,"quit",4)){
printf("i will quit\n");
break;
}
len=send(sockfd,buf,strlen(buf)-1,0);
if(len<0){
perror("send");
break;
}
}
}
close(sockfd);
return 0;
}
運行客戶端和服務器端後,服務器端:
客戶端:
不知道這是什麼情況呢,==
有兩個問題:
socklen_t
是無符號整型,你用socklen_t len
這個變量去存儲recv的返回值,然後又判斷它是否大於0,結果就是:返回值為-1,但判斷結果會認為這是個正數,所以accept、recv看起來都成功了,你的程序就到死循環裡去了。其實你accept的時候就失敗了,因為之前沒調用listen函數。