我是初学socket编程,单机调试一个c/s程序的框架,服务端的accept在客户端循环请求发送成功的情况下,总是返回-1。这是怎么回事?望明白人指点一下。谢谢。//客户端的各函数返回都没有异常。
//客户端:
#include <winsock2.h>
#include <stdio.h>
int main(int argc,char** argv)
{
WSADATA wsa_Data;
SOCKET client_socket;
SOCKADDR_IN host_addr;
int length;
char* chs;
if(WSAStartup(MAKEWORD(2,2),&wsa_Data)==0)
{
client_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(client_socket!=INVALID_SOCKET)
{
host_addr.sin_family=AF_INET;
host_addr.sin_port=htons(4567);
host_addr.sin_addr.S_un.S_addr=inet_addr("192.168.1.234");
if(connect(client_socket,(SOCKADDR*)&host_addr,sizeof(SOCKADDR_IN))!=SOCKET_ERROR)
{
chs=(char*)malloc(20*sizeof(char));
memset(chs,0,20);
strcpy(chs,"hello world!!!");
while(1)
{
length=send(client_socket,chs,strlen(chs),0);
}
}
free(chs);
closesocket(client_socket);
}
WSACleanup();
}
return 0;
}
//服务端:
#include <winsock2.h>
#include <stdio.h>
int main(int argc, char** argv)
{
WSADATA wsa_Data;
SOCKET server_socket,client_socket;
SOCKADDR_IN host_addr,client_addr;
int length;
unsigned int i;
if(WSAStartup(MAKEWORD(2,2),&wsa_Data)==0)
{
server_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(server_socket!=INVALID_SOCKET)
{
host_addr.sin_family=AF_INET;
host_addr.sin_port=htons(4567);
host_addr.sin_addr.S_un.S_addr=inet_addr("192.168.1.234");
if(bind(server_socket,(SOCKADDR*)&host_addr,sizeof(SOCKADDR_IN))!=SOCKET_ERROR)
{
if(listen(server_socket,SOMAXCONN)!=SOCKET_ERROR)
{
i=0;
while(1)
{
client_socket=accept(server_socket,(SOCKADDR*)&client_addr,&length);
if(client_socket!=INVALID_SOCKET)
{
printf("one request!\n");
//recv
//send
//closesocket
}
i++;
}
}
}
closesocket(server_socket);
}
WSACleanup();
}
return 0;
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课