Jump to content
  • Advertisement
Sign in to follow this  
Kilguri

Mysterious Socket Error

This topic is 4821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First of all, allow me to say this is by no means an expert problem, so if this is not the place to post it forgive me, but it seemed much more relevant here than in the beginners section. What I am trying to do is simple enough, I'm trying to get the hang of socket programming in C, with the end goal is being able to send a single bit of information from one computer to another. I've read through Beej's guide to sockets about half a dozen times (trying to make sure I understand as much of it as I can, the rest time will do) up to the point where the definitions are put aside and the actual functions begin, and that's where my problems begin aswell. Now with the introduction out of the day, here's my problem. Whenever I call socket, it returns an error (-1), however errno and perror state there is no error. I am thinking it's one of those head-wall cases where a single misplaced character puts you off for hours, but that's just my non-expert analysis. Either way, here is the code (not much, but getting there):
#include <winsock.h>
#include <dos.h>

int main( int argc, char* argv[])
{
    struct sockaddr_in scAdr; 
    int sockfd, sError;
    
    scAdr.sin_family = AF_INET;        
    scAdr.sin_port = htons(5432);
    scAdr.sin_addr.s_addr = inet_addr("127.0.0.1");
    memset(&(scAdr.sin_zero), '\0', 8);

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
    printf("Socket Result: %d\n",sockfd);
    printf("Errno: %d\n",errno);
    perror("socket");
    
    sError = bind(sockfd, (struct sockaddr*)&scAdr, sizeof(struct sockaddr));
    
    printf("Bind Result: %d\n",sError);
    printf("Errno: %d\n",errno);
    perror("socket");
                
    system("PAUSE");
    return 0;
}
For which the output would be:
Socket Result: -1
Errno: 0
socket: No error
Bind Result: -1
Errno: 0
socket: No error
Obviously something is wrong here, most likely due to my error. Either that or I just descovered the mythical -1 port. Anyhow, thanks in advance :)

Share this post


Link to post
Share on other sites
Advertisement
Here's my guess:

1) You're using some flavor of windows.
2) You're not calling WSAStartup before trying to create a socket.


// Add this at the beginning of main()
#if defined(WIN32)
WSADATA wsaData;
WSAStartup( MAKEWORD(2,2), &wsaData );
#endif



Also make sure that the first header your program includes (or the first header your precompiled header includes) is <winsock2.h>, and link with ws2_32.lib.

Share this post


Link to post
Share on other sites
*smacks forehead*

You're so right is frightening. I took note to initialize WSA but complete forgot abou that. I feel a little embarassed right now, typical error on my part :p

And a little off, but while on the subject, could you possibly educate me on what is the word that is passed to WSAStartup? I've seen several variations of it, 0,1; 1,1; and now 2,2; Just trying to make some sense in it :)

Thanks again

Share this post


Link to post
Share on other sites
Since this is winsock you should also be checking the error via WSAGetLastError() instead of errno. perror doesn't work either.

There are a few other minor difference between socket programming on Windows vs Linux/Unix. They can mostly be #defined away. For example don't try to use close() or ioctl() on a socket under winsock - you have to use closesocket and ioctlsocket.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!