当前位置: 技术问答>linux和unix
服务器客户端通信问题
来源: 互联网 发布时间:2015-07-29
本文导语: 想用Linux做服务器,Windows为客户端实现通信问题,照着书上写了服务器端的程序,出现问题,请高手帮忙解决一下。 | 客户端: #include #include #include #include #include char *msg = "HELLO"; in...
想用Linux做服务器,Windows为客户端实现通信问题,照着书上写了服务器端的程序,出现问题,请高手帮忙解决一下。
|
客户端:
#include
#include
#include
#include
#include
char *msg = "HELLO";
int main()
{
int sockfd;
struct sockaddr_in dest_addr;
char buff[20];
sockfd = socket( AF_INET, SOCK_STREAM, 0 );
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons( 5050 );
dest_addr.sin_addr.s_addr = inet_addr( "192.168.8.201" );
connect( sockfd, (struct sockaddr*)&dest_addr, sizeof(struct sockaddr));
send( sockfd, msg, strlen(msg), 0 );
recv( sockfd, buff, 20, 0 );
printf( "%sn",buff );
close( sockfd );
return 0;
}
服务器端:
#include
#include
#include
#include
#include
int main()
{
int sockfd, newfd;
char buff[20];
struct sockaddr_in my_addr;
struct sockaddr_in other_addr;
int sin_size;
sockfd = socket( AF_INET, SOCK_STREAM, 0);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons( 5050 );
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero), 8);
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
listen(sockfd, 1);
// sin_size = sizeof(struct sockaddr_in);
printf( "1n" );
newfd = accept( sockfd, &other_addr, &sin_size );
printf( "2n" );
printf( "%s:%d CONNECTEDn", inet_ntoa(other_addr.sin_addr), ntohs(other_addr.sin_port) );
recv( newfd, buff, 20, 0 );
printf( "%sn",buff );
memset( buff, 0 , 20 );
strcpy( buff, inet_ntoa(other_addr.sin_addr) );
send( newfd, buff, strlen(inet_ntoa(other_addr.sin_addr)),0 );
close( sockfd) ;
}
#include
#include
#include
#include
#include
char *msg = "HELLO";
int main()
{
int sockfd;
struct sockaddr_in dest_addr;
char buff[20];
sockfd = socket( AF_INET, SOCK_STREAM, 0 );
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons( 5050 );
dest_addr.sin_addr.s_addr = inet_addr( "192.168.8.201" );
connect( sockfd, (struct sockaddr*)&dest_addr, sizeof(struct sockaddr));
send( sockfd, msg, strlen(msg), 0 );
recv( sockfd, buff, 20, 0 );
printf( "%sn",buff );
close( sockfd );
return 0;
}
服务器端:
#include
#include
#include
#include
#include
int main()
{
int sockfd, newfd;
char buff[20];
struct sockaddr_in my_addr;
struct sockaddr_in other_addr;
int sin_size;
sockfd = socket( AF_INET, SOCK_STREAM, 0);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons( 5050 );
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero), 8);
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
listen(sockfd, 1);
// sin_size = sizeof(struct sockaddr_in);
printf( "1n" );
newfd = accept( sockfd, &other_addr, &sin_size );
printf( "2n" );
printf( "%s:%d CONNECTEDn", inet_ntoa(other_addr.sin_addr), ntohs(other_addr.sin_port) );
recv( newfd, buff, 20, 0 );
printf( "%sn",buff );
memset( buff, 0 , 20 );
strcpy( buff, inet_ntoa(other_addr.sin_addr) );
send( newfd, buff, strlen(inet_ntoa(other_addr.sin_addr)),0 );
close( sockfd) ;
}
|
哦,好多简单的错误哦。。。。。。
慢慢来,会提高效率的。。
慢慢来,会提高效率的。。
|
全是语法错误。编译器已经把错误定位的很清楚拉,自己一个个照着去检查不就得了。
|
小妹妹,你写程序太不仔细了,
sprintf()写错
还有你封装write_all和read_all的在那里
sprintf()写错
还有你封装write_all和read_all的在那里
|
你看一下TCP/IP高级编程,看不懂来问
|
想要的话 我发给你一个 不过下个礼拜才能给你
|
这种错误一般都是你联接头文件出错,你应该仔细检查一下头文件包含的路径,和你自己的程序,和头文件没有关系