CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Socktn error

  1. #1
    Join Date
    Feb 2016
    Posts
    1

    Socktn error

    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 :
    Code:
    #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);
    }
    
    // Disable blocking send/recv calls.
    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);
    	}
    
    	//bool sb = Nonblocking(sock);
    	//sb = Nonblocking(sock1);
    		
    	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);
    	}
    	//memset(&addr1, 0, sizeof(sockaddr_in));
    
    	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:

    Code:
    #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;
    	//char errStr[1000];
    	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);
    	//addr.sin_addr.s_addr = htonl(INADDR_ANY);
    	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);
    	//addr1.sin_addr.s_addr = htonl(INADDR_ANY);
    	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);
    	}
    	
    	//memset(&addr, 0, sizeof(sockaddr_in));
    	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
    Last edited by rsergeya; February 10th, 2016 at 12:46 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Socktn error

    When posting, please use code tags. Go Advanced, select the formatted code and click '#' - although as you only have made 3 posts, I'm not sure you can edit them yet.

    To repost, just do a reply then Select Go Advanced, paste the code for the client, make sure it is all selected and then click '#'. This will add the required code tags. do the same for the server code so that you have two bocks of code each with their own set of code tags.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Socktn error

    Please provide Which call is erroring out, and why don't you copy and paste the results of
    running your program into code tags here.
    Code:
    printf("WSAEventSelect for sock failed. WSAGetLastError() = %ld\n", errCode);
    The results of this sort of call will give us some hints.

    I see your talking about an error, but not which one.
    ahoodin
    To keep the plot moving, that's why.

  4. #4
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Socktn error

    Most of us have full time jobs in addition to helping you. It would be good if You would provide more details on this post. Also good in the future if in future posts you provide us with the information we need so that we don't have to resurrect your project on our own computers unless we absolutely have to so this can be a knowledge transfer rather than a chore.

    Best Regards and Thanks,
    ahoodin
    To keep the plot moving, that's why.

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Socktn error

    Quote Originally Posted by rsergeya View Post
    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.
    And what is the error?
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured