菜鸟问题 一个最简单的网络编程
下面的代码为什么一直运行到 else if (ret == SOCKET_ERROR)
{
printf("recv() failed: %d\n", WSAGetLastError());
return false;
}
还有请问您一下出现接受信心时的
SOCKET_ERROR 有哪些情况?
服务端
#include "stdafx.h"
#include < winsock2.h >
#pragma comment(lib,"ws2_32.lib")
struct WSAInfo
{
int type;
char infoBuffer[256];
};
bool startWsa()
{
WSADATA WSAData;
if (0 != WSAStartup(MAKEWORD(2,2), &WSAData))
{
printf("WDAStartup() get wrong, check it.\n");
return false;
}
SOCKET pruSockt,newSockt;
pruSockt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (pruSockt == INVALID_SOCKET)
{
printf("socket() failed: %d\n", WSAGetLastError());
return false;
}
struct sockaddr_in server;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_family = AF_INET;
server.sin_port = htons( 30038 );
if( bind( pruSockt, (struct sockaddr *)&server, sizeof(server) ) == SOCKET_ERROR )
{
printf("bind() failed: %d\n", WSAGetLastError());
return 1;
}
listen( pruSockt,10);
while( 1 )
{
int sizeOfSocket = sizeof( server );
newSockt = accept( pruSockt,( struct sockaddr *)&server, &sizeOfSocket );
if (newSockt == INVALID_SOCKET)
{
printf("accept() failed: %d\n", WSAGetLastError());
break;
}
// WSAInfo *resBuf;
// resBuf->type = 0;
// resBuf->infoBuffer = ;
//memset(resBuf,0,sizeof( WSAInfo ) );
//int lenth = sizeof( WSAInfo );
//char*szBuff = new char[lenth];
char buf[12];
int ret = recv(newSockt, buf, 12, 0);
if (ret == 0) // Graceful close
{
}
else if (ret == SOCKET_ERROR)
{
printf("recv() failed: %d\n", WSAGetLastError());
return false;
}
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
startWsa();
Sleep( 10000 );
return 0;
}
#include "stdafx.h"
#include < winsock2.h >
#pragma comment(lib,"ws2_32.lib")
struct WSAInfo
{
int type;
char infoBuffer[256];
};
bool startWsa()
{
WSADATA WSAData;
if (0 != WSAStartup(MAKEWORD(2,2), &WSAData))
{
printf("WDAStartup() get wrong, check it.\n");
return false;
}
SOCKET pruSockt,newSockt;
pruSockt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (pruSockt == INVALID_SOCKET)
{
printf("socket() failed: %d\n", WSAGetLastError());
return false;
}
struct sockaddr_in server;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 30038 );
//连接
if (connect(pruSockt, (struct sockaddr *)&server,
sizeof(server)) == SOCKET_ERROR)
{
printf("connect() failed: %d\n", WSAGetLastError());
return false;
}
//WSAInfo *resBuf;
//memset(resBuf,0,sizeof( WSAInfo ) );
//resBuf->type = 1;
//char*info = "this is a test";
// strcpy( resBuf->infoBuffer,info);
//int lenth = sizeof(resBuf);
char *resBuf = "111111111111";
int lenth = strlen( resBuf );
int ret = send(pruSockt, (const char *)resBuf, lenth, 0);
if (ret == 0)
{
printf("send() ret = 0 failed: %d\n", WSAGetLastError());
return false;
}
else if (ret == SOCKET_ERROR)
{
printf("send() ret == SOCKET_ERROR failed: %d\n", WSAGetLastError());
return false;
}
else if ( ret > 0 && ret < lenth )
{
printf("send() ret < lenth failed: %d\n", WSAGetLastError());
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
if( startWsa() )
{
printf("%s","客户端成功!" );
}
else
{
printf( "%s","客户端失败!");
}
Sleep( 100000 );
return 0;
}
char buf[13]={0};void HexDump(char *buf,int len) {
int i,j,k;
char binstr[80];
for (i=0;i<len;i++) {
if (0==(i%16)) {
sprintf(binstr,"%04x -",i);
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
} else if (15==(i%16)) {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
sprintf(binstr,"%s ",binstr);
for (j=i-15;j<=i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
} else {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
}
}
if (0!=(i%16)) {
k=16-(i%16);
for (j=0;j<k;j++) {
sprintf(binstr,"%s ",binstr);
}
sprintf(binstr,"%s ",binstr);
k=16-k;
for (j=i-k;j<i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
}
}