套接字绑定失败、监听失败....求解
//我已经按书籍要求加入库模块ws2_32.lib,先运行服务器,在运行客户端的,但是客户端运行不起来,这个是运行效果-------------------------------------这个是服务器这边的编程
#include<stdio.h>
#include<winsock.h>
int main()
{
char Sendbuf[100];
char Receivebuf[100];
int SendLen;
int ReceiveLen;
int Length;
SOCKET socket_server;
SOCKET socket_receive;
SOCKADDR_IN Server_add;
SOCKADDR_IN Client_add;
WORD wVersionRequested;
WSADATA wsaData;
int error;
wVersionRequested=MAKEWORD(2,2);
error=WSAStartup(wVersionRequested,&wsaData);
if(error!=0)
{
printf("加载套接字失败!");
return 0;
}
if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
{
WSACleanup();
return 0;
}
Server_add.sin_family=AF_INET;
Server_add.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
Server_add.sin_port=htons(5000);
socket_server=socket(AF_INET,SOCK_STREAM,0);
if(bind(socket_server,(SOCKADDR*)&Server_add,sizeof(SOCKADDR))==SOCKET_ERROR)
{
printf("绑定失败\n");
}
if(listen(socket_server,5)<0)
{
printf("监听失败\n");
}
Length=sizeof(SOCKADDR);
socket_receive=accept(socket_server,(SOCKADDR*)&Client_add,&Length);
if(socket_receive==SOCKET_ERROR)
{
printf("接受连接失败");
}
while(1)
{
ReceiveLen=recv(socket_receive,Receivebuf,100,0);
if(ReceiveLen<0)
{
printf("接收失败\n");
printf("程序退出\n");
break;
}
else
{
printf("client say: %s\n",Receivebuf);
}
printf("please enter message:");
scanf("%s",Sendbuf);
SendLen=send(socket_receive,Sendbuf,100,0);
if(SendLen<0)
{
printf("发送失败\n");
}
}
closesocket(socket_receive);
closesocket(socket_server);
WSACleanup();
return 0;
}
-------------------------------------这个是客户端这边的编程
#include<stdio.h>
#include<winsock.h> //调试前注意添加库模块ws2_32.lib
int main()
{
char Sendbuf[100];
char Receivebuf[100];
int SendLen;
int ReceiveLen;
SOCKET socket_send;
SOCKADDR_IN Server_add;
WORD wVersionRequested;
WSADATA wsaData;
int error;
wVersionRequested = MAKEWORD( 2, 2 );
error = WSAStartup( wVersionRequested, &wsaData);
printf("%d",error);
if(error!=0)
{
printf("加载套接字失败!");
return 0;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
{
WSACleanup( );
return 0;
}
Server_add.sin_family=AF_INET;
Server_add.sin_addr.S_un.S_addr = inet_addr("192.168.1.238");
Server_add.sin_port=htons(5000);/*端口号*/
socket_send=socket(AF_INET,SOCK_STREAM,0);
if(connect(socket_send,(SOCKADDR*)&Server_add,sizeof(SOCKADDR)) == SOCKET_ERROR)
{
printf("连接失败!\n");
}
while(1)
{
printf("please enter message:");
scanf("%s",Sendbuf);
SendLen = send(socket_send,Sendbuf,100,0);
if(SendLen < 0)
{
printf("发送失败!\n");
}
ReceiveLen =recv(socket_send,Receivebuf,100,0);
if(ReceiveLen<0)
{
printf("接收失败\n");
printf("程序退出\n");
break;
}
else
{
printf("Server say: %s\n",Receivebuf);
}
}
closesocket(socket_send);
WSACleanup();
return 0;
}