初学winsock编程,想练习一下发送和接受的编写,以下是服务端和客户端的两段代码,运行时客户端正常,但服务端的recv()总是返回-1,收不到发来的信息,"include <winsock.h>"和AfxSocketInit()没忘加入,请大家帮忙分析一下错在哪里?
服务器端:
WSADATA wsd;
char buffer[255];
CString str;
int xx,IpPort;
SOCKET socketListen,sClient;
struct sockaddr_in LocalAddr,ClientAddr;
IpPort=5051;
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
AfxMessageBox("启动socket.dll出错");
return ;
}
socketListen=socket(AF_INET,SOCK_STREAM,0);//IPPORO_IP);
if(socketListen==SOCKET_ERROR)
{
str.Format("%d",GetLastError());
AfxMessageBox("socket wrong is" + str);
return ;
}
LocalAddr.sin_addr.s_addr=inet_addr(GetMyIP());//GetMyIP()是取得本机IP地址
LocalAddr.sin_port=htons(IpPort);
LocalAddr.sin_family=AF_INET;
if(bind(socketListen,(struct sockaddr *)&LocalAddr,sizeof(LocalAddr))==SOCKET_ERROR)
{
str.Format("%d",GetLastError());
AfxMessageBox("bind wrong is" + str);
return ;
}
listen(socketListen,2);
xx=sizeof(ClientAddr);
sClient=accept(socketListen,(struct sockaddr *) &ClientAddr,&xx);
if(sClient==INVALID_SOCKET)
{
str.Format("%d",GetLastError());
AfxMessageBox("accept wrong is" + str);
return ;
}
xx=recv(sClient,buffer,33,0);//这里总是返回-1?
if(xx=SOCKET_ERROR)
{
str.Format("recv return %d",xx);
AfxMessageBox(str);
return ;
}
AfxMessageBox(buffer);
shutdown(socketListen,2);
closesocket(socketListen);
WSACleanup();
客户端:
CString str;
int xx;
WSADATA wsd;
SOCKET sockSend;
SOCKADDR_IN addrServer;
char * szMessage="hello! the information come from ";
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
AfxMessageBox("启动socket.dll出错");
return ;
} sockSend=socket(AF_INET,SOCK_STREAM,0);
addrServer.sin_addr.s_addr=inet_addr(m_strIP);
addrServer.sin_family=AF_INET;
addrServer.sin_port=htons( 5051);
if(connect(sockSend,(struct sockaddr *)&addrServer,sizeof(addrServer))==SOCKET_ERROR)
{
str.Format("%d",GetLastError());
AfxMessageBox("connect wrong is" + str);
return ;
} xx=send(sockSend,szMessage,strlen(szMessage),0);
if(xx==SOCKET_ERROR)
{
str.Format("%d",GetLastError());
AfxMessageBox("send wrong is" + str);
return ;
}
shutdown(sockSend,2);
closesocket(sockSend);
WSACleanup();
服务器端:
WSADATA wsd;
char buffer[255];
CString str;
int xx,IpPort;
SOCKET socketListen,sClient;
struct sockaddr_in LocalAddr,ClientAddr;
IpPort=5051;
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
AfxMessageBox("启动socket.dll出错");
return ;
}
socketListen=socket(AF_INET,SOCK_STREAM,0);//IPPORO_IP);
if(socketListen==SOCKET_ERROR)
{
str.Format("%d",GetLastError());
AfxMessageBox("socket wrong is" + str);
return ;
}
LocalAddr.sin_addr.s_addr=inet_addr(GetMyIP());//GetMyIP()是取得本机IP地址
LocalAddr.sin_port=htons(IpPort);
LocalAddr.sin_family=AF_INET;
if(bind(socketListen,(struct sockaddr *)&LocalAddr,sizeof(LocalAddr))==SOCKET_ERROR)
{
str.Format("%d",GetLastError());
AfxMessageBox("bind wrong is" + str);
return ;
}
listen(socketListen,2);
xx=sizeof(ClientAddr);
sClient=accept(socketListen,(struct sockaddr *) &ClientAddr,&xx);
if(sClient==INVALID_SOCKET)
{
str.Format("%d",GetLastError());
AfxMessageBox("accept wrong is" + str);
return ;
}
xx=recv(sClient,buffer,33,0);//这里总是返回-1?
if(xx=SOCKET_ERROR)
{
str.Format("recv return %d",xx);
AfxMessageBox(str);
return ;
}
AfxMessageBox(buffer);
shutdown(socketListen,2);
closesocket(socketListen);
WSACleanup();
客户端:
CString str;
int xx;
WSADATA wsd;
SOCKET sockSend;
SOCKADDR_IN addrServer;
char * szMessage="hello! the information come from ";
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
AfxMessageBox("启动socket.dll出错");
return ;
} sockSend=socket(AF_INET,SOCK_STREAM,0);
addrServer.sin_addr.s_addr=inet_addr(m_strIP);
addrServer.sin_family=AF_INET;
addrServer.sin_port=htons( 5051);
if(connect(sockSend,(struct sockaddr *)&addrServer,sizeof(addrServer))==SOCKET_ERROR)
{
str.Format("%d",GetLastError());
AfxMessageBox("connect wrong is" + str);
return ;
} xx=send(sockSend,szMessage,strlen(szMessage),0);
if(xx==SOCKET_ERROR)
{
str.Format("%d",GetLastError());
AfxMessageBox("send wrong is" + str);
return ;
}
shutdown(sockSend,2);
closesocket(sockSend);
WSACleanup();
255