客户端连接和断开x2 [英] Client connects and disconnects x2
问题描述
您好,
我正在创建一个应用程序来处理我正在创建的服务器。服务器是一个win32控制台应用程序,客户端是C ++ windows存储应用程序,但我似乎在某处遇到了我的代码问题。
服务器输出:
服务器:客户端已连接。然后服务器:客户端断开连接两次。
连接处理程序:
for(;;)
{
while(socketConnection = accept(socketListen,(SOCKADDR *)& Address,& AddressSize))
{
if(socketConnection == INVALID_SOCKET)
{
closesocket(socketConnection);
休息;
}
unsigned ThreadID;
HANDLE clientHandle =(HANDLE)_beginthreadex(NULL,0,& ClientSession,(void *)socketConnection,0,& ThreadID);
Success = send(socketConnection,"你已连接到服务器。",120,NULL);
printf("服务器:客户端已连接。\ n");
}
}
断开处理程序:
unsigned __stdcall ClientSession (void * data)
{
while(true)
{
int Result;
char buf [BUF_LEN];
结果= recv(socketConnection,buf,BUF_LEN,NULL);
字符串转换器;
converter = buf;
if(Result == -1)
{
if(errno == 0)
{
printf(" Server:client has connected .\\\
");
}
其他
{
cout<< "服务器:收听时出错。 ErrorID:" << errno<< " \\\
英寸;
}
休息;
}
其他
{
cout<< "服务器:收到" <<转换器<< "来自客户端\ n" ;;
}
}
返回1;
}
客户端:
auto Sock = ref new Windows :: Networking :: Sockets :: StreamSocket();
auto localHostName = ref new Windows :: Networking :: HostName(SERVER_IP);
String ^ localService(SERVER_PORT);
Sock-> ConnectAsync(localHostName,localService);
我使用sstream作为客户端,Winsock使用服务器。
我只是不明白为什么会这样。
非常感谢任何帮助。
谢谢,
迈克。
编辑:我想补充说我已经尝试使用控制台应用程序作为客户端,它可以正常运行而不会出现任何错误。
客户端和服务器不在同一个盒子上吗?
Hello,
I'm currently creating a application to work with a server that I am creating. The server is a win32 console application and the client is C++ windows store application, however I seem to have an issue with my code somewhere.
The server outputs:
Server: A client has connected. then Server: A client has disconnected this happens twice.
Connection handler:
for (;;) { while (socketConnection = accept(socketListen, (SOCKADDR*)&Address, &AddressSize)) { if (socketConnection == INVALID_SOCKET) { closesocket(socketConnection); break; } unsigned ThreadID; HANDLE clientHandle = (HANDLE)_beginthreadex(NULL, 0, &ClientSession, (void*)socketConnection, 0, &ThreadID); Success = send(socketConnection, "You have connected to the server.", 120, NULL); printf("Server: A client has connected.\n"); } }
Disconnect handler:
unsigned __stdcall ClientSession(void *data) { while (true) { int Result; char buf[BUF_LEN]; Result = recv(socketConnection, buf, BUF_LEN, NULL); string converter; converter = buf; if (Result == -1) { if (errno == 0) { printf("Server: client has disconnected.\n"); } else { cout << "Server: Error whilst listening. ErrorID: " << errno << "\n"; } break; } else { cout << "Server: Received " << converter << " from client\n"; } } return 1; }
Client side:
auto Sock = ref new Windows::Networking::Sockets::StreamSocket(); auto localHostName = ref new Windows::Networking::HostName(SERVER_IP); String^ localService(SERVER_PORT); Sock->ConnectAsync(localHostName, localService);
I'm using sstream for client and Winsock for server.
I just don't understand why this happens.
Any help is greatly appreciated.
Thanks,
Mike.
EDIT: I would like to add that I've tried using a console application as client and it works fine without any sort of errors
The client and server are not on the same box are they?
这篇关于客户端连接和断开x2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!