Wrong accept() call?

This is a discussion on Wrong accept() call? within the Networking/Device Communication forums, part of the General Programming Boards category; I have made a server and a client.At first, I made a very simple server and client, the connection worked ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    Wrong accept() call?

    I have made a server and a client.At first, I made a very simple server and client, the connection worked - client connected to server without any problems.But now I rewrote the server to make it better, but it doesnt connect now.The code is almost the same as before.I havent changed the client, im testing both server and client on the same computer.

    ------------SERVER
    Code:
    #include <winsock.h>
    #include <iostream>
    
    using namespace std;
    
    WSADATA wsaData;
    SOCKET Socket;
    SOCKADDR_IN SockAddr;
    
    SOCKET SetUpClient (const char* Clip, int nPort);
    void AcceptClient(SOCKET ListeningSocket);
    SOCKET Test(SOCKET ListeningSocket);
    int nPort = 50;
    
    int main (const char* Clip, int nPort) {
        printf("Initializing WSA...\n");
        if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
            printf("WSA Initialization failed."); 
        } else { 
            printf("->Succeeded.\n\n");
            SOCKET ListeningSocket = SetUpClient(Clip, htons(nPort));
            if ((ListeningSocket == 0) || (ListeningSocket == INVALID_SOCKET)) {
                printf("Fatal error: Socket couldn't be initialized.");
                system("PAUSE");
                return 0;
            } else { 
                printf("Waiting for connections...\n\n");
                while (1) {
                    AcceptClient(ListeningSocket);
                    printf("Acceptor restarting...\n");
                }
            }
        }
    }
    
    SOCKET SetUpClient (const char* Clip, int nPort) {
        printf("Creating socket...\n");
        Socket = socket(AF_INET, SOCK_STREAM, 0);
        if (Socket == INVALID_SOCKET) {
            printf("Socket creation failed.");
        } else {
            printf("->Succeeded.\n\n");
            SockAddr.sin_port = nPort;  
            SockAddr.sin_family = AF_INET; 
            SockAddr.sin_addr.S_un.S_addr = INADDR_ANY;
    
            printf("Binding socket...\n"); 
            if (bind(Socket, (SOCKADDR *)(&SockAddr), sizeof(SOCKADDR_IN)) == SOCKET_ERROR) { 
                printf("Attempt to bind failed with error code: ");
                cout << GetLastError() << endl;
                return 0;
            } else {
                printf("->Succeeded.\n\n");
                listen(Socket, SOMAXCONN);
                printf("Server initialized at port 50.\n\n");
                return Socket;
            }
        }
        return INVALID_SOCKET;  
    }
    
    void AcceptClient(SOCKET ListeningSocket) {
        SOCKADDR_IN sinRemote;
        SOCKET AcceptSock;
        int nAddrSize = sizeof(sinRemote);
    
        while (1) {
            SOCKET AcceptSock = accept(ListeningSocket, (SOCKADDR *)(&sinRemote),
                    &nAddrSize);
            /*AcceptSock == SOCKET_ERROR;
            while (AcceptSock == SOCKET_ERROR) {
                AcceptSock = accept(ListeningSocket, (SOCKADDR *)(&sinRemote),
                    &nAddrSize);
                cout << "test";
            }
            Socket = AcceptSock;*/
            if (AcceptSock != INVALID_SOCKET) {
                cout << "Accepted connection from " <<
                        inet_ntoa(sinRemote.sin_addr) << ":" <<
                        ntohs(sinRemote.sin_port) << "." <<
                        endl;
                Socket = AcceptSock;
                DWORD nThreadID;
                //CreateThread(0, 0, EchoHandler, (void*)sd, 0, &nThreadID);
            } else {
                cout << "Fatal error: error code: " << GetLastError();
                system("PAUSE");
                return;
            }
        }
    }
    As you can see, ive been trying things out.I have checked if listen() worked, and it did.

    ------------CLIENT
    Code:
    #include <winsock.h>
    #include <iostream>
    
    using namespace std;
    
    WSADATA wsaData;
    
    int main () {
        SOCKADDR_IN SockAddr; 
        printf("Initializing WSA...\n");
        if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
            printf("WSA Initialization failed."); 
        } else { 
            printf("->Succeeded.\n\n");
        }
    
        printf("Creating socket...\n");
        SOCKET Socket;
        Socket = socket(AF_INET, SOCK_STREAM, 0);
        if (Socket == INVALID_SOCKET) {
            printf("Socket creation failed.");
        } else {
            printf("->Succeeded.\n\n");
        }
        int port = 50;
        SockAddr.sin_port = htons(port);
    
        SockAddr.sin_family = AF_INET; 
    
        SockAddr.sin_addr.S_un.S_un_b.s_b1 = 127; 
        SockAddr.sin_addr.S_un.S_un_b.s_b2 = 0; 
        SockAddr.sin_addr.S_un.S_un_b.s_b3 = 0; 
        SockAddr.sin_addr.S_un.S_un_b.s_b4 = 1;
        //SockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 
    
        printf("Connecting to server...\n");
        if (connect(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) != 0) {
            int test = GetLastError();
            printf("Failed to establish connection with server.\n\n");
            printf("Error code:\n");
            if (test == 10061) {
                printf("WSAECONNREFUSED: Connection was refused by the server, server could be offline.");
            } else if (test == 10058) {
                printf("WSAESHUTDOWN: Shutdown commenced, cannot connect.");
            } else if (test == 10060) {
                printf("WSAETIMEDOUT: Timeout in connecting to server.");
            } else {
                cout << "Unknown error code: " << test;
            }
        } else {
            printf("->Connection succeeded on 127.0.0.1.\n");
        }
        printf("\n\n\n*Test program ended for now*\n");
        system("PAUSE");
    }
    So why cant the client connect to the server, it always gives me WSAECONNREFUSED?
    Last edited by Nephiroth; 02-12-2006 at 01:38 AM.

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Provo, UT, USA, Earth
    Posts
    2
    Client:
    int port = 50;
    SockAddr.sin_port = htons(port);

    Server:
    int nPort = 50;
    SockAddr.sin_port = nPort;

    The ports are not the same. Let me know if that works for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 05-19-2009, 09:17 AM
  2. Interrupted System Call
    By Seiki in forum C Programming
    Replies: 2
    Last Post: 01-28-2009, 11:55 PM
  3. asynchronous and synchronous call between apartments
    By George2 in forum Windows Programming
    Replies: 0
    Last Post: 04-05-2008, 02:22 AM
  4. Bjarne's member function binding sample is wrong?
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 03-11-2008, 05:05 AM
  5. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21