我有一个正常的客户端 - 服务器通信,就像Beej的指南insructs来做这些事情。
服务器发送一个字符串,但是在客户端的recv中,虽然返回的值是ok(即等于从服务器发送的字节数),但是收到的字符非常奇怪,并且strlen在接收到的字符串上返回的值小于recv。返回的值
这对我来说似乎很奇怪,所以如果你能给我一个提示,那就太好了。
这是服务器代码的一部分:
s_gateway_server = socket( PF_INET, SOCK_STREAM, 0 );
memset( &server, 0, sizeof( server ) ); // asiguram padding-ul
server.sin_family = AF_INET;
server.sin_port = htons( is.port );
inet_pton( AF_INET, is.ip, &server.sin_addr );
status = connect( s_gateway_server, ( struct sockaddr* ) &server, sizeof( server ) );
if( status == -1 )
{
printf( "ERROR\nNu te-ai putut conecta la server-ul %s :-(\n", is.ip );
goto exit;
}
l = strlen( msg ) + 1;
status = send( s_gateway_server, &msg, l, 0 );
if( status == -1 )
{
perror( "ERROR\nla send catre server" );
goto exit;
}
if( status < l )
{
//TODO trimite restul de pachet
printf( "ERROR\npachet ajuns incomplet la server\n" );
goto exit;
}
printf( "Am trimis %d octeti: %s\n", status, msg );
这是客户代码的一部分:
void send_and_receive( int sock )
{
int status, ret;
while( 1 )
{
char receivebuff[500];
status = recv( sock, &receivebuff, 500, 0 );
if( status == -1 )
{
perror( "ERROR\nla recv" );
continue;
}
if( status == 0 )
// connection closed
break;
(没有粘贴所有函数),并在main()中调用该函数:
int main( int argc, char **argv )
{
int s_server_gateway, conn_s_server_gateway, ret;
struct sockaddr_in gateway;
socklen_t length;
ret = parse_arg(argc, argv);
if( ret )
return 1;
s_server_gateway = listen_to_clients();
if( s_server_gateway < 0 )
return 2;
while( 1 )
{
length = sizeof( gateway );
conn_s_server_gateway = accept( s_server_gateway, ( struct sockaddr * ) &gateway, &length );
if( conn_s_server_gateway == -1 )
{
perror( "ERROR\nla accept de la gateway" );
continue;
}
if( !fork() )
{
close( s_server_gateway );
send_and_receive( conn_s_server_gateway );
return 0;
}
else
{
close( conn_s_server_gateway );
continue;
}
}
close( s_server_gateway );
return 0;
}
int listen_to_clients()
{
int s_server_gateway, optval, ret;
struct sockaddr_in server;
s_server_gateway = socket( PF_INET, SOCK_STREAM, 0 );
if( s_server_gateway == -1 )
{
perror( "ERROR\nla deschidere socket" );
return -1;
}
memset( &server, 0, sizeof( server ) ); // asiguram padding-ul
server.sin_family = AF_INET;
server.sin_port = htons( port );
inet_pton( AF_INET, "127.0.0.1", &server.sin_addr );
optval = 1;
ret = setsockopt( s_server_gateway, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof( int ) );
if( ret == -1 )
{
perror( "ERROR\nla setsockopt" );
close( s_server_gateway );
return -2;
}
ret = bind( s_server_gateway, ( struct sockaddr * ) &server, sizeof( server ) );
if( ret == -1 )
{
perror( "ERROR\nla bind" );
close( s_server_gateway );
return -3;
}
listen( s_server_gateway, BACKLOG );
if( ret == -1 )
{
perror( "ERROR\nla listen" );
close( s_server_gateway );
return -4;
}
return s_server_gateway;
}
答案 0 :(得分:1)
recv()
char receivebuff[500];
// status = recv( sock, &receivebuff, 500, 0 )
status = recv( sock, receivebuff, 500, 0 ); // no &
// or better
status = recv( sock, receivebuff, sizeof(receivebuff), 0 );