Help to find the error.
Both client and server are on one machine. The network is disconnected. The error occurs constantly. The firewall is off.
This client side :
#include "stdafx.h"
#include <iostream>
#include <clocale>
#include <conio.h>
using namespace std;
bool Nonblocking(SOCKET fd)
{
u_long arg = 1;
return (::ioctlsocket(fd, FIONBIO, &arg) == 0);
}
bool Blocking(SOCKET fd)
{
u_long arg = 0;
return (::ioctlsocket(fd, FIONBIO, &arg) == 0);
}
bool is_connected(int sock)
{
char buf;
int err = recv(sock, &buf, 1, MSG_PEEK);
return err == -1 ? false : true;
}
void checkRecvEvent(WSANETWORKEVENTS NetworkEvents)
{
switch (NetworkEvents.lNetworkEvents)
{
case FD_READ:
printf("Socket recived event FD_READ.\n");
break;
case FD_WRITE:
printf("Socket recived event FD_WRITE.\n");
break;
case FD_OOB:
printf("Socket recived event FD_OOB.\n");
break;
case FD_ACCEPT:
printf("Socket recived event FD_ACCEPT.\n");
break;
case FD_CONNECT:
printf("Socket recived event FD_CONNECT.\n");
break;
case FD_CLOSE:
printf("Socket recived event FD_CLOSE.\n");
break;
case FD_QOS:
printf("Socket recived event FD_QOS.\n");
break;
case FD_GROUP_QOS:
printf("Socket recived event FD_GROUP_QOS.\n");
break;
case FD_ROUTING_INTERFACE_CHANGE:
printf("Socket recived event FD_ROUTING_INTERFACE_CHANGE.\n");
break;
case FD_ADDRESS_LIST_CHANGE:
printf("Socket recived event FD_ADDRESS_LIST_CHANGE.\n");
break;
case FD_ALL_EVENTS:
printf("Socket recived event FD_ALL_EVENTS.\n");
break;
default:
printf("We are not known this event.\n");
}
}
int _tmain(int argc, _TCHAR* argv[])
{
WSAData wsaData;
const char ConnectIP[20] = "127.0.0.1";
int errCode = 0;
struct sockaddr_in addr, addr1;
int iClientSize = sizeof(sockaddr);
DWORD EventTotal = 0;
WSANETWORKEVENTS NetworkEvents;
WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
memset(&addr, 0, sizeof(sockaddr_in));
memset(&addr1, 0, sizeof(sockaddr_in));
if (WSAStartup(0x0202, &wsaData) != 0){
return -1;
}
for (int i = 0; i < WSA_MAXIMUM_WAIT_EVENTS; i++)
EventArray[i] = WSA_INVALID_EVENT;
addr.sin_family = AF_INET;
addr.sin_port = htons(5801);
addr.sin_addr.s_addr = inet_addr(ConnectIP);
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
addr1.sin_family = AF_INET;
addr1.sin_port = htons(5802);
addr1.sin_addr.s_addr = inet_addr(ConnectIP);
SOCKET sock1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
unsigned long sockArg = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&sockArg, sizeof(sockArg)) != 0)
{
errCode = WSAGetLastError();
closesocket(sock);
printf("setsockopt for sock failed. WSAGetLastError() = %ld\n", errCode);
}
if (setsockopt(sock1, SOL_SOCKET, SO_REUSEADDR, (const char*)&sockArg, sizeof(sockArg)) != 0)
{
errCode = WSAGetLastError();
closesocket(sock1);
printf("setsockopt for sock1 failed. WSAGetLastError() = %ld\n", errCode);
}
EventArray[0] = CreateEvent(NULL, FALSE, FALSE, NULL);
WSAResetEvent(EventArray[0]);
errCode = WSAEventSelect(sock, EventArray[0], FD_CONNECT);
if (errCode == SOCKET_ERROR)
{
errCode = WSAGetLastError();
printf("WSAEventSelect for sock failed. WSAGetLastError() = %ld\n", errCode);
}
EventArray[1] = CreateEvent(NULL, FALSE, FALSE, NULL);
WSAResetEvent(EventArray[1]);
errCode = WSAEventSelect(sock1, EventArray[1], FD_CONNECT);
if (errCode == SOCKET_ERROR)
{
errCode = WSAGetLastError();
printf("WSAEventSelect for sock failed. WSAGetLastError() = %ld\n", errCode);
}
errCode = WSAConnect(sock, (PSOCKADDR)&addr, sizeof(addr), NULL, NULL, NULL, NULL);
errCode = WSAGetLastError();
errCode = WSAConnect(sock1, (PSOCKADDR)&addr1, sizeof(addr1), NULL, NULL, NULL, NULL);
errCode = WSAGetLastError();
DWORD index = 0;
while (EventTotal == 0)
{
index = ::WaitForMultipleObjects(2, EventArray, TRUE, WSA_INFINITE);
memset(&NetworkEvents, 0, sizeof(WSANETWORKEVENTS));
errCode = WSAEnumNetworkEvents(sock, EventArray[0], &NetworkEvents);
printf("Socket 0:\n");
checkRecvEvent(NetworkEvents);
memset(&NetworkEvents, 0, sizeof(WSANETWORKEVENTS));
errCode = WSAEnumNetworkEvents(sock1, EventArray[1], &NetworkEvents);
printf("Socket 1:\n");
checkRecvEvent(NetworkEvents);
EventTotal = 2;
}
int num1;
cout << "Test Client => Num1" << endl;
cin >> num1;
for (int i = 0; i < WSA_MAXIMUM_WAIT_EVENTS; i++)
{
if (EventArray[i] != WSA_INVALID_EVENT)
WSACloseEvent(EventArray[i]);
}
closesocket(sock);
closesocket(sock1);
return 0;
}
This server side:
#include "stdafx.h"
#include <iostream>
#include <clocale>
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
WSAData wsaData;
char ConnectIP[255] = "127.0.0.1";
int errCode = 0;
struct sockaddr_in addr,addr1;
SOCKET AcceptSocket;
sockaddr destSockAddr;
int iClientSize = sizeof(sockaddr);
DWORD EventTotal = 0;
WSANETWORKEVENTS NetworkEvents;
WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
if (WSAStartup(0x0202, &wsaData) != 0){
return -1;
}
for (int i = 0; i < WSA_MAXIMUM_WAIT_EVENTS; i++)
EventArray[i] = WSA_INVALID_EVENT;
addr.sin_family = AF_INET;
addr.sin_port = htons(58001);
inet_pton(AF_INET, ConnectIP, &addr.sin_addr);
SOCKET sock = socket(addr.sin_family, SOCK_STREAM, IPPROTO_TCP);
unsigned long sockArg = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&sockArg, sizeof(int)) != 0)
{
errCode = WSAGetLastError();
closesocket(sock);
}
if (::bind(sock, (struct sockaddr *)&addr, sizeof(sockaddr_in)) == SOCKET_ERROR)
{
errCode = WSAGetLastError();
closesocket(sock);
}
addr1.sin_family = AF_INET;
addr1.sin_port = htons(58002);
inet_pton(AF_INET, ConnectIP, &addr1.sin_addr);
SOCKET sock1 = socket(addr1.sin_family, SOCK_STREAM, IPPROTO_TCP);
if (setsockopt(sock1, SOL_SOCKET, SO_REUSEADDR, (const char*)&sockArg, sizeof(int)) != 0)
{
errCode = WSAGetLastError();
closesocket(sock1);
}
if (::bind(sock1, (struct sockaddr *)&addr1, sizeof(sockaddr_in)) == SOCKET_ERROR)
{
errCode = WSAGetLastError();
closesocket(sock1);
}
errCode = ::listen(sock, SOMAXCONN);
errCode = WSAGetLastError();
errCode = ::listen(sock1, SOMAXCONN);
errCode = WSAGetLastError();
EventArray[0] = WSACreateEvent();
WSAResetEvent(EventArray[0]);
WSAEventSelect(sock, EventArray[0], FD_ACCEPT);
EventTotal++;
EventArray[1] = WSACreateEvent();
WSAResetEvent(EventArray[1]);
WSAEventSelect(sock1, EventArray[1], FD_ACCEPT);
DWORD index = ::WaitForMultipleObjects(2, EventArray, TRUE, WSA_INFINITE);
WSAEnumNetworkEvents(sock, EventArray[index - WSA_WAIT_EVENT_0], &NetworkEvents);
if (NetworkEvents.lNetworkEvents & FD_ACCEPT)
{
AcceptSocket = accept(sock, (SOCKADDR*)&destSockAddr, &iClientSize);
closesocket(sock);
sock = AcceptSocket;
AcceptSocket = accept(sock1, (SOCKADDR*)&destSockAddr, &iClientSize);
closesocket(sock1);
sock1 = AcceptSocket;
}
int num1, num2;
num2 = 1;
cout << " Test Server => Num1" << endl;
cin >> num1;
for (int i = 0; i < WSA_MAXIMUM_WAIT_EVENTS; i++)
if (EventArray[i] != NULL)
WSACloseEvent(EventArray[i]);
closesocket(sock);
closesocket(sock1);
return 0;
}
Don't understand what's going on. Help to find the error.
By the way, when I connect to the server using telnet, accept server works correctly.
telnet 127.0.0.1 58001
telnet 127.0.0.1 58002
What I have tried:
When I connect to the server using telnet, accept server works correctly.
telnet 127.0.0.1 58001
telnet 127.0.0.1 58002
When I connect to the server using my client, server not receave WSAEvents, client refuse