Socket not initilizing properly

This is a discussion on Socket not initilizing properly within the Networking/Device Communication forums, part of the General Programming Boards category; Greetings! I am attempting a server program. It compiles fine, but when I run it, my error checking says that ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    8

    Question Socket not initilizing properly

    Greetings!

    I am attempting a server program. It compiles fine, but when I run it, my error checking says that bind and listen aren't working correctly. I used WSAGetLastError on both of them, and I get the same error code: 10038 ("The descriptor is not a socket." -Microsoft website). My code is attatched. Is there something really obvious to this problem?

    Thankyou for your time,

    -IEatGreyFoxes.

    [edit] Using Windows XP, Bloodshed Dev-C++ version 4. [/edit]
    Attached Files Attached Files
    Last edited by IEatGreyFoxes; 05-17-2004 at 01:27 AM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Due to operator precedence
    Code:
    if( testSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) == INVALID_SOCKET)
    is equivalent to(note brackets):
    Code:
    if( testSocket = (socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) == INVALID_SOCKET))
    You want:
    Code:
    if((testSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )) == INVALID_SOCKET)
    Also check the rest of your code for this common mistake.

    P.S Please consider posting some code inline if possible. In this case the call to socket() and surrounding lines would have been sufficient.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    8

    Smile hooray!

    It works! Thankyou!
    But one more thing... If I run it without the cin.get();, then it quits after starting and stopping the server. How can I keep it running?

    Once again, thankyou for your time,

    -IEatGreyFoxes

    [another edit] Wait, wait. What if I put cin.get(); before closesocket() and WSACleanup()? [/another edit]
    Last edited by IEatGreyFoxes; 05-17-2004 at 01:46 AM.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> But one more thing... If I run it without the cin.get();, then it quits after starting and stopping the server. How can I keep it running? <<

    I'm not sure what you mean. Your code is currently only half a server. After the listen() you need a call to accept() which will accept an incoming connection and return a socket representing that connection. Then you can recv() and send() data on that socket.

    So for a simple echo style server you may use this code after the call to listen():
    Code:
    while(TRUE)
    {
    	SOCKET msgsock;
    	struct sockaddr_in from;
    	int    retval;
    	int    fromlen = sizeof(from);
    	char   Buffer[512];
    
    	msgsock = accept(testSocket, (struct sockaddr*) &from, &fromlen);
    	if (msgsock == INVALID_SOCKET)
    	{
                    cout << "accept() error: " << WSAGetLastError() << endl;
                    break;
            }
    
    	cout << "accepted connection from " << inet_ntoa(from.sin_addr) <<
    	    ", port " << htons(from.sin_port) << endl;
                
            retval = recv(msgsock, Buffer, sizeof(Buffer) - 1, 0);
            if (retval == SOCKET_ERROR)
    	{
    		cout << "recv() error: " << WSAGetLastError() << endl;
    		closesocket(msgsock);
    		continue;
            }
    
    	// Make sure buffer is null terminated before use.
    	Buffer[retval] = '\0';
    
    	cout << "Received " << retval << " bytes from client: << endl;
    	cout << Buffer << endl;
    	cout << "Echoing data back to client." << endl;
    
    	retval = send(msgsock, Buffer, sizeof(Buffer), 0);
    	if (retval == SOCKET_ERROR)
    	{
    		cout << "send() error: " << WSAGetLastError() << endl;
    	}
    
    	cout << "Sent " << retval << " bytes back to client." << endl;
    	cout << "Terminating connection." << endl;
    
    	closesocket(msgsock);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 01:51 AM
  3. socket programming in linux
    By crazeinc in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 07:40 PM
  4. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 09:33 AM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM

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