当前位置: 技术问答>linux和unix
急!socket编程出现了问题!
来源: 互联网 发布时间:2016-03-16
本文导语: 运行时服务器端显示 read error : Transport endpoint is not connected 服务器端程序: #include #include #include #include #include #include #include #include #define MYPORT 4000 #define BACKLOG 10 int main(int argc, char **argv...
运行时服务器端显示
read error
: Transport endpoint is not connected
服务器端程序:
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 4000
#define BACKLOG 10
int main(int argc, char **argv)
{
char buffer[1024];
int sockfd,new_fd ;
int sin_size;
struct sockaddr_in my_addr ;
struct sockaddr_in their_addr ;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("create sockn");
return -1;
}
else
{
my_addr.sin_family= AF_INET ;
my_addr.sin_port= htons(MYPORT);
my_addr.sin_addr.s_addr=inet_addr("192.168.1.5") ;
bzero(&(my_addr.sin_zero),8);
if (bind (sockfd, (struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
printf("bind errorn");
return -1 ;
}
listen(sockfd, BACKLOG);
while (1)
{
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if(new_fd == -1)
{
printf("accept errorn");
}
char *msg = "Hello! World! ";
int len, bytes_sent;
len = strlen(msg);
bytes_sent = send(new_fd, msg, len, 0);
printf("okn");
int nbytes;
if((nbytes=read(sockfd,buffer,1024))==-1)
{
perror("read errorn");
}
buffer[nbytes]='';
printf("I have received:%sn",buffer);
close(new_fd);
}
close(sockfd);
}
}
客户端程序:
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 4000
#define BACKLOG 10
int main(int argc, char **argv)
{
int sockfd;
char buffer[1024];
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber,nbytes;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("socket errorn");
return -1 ;
}
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(MYPORT);
server_addr.sin_addr.s_addr= inet_addr("192.168.1.5") ;
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
perror("connect errorn");
return -1;
}
if((nbytes=read(sockfd,buffer,1024))==-1)
{
printf("read errorn");
return -1 ;
}
buffer[nbytes]='';
printf("I have received:%sn",buffer);
char *msg = "Hello! World! ";
int len, bytes_sent;
len = strlen(msg);
bytes_sent = send(sockfd, msg, len, 0);
printf("okn");
close(sockfd);
}
read error
: Transport endpoint is not connected
服务器端程序:
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 4000
#define BACKLOG 10
int main(int argc, char **argv)
{
char buffer[1024];
int sockfd,new_fd ;
int sin_size;
struct sockaddr_in my_addr ;
struct sockaddr_in their_addr ;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("create sockn");
return -1;
}
else
{
my_addr.sin_family= AF_INET ;
my_addr.sin_port= htons(MYPORT);
my_addr.sin_addr.s_addr=inet_addr("192.168.1.5") ;
bzero(&(my_addr.sin_zero),8);
if (bind (sockfd, (struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
printf("bind errorn");
return -1 ;
}
listen(sockfd, BACKLOG);
while (1)
{
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if(new_fd == -1)
{
printf("accept errorn");
}
char *msg = "Hello! World! ";
int len, bytes_sent;
len = strlen(msg);
bytes_sent = send(new_fd, msg, len, 0);
printf("okn");
int nbytes;
if((nbytes=read(sockfd,buffer,1024))==-1)
{
perror("read errorn");
}
buffer[nbytes]='';
printf("I have received:%sn",buffer);
close(new_fd);
}
close(sockfd);
}
}
客户端程序:
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 4000
#define BACKLOG 10
int main(int argc, char **argv)
{
int sockfd;
char buffer[1024];
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber,nbytes;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("socket errorn");
return -1 ;
}
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(MYPORT);
server_addr.sin_addr.s_addr= inet_addr("192.168.1.5") ;
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
perror("connect errorn");
return -1;
}
if((nbytes=read(sockfd,buffer,1024))==-1)
{
printf("read errorn");
return -1 ;
}
buffer[nbytes]='';
printf("I have received:%sn",buffer);
char *msg = "Hello! World! ";
int len, bytes_sent;
len = strlen(msg);
bytes_sent = send(sockfd, msg, len, 0);
printf("okn");
close(sockfd);
}
|
服务器端去读的时候,客户端已经关闭连接了.
bytes_sent = send(sockfd, msg, len, 0);
printf("okn");
close(sockfd);
改成
bytes_sent = send(sockfd, msg, len, 0);
sleep(1);
printf("okn");
close(sockfd);