MinGW/C++使用WinInet函数send()和recv()异常

Toxic_Obsidian 2019-02-02 11:51:21
服务端代码:

WSADATA wsaData;
WSAStartup( MAKEWORD(2,2) , &wsaData );
SOCKET cli , ser;
SOCKADDR_IN sera , clia;
int clin = sizeof( SOCKADDR );

ZeroMemory( &sera , sizeof(sera) );
ZeroMemory( &clia , sizeof(clia) );
sera.sin_family = AF_INET;
sera.sin_port = htons( 10080 );
sera.sin_addr.S_un.S_addr = ADDR_ANY;
ser = socket( AF_INET , SOCK_STREAM , 0 );
if( ser == INVALID_SOCKET ){
cout << "Cannot init socket!\n";
return 0;
}
if( 0 != bind( ser , (SOCKADDR*)(&sera) , sizeof(SOCKADDR) ) ){
cout << "Cannot bind!\n";
printf("Error code:%d\n",WSAGetLastError());
closesocket( ser );
return 1;
}
if( 0 != listen( ser , 5 ) ){
cout << "Cannot listen!\n";
printf("Error code:%d\n",WSAGetLastError());
closesocket( ser );
return 1;
}

const char wav[] = "Hello Client!";
char* rwav = new char[sizeof(wav)];
ZeroMemory( rwav , sizeof(wav) );
cli = accept( ser , (SOCKADDR*)(&clia) , &clin );
if( cli == INVALID_SOCKET ){
cout << "Cannot init acc-socket!\n" << "Error code:";
printf( "0x%x" , WSAGetLastError() );
closesocket( ser );
return 1;
}
cout << "Prepared for transmitting!\nsizeof(wav)=" << sizeof(wav) << endl;
if( SOCKET_ERROR == send( cli , wav , sizeof(wav) , 0 ) ){
cout << "Failed to send!\n";
printf("Error code:%d\n",WSAGetLastError());
closesocket( ser );
closesocket( cli );
return 1;
}

int rcvnum;
rcvnum = recv( cli , rwav , sizeof(wav) , 0 );
if( rcvnum != sizeof(wav) ){
if( rcvnum <= 0 ){
cout << "Error occurred:" << rcvnum << endl;
return 1;
}
else{
cout << "Length not enough:" << rcvnum << endl;
return 1;
}
}
cout << "Client returned:" << rwav << endl;

getch();
delete[] rwav;
closesocket( cli );
closesocket( ser );
WSACleanup();


客户端代码:

WSADATA wsaData;
WSAStartup( MAKEWORD(2,2) , &wsaData );
SOCKET ser;
SOCKADDR_IN sera;
ser = socket( AF_INET , SOCK_STREAM , 0 );

ZeroMemory( &sera , sizeof(sera) );
sera.sin_family = AF_INET;
sera.sin_port = htons( 10080 );
sera.sin_addr.S_un.S_addr = inet_addr( "127.0.0.1" );

if( 0 != connect( ser , (SOCKADDR*)(&sera) , sizeof(SOCKADDR) ) ){
cout << "Cannot connect to server!\n";
printf("Error code:%d\n",WSAGetLastError());
return 1;
}

// "Hello Server!"
const char* wav = "Hello Server!";
char* rwav = new char[sizeof(wav)];
ZeroMemory( rwav , sizeof(wav) );
recv( ser , rwav , sizeof(wav) , 0 );
cout << "Server sent:" << rwav;
getchar();

send( ser , wav , sizeof(wav) , 0 );

getch();
delete[] rwav;
WSACleanup();

现在出现了比较诡异的场面。。。
服务端收到客户端发来的信息是前4字节,后面没有收到(recv()返回值是4)
客户端收到服务端发来的信息是不定前n字节+一串乱码(没有显示recv了多少个,但后面乱码到最后一个NULL时停止了)

如图
求各位大虾支招啊!
...全文
254 点赞 收藏 3
写回复
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
小猪jazzy 2019-02-19
这个是类型判断,原来类型是char *,现在类型时char 【14】,指针与char[14]大小当然不同
回复
Toxic_Obsidian 2019-02-03
引用 1 楼 weixin_39913932 的回复:
你的客户端指针使用的sizeof(wav)返回就是4,与服务端有区别,就是这个问题 const char* wav = "Hello Server!"; char* rwav = new char[sizeof(wav)]; ZeroMemory( rwav , sizeof(wav) ); recv( ser , rwav , sizeof(wav) , 0 ); cout << "Server sent:" << rwav; getchar(); send( ser , wav , sizeof(wav) , 0 );
感谢您,但我现在又有个问题。 为是什么把const char* wav改成const char wav[]就能返回正常的sizeof(wav)=14而不是sizeof(wav)=4
回复
小猪jazzy 2019-02-03
你的客户端指针使用的sizeof(wav)返回就是4,与服务端有区别,就是这个问题 const char* wav = "Hello Server!"; char* rwav = new char[sizeof(wav)]; ZeroMemory( rwav , sizeof(wav) ); recv( ser , rwav , sizeof(wav) , 0 ); cout << "Server sent:" << rwav; getchar(); send( ser , wav , sizeof(wav) , 0 );
回复
相关推荐
发帖
其它技术问题
创建于2007-09-28

3843

社区成员

C/C++ 其它技术问题
申请成为版主
帖子事件
创建了帖子
2019-02-02 11:51
社区公告
暂无公告