当前位置: 技术问答>linux和unix
急!socket编程错误!
来源: 互联网 发布时间:2016-03-17
本文导语: 刚编了一个服务器和客户端的程序 功能是客户端向服务器端发送消息,服务器端接受并显示 可是当第一次向服务器端发送消息时,服务器端能正常显示 但是接下来再向服务器端发送时,服务器端没有响应! server.c如...
刚编了一个服务器和客户端的程序
功能是客户端向服务器端发送消息,服务器端接受并显示
可是当第一次向服务器端发送消息时,服务器端能正常显示
但是接下来再向服务器端发送时,服务器端没有响应!
server.c如下:
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 7777
#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);
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 client! ";
int len, bytes_sent;
len = strlen(msg);
bytes_sent = send(new_fd, msg, len, 0);
printf("okn");
int nbytes;
//bzero(buffer,sizeof(buffer));
while(1)
{
if((nbytes=read(new_fd,buffer,1024))==-1)
{
perror("read errorn");
exit(1);
}
buffer[nbytes]='';
printf("I have received:%sn",buffer);
/*bzero(buffer,sizeof(buffer));
scanf("%s",buffer);
int len, bytes_sent;
bytes_sent = write(sockfd, buffer,strlen(buffer));
printf("okn");
*/
//close(new_fd);
}
//close(sockfd);
}
}
client.c 如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 7777
#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") ;
int nsock;
nsock=connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr));
if(nsock==-1)
{
perror("connect errorn");
return -1;
}
while(1)
{
/* if((nbytes=read(sockfd,buffer,1024))==-1)
{
printf("read errorn");
return -1 ;
}
buffer[nbytes]='';
printf("I have received:%sn",buffer);
*/
bzero(buffer,sizeof(buffer));
scanf("%s",buffer);
int len, bytes_sent;
bytes_sent = write(sockfd, buffer,strlen(buffer));
printf("okn");
}
close(sockfd);
}
功能是客户端向服务器端发送消息,服务器端接受并显示
可是当第一次向服务器端发送消息时,服务器端能正常显示
但是接下来再向服务器端发送时,服务器端没有响应!
server.c如下:
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 7777
#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);
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 client! ";
int len, bytes_sent;
len = strlen(msg);
bytes_sent = send(new_fd, msg, len, 0);
printf("okn");
int nbytes;
//bzero(buffer,sizeof(buffer));
while(1)
{
if((nbytes=read(new_fd,buffer,1024))==-1)
{
perror("read errorn");
exit(1);
}
buffer[nbytes]='';
printf("I have received:%sn",buffer);
/*bzero(buffer,sizeof(buffer));
scanf("%s",buffer);
int len, bytes_sent;
bytes_sent = write(sockfd, buffer,strlen(buffer));
printf("okn");
*/
//close(new_fd);
}
//close(sockfd);
}
}
client.c 如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 7777
#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") ;
int nsock;
nsock=connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr));
if(nsock==-1)
{
perror("connect errorn");
return -1;
}
while(1)
{
/* if((nbytes=read(sockfd,buffer,1024))==-1)
{
printf("read errorn");
return -1 ;
}
buffer[nbytes]='';
printf("I have received:%sn",buffer);
*/
bzero(buffer,sizeof(buffer));
scanf("%s",buffer);
int len, bytes_sent;
bytes_sent = write(sockfd, buffer,strlen(buffer));
printf("okn");
}
close(sockfd);
}
|
server端的接收:while((nbytes=recv(new_fd,buffer,sizeof(buffer), 0)) == -1)
由于你的socket是阻塞方式的,那么每次recv一定要收全sizeof(buffer)个字节才会返回,否则一直阻塞,
再看看buffer的声明:char buffer[1024]; 那么sizeof(buffer)就是1024,
于是每次recv操作一定要收全1024个字节才会返回。
client端的发送:if((bytes_sent = send(sockfd, buffer,strlen(buffer), 0))
由于你的socket是阻塞方式的,那么每次recv一定要收全sizeof(buffer)个字节才会返回,否则一直阻塞,
再看看buffer的声明:char buffer[1024]; 那么sizeof(buffer)就是1024,
于是每次recv操作一定要收全1024个字节才会返回。
client端的发送:if((bytes_sent = send(sockfd, buffer,strlen(buffer), 0))