当前位置: 技术问答>linux和unix
请求大家帮助,询问一个linux下本机调试一个简单网络通信的问题!!
来源: 互联网 发布时间:2016-03-30
本文导语: 本人是初学,现在linux下有服务器端和客户端的网络程序如下: /*server.c*/ #include #include #include #include #include #include int main() { int server_sockfd,client_sockfd; int server_len,client_len; struct sockaddr_in ...
本人是初学,现在linux下有服务器端和客户端的网络程序如下:
/*server.c*/
#include
#include
#include
#include
#include
#include
int main()
{
int server_sockfd,client_sockfd;
int server_len,client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
server_sockfd=socket(AF_INET,SOCK_STREAM,0);
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr=htonl(INADDR_ANY);
server_address.sin_port=htons(2000);
server_len=sizeof(struct sockaddr);
bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
listen(server_sockfd,5);
while(1)
{
char ch[40];
char ch1[]="Hello I am server! ";
printf("server waiting n");
client_sockfd=accept(server_sockfd,
(struct sockaddr *)&client_address,&client_len);
read(client_sockfd,&ch,40);
printf("receive from client : %sn",ch);
write(client_sockfd,&ch1,sizeof(ch1));
close(client_sockfd);
}
return 0;
}
//////////////////////////////////////
/*client.c*/
#include
#include
#include
#include
#include
#include
int main()
{
int sockfd;
int len;
struct sockaddr_in address;
int result;
char ch[]="Hello I am clientn ";
sockfd=socket(AF_INET,SOCK_STREAM,0);
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("127.0.0.1");
address.sin_port=htons(1989);
len=sizeof(struct sockaddr);
result=connect(sockfd,(struct sockaddr *)&address,len);
if(result==-1)
{
perror("oops:client1");
exit(0);
}
write(sockfd,&ch,sizeof(ch));
char ch1[40];
read(sockfd,&ch1,40);
printf("char form server = %s n",ch1);
close (sockfd);
exit(0);
return 0;
}
由于我没有两台电脑分别运行两个程序,所以我只能在本机上调试运行。在终端我用gcc编译两个程序都没问题,运行服务器端也没问题,显示服务器“server waiting”,当开另一个终端运行客户端时,出现“bash: ./client2.c: Permission denied”,后来我把客户端程序的端口改成和2000和服务器端一样还是出现同样的问题,接收不到服务器端发来的字符。我不知道是linux下不能单机调试网络程序还是什么原因,请各位指点一下,菜鸟在此非常谢谢了!
/*server.c*/
#include
#include
#include
#include
#include
#include
int main()
{
int server_sockfd,client_sockfd;
int server_len,client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
server_sockfd=socket(AF_INET,SOCK_STREAM,0);
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr=htonl(INADDR_ANY);
server_address.sin_port=htons(2000);
server_len=sizeof(struct sockaddr);
bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
listen(server_sockfd,5);
while(1)
{
char ch[40];
char ch1[]="Hello I am server! ";
printf("server waiting n");
client_sockfd=accept(server_sockfd,
(struct sockaddr *)&client_address,&client_len);
read(client_sockfd,&ch,40);
printf("receive from client : %sn",ch);
write(client_sockfd,&ch1,sizeof(ch1));
close(client_sockfd);
}
return 0;
}
//////////////////////////////////////
/*client.c*/
#include
#include
#include
#include
#include
#include
int main()
{
int sockfd;
int len;
struct sockaddr_in address;
int result;
char ch[]="Hello I am clientn ";
sockfd=socket(AF_INET,SOCK_STREAM,0);
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("127.0.0.1");
address.sin_port=htons(1989);
len=sizeof(struct sockaddr);
result=connect(sockfd,(struct sockaddr *)&address,len);
if(result==-1)
{
perror("oops:client1");
exit(0);
}
write(sockfd,&ch,sizeof(ch));
char ch1[40];
read(sockfd,&ch1,40);
printf("char form server = %s n",ch1);
close (sockfd);
exit(0);
return 0;
}
由于我没有两台电脑分别运行两个程序,所以我只能在本机上调试运行。在终端我用gcc编译两个程序都没问题,运行服务器端也没问题,显示服务器“server waiting”,当开另一个终端运行客户端时,出现“bash: ./client2.c: Permission denied”,后来我把客户端程序的端口改成和2000和服务器端一样还是出现同样的问题,接收不到服务器端发来的字符。我不知道是linux下不能单机调试网络程序还是什么原因,请各位指点一下,菜鸟在此非常谢谢了!
|
/*server.c*/
#include
#include
#include
#include
#include
#include
int main()
{
int server_sockfd,client_sockfd;
int server_len,client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
server_sockfd=socket(AF_INET,SOCK_STREAM,0);
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr=htonl(INADDR_ANY);
server_address.sin_port=htons(2000);
server_len=sizeof(struct sockaddr);
bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
listen(server_sockfd,5); //bind和listen这两个函数应该添加错误处理,判断返回值是否已经绑定成功和监听成功
while(1)
{
char ch[40];
char ch1[]="Hello I am server! ";
printf("server waiting n");
client_sockfd=accept(server_sockfd,
(struct sockaddr *)&client_address,&client_len);
read(client_sockfd,&ch,40);
printf("receive from client : %sn",ch);
write(client_sockfd,&ch1,sizeof(ch1));
close(client_sockfd);
}
return 0;
}
//////////////////////////////////////
/*client.c*/
#include
#include
#include
#include
#include
#include
int main()
{
int sockfd;
int len;
struct sockaddr_in address;
int result;
char ch[]="Hello I am clientn ";
sockfd=socket(AF_INET,SOCK_STREAM,0);
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("127.0.0.1");
address.sin_port=htons(1989);
len=sizeof(struct sockaddr);
result=connect(sockfd,(struct sockaddr *)&address,len);
if(result==-1)
{
perror("oops:client1");
exit(0);
}
write(sockfd,&ch,sizeof(ch));
char ch1[40];
read(sockfd,&ch1,40);
printf("char form server = %s n",ch1);
close (sockfd);
exit(0);
return 0;
}
你的服务器端用的端口号是2000,而你的客服端用的端口号却是1989.
运行客户端,出错变成了oops:client1: Connection refused
这很可能是你的服务器没有启动或是因为端口号不对,你先改改看看。
用单机调试和联网调试是一样的,只要在单机调试通过一般是没问题了。
#include
#include
#include
#include
#include
#include
int main()
{
int server_sockfd,client_sockfd;
int server_len,client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
server_sockfd=socket(AF_INET,SOCK_STREAM,0);
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr=htonl(INADDR_ANY);
server_address.sin_port=htons(2000);
server_len=sizeof(struct sockaddr);
bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
listen(server_sockfd,5); //bind和listen这两个函数应该添加错误处理,判断返回值是否已经绑定成功和监听成功
while(1)
{
char ch[40];
char ch1[]="Hello I am server! ";
printf("server waiting n");
client_sockfd=accept(server_sockfd,
(struct sockaddr *)&client_address,&client_len);
read(client_sockfd,&ch,40);
printf("receive from client : %sn",ch);
write(client_sockfd,&ch1,sizeof(ch1));
close(client_sockfd);
}
return 0;
}
//////////////////////////////////////
/*client.c*/
#include
#include
#include
#include
#include
#include
int main()
{
int sockfd;
int len;
struct sockaddr_in address;
int result;
char ch[]="Hello I am clientn ";
sockfd=socket(AF_INET,SOCK_STREAM,0);
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("127.0.0.1");
address.sin_port=htons(1989);
len=sizeof(struct sockaddr);
result=connect(sockfd,(struct sockaddr *)&address,len);
if(result==-1)
{
perror("oops:client1");
exit(0);
}
write(sockfd,&ch,sizeof(ch));
char ch1[40];
read(sockfd,&ch1,40);
printf("char form server = %s n",ch1);
close (sockfd);
exit(0);
return 0;
}
你的服务器端用的端口号是2000,而你的客服端用的端口号却是1989.
运行客户端,出错变成了oops:client1: Connection refused
这很可能是你的服务器没有启动或是因为端口号不对,你先改改看看。
用单机调试和联网调试是一样的,只要在单机调试通过一般是没问题了。