当前位置: 技术问答>linux和unix
用socket去connect后,recv和send都出错
来源: 互联网 发布时间:2016-02-17
本文导语: 我用Vmware装了个linux,用socket去connect我windows的一个通信server程序 connect没有报错,但recv和send都出错,说Transport endpoint is not connected之类的 但从代码可以看出,connect没有报错 代码是这样的,非常简单,希望高手指点. #includ...
我用Vmware装了个linux,用socket去connect我windows的一个通信server程序
connect没有报错,但recv和send都出错,说Transport endpoint is not connected之类的
但从代码可以看出,connect没有报错
代码是这样的,非常简单,希望高手指点.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 56790
#define MAXDATASIZE 1024
int sockfd;
pthread_t recthread;
void recmessage()
{
while(1)
{
int numbytes;
char buf[MAXDATASIZE];
if((numbytes=recv(sockfd,buf,MAXDATASIZE,0))==-1)
{
perror("recv......");
exit(1);
}
buf[numbytes]= ' ';
if(strcmp(buf,"exit")==0)
{
printf("Server is closed.n");
close(sockfd);
exit(1);
}
printf("Server :%s.n",buf);
}
}
int main()
{
struct hostent *he;
struct sockaddr_in their_addr;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
their_addr.sin_family=AF_INET;
bzero(&their_addr,sizeof(their_addr));
their_addr.sin_port=htons(PORT);
their_addr.sin_addr.s_addr=inet_addr("192.168.1.77");
bzero(&(their_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr*)&their_addr,sizeof(struct sockaddr))==-1)
{
perror("connect");
exit(1);
}
sleep(1);
printf("socket id is %d",sockfd);
if((pthread_create(&recthread,NULL,(void *)recmessage,NULL))!=0)
{
printf("Create thread error!n");
exit(1);
}
while(1)
{
char msg[MAXDATASIZE];
scanf("%s",msg);
if(send(sockfd,msg,strlen(msg),0)==-1)
{
perror("send");
close(sockfd);
exit(1);
}
if(strcmp(msg,"exit")==0)
{
printf("Byebye!n");
close(sockfd);
exit(1);
}
}
return 0;
}
connect没有报错,但recv和send都出错,说Transport endpoint is not connected之类的
但从代码可以看出,connect没有报错
代码是这样的,非常简单,希望高手指点.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 56790
#define MAXDATASIZE 1024
int sockfd;
pthread_t recthread;
void recmessage()
{
while(1)
{
int numbytes;
char buf[MAXDATASIZE];
if((numbytes=recv(sockfd,buf,MAXDATASIZE,0))==-1)
{
perror("recv......");
exit(1);
}
buf[numbytes]= ' ';
if(strcmp(buf,"exit")==0)
{
printf("Server is closed.n");
close(sockfd);
exit(1);
}
printf("Server :%s.n",buf);
}
}
int main()
{
struct hostent *he;
struct sockaddr_in their_addr;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
their_addr.sin_family=AF_INET;
bzero(&their_addr,sizeof(their_addr));
their_addr.sin_port=htons(PORT);
their_addr.sin_addr.s_addr=inet_addr("192.168.1.77");
bzero(&(their_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr*)&their_addr,sizeof(struct sockaddr))==-1)
{
perror("connect");
exit(1);
}
sleep(1);
printf("socket id is %d",sockfd);
if((pthread_create(&recthread,NULL,(void *)recmessage,NULL))!=0)
{
printf("Create thread error!n");
exit(1);
}
while(1)
{
char msg[MAXDATASIZE];
scanf("%s",msg);
if(send(sockfd,msg,strlen(msg),0)==-1)
{
perror("send");
close(sockfd);
exit(1);
}
if(strcmp(msg,"exit")==0)
{
printf("Byebye!n");
close(sockfd);
exit(1);
}
}
return 0;
}
|
their_addr.sin_family=AF_INET;
bzero(&their_addr,sizeof(their_addr));
their_addr.sin_port=htons(PORT);
their_addr.sin_addr.s_addr=inet_addr( "192.168.1.77 ");
bzero(&(their_addr.sin_zero),8);
=============
试试改为:
bzero(&their_addr,sizeof(their_addr));
their_addr.sin_family=AF_INET;
their_addr.sin_port=htons(PORT);
their_addr.sin_addr.s_addr=inet_addr( "192.168.1.77");
bzero(&their_addr,sizeof(their_addr));
their_addr.sin_port=htons(PORT);
their_addr.sin_addr.s_addr=inet_addr( "192.168.1.77 ");
bzero(&(their_addr.sin_zero),8);
=============
试试改为:
bzero(&their_addr,sizeof(their_addr));
their_addr.sin_family=AF_INET;
their_addr.sin_port=htons(PORT);
their_addr.sin_addr.s_addr=inet_addr( "192.168.1.77");