Thread: Getting a socket error

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Getting a socket error

    Hi!
    A strange error happens in my app.
    Here is the initial code:
    Code:
    #include <iostream>
    #include <winsock.h>
    #include <stdlib.h>
    #include <errno.h>
    
    using namespace std;
    
    #define PORT 6000
    
    int main(int argc, char** argv){
    
       int server, client;
       struct sockaddr_in local;
       struct sockaddr_in remote;
       
       server = socket(AF_INET, SOCK_STREAM, 0);
       if( server == -1 ){
          perror("socket");
          exit(1);
       }
       // the rest of the code is not included
    }
    The if is always true, but perror prints: "socket: No Error".
    What is the problem?
    Thanks any help or pointer to solution!
    Nothing more to tell about me...
    Happy day =)

  2. #2
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Since you're using windows sockets you have to call the WSAStartup function before you can perform any socket operations succesfully. That might be it. There's an example of how to use it on the MSDN site at the bottom of this page.

    http://msdn.microsoft.com/library/de...astartup_2.asp

    Might be an idea to read through the docs on the MSDN site about windows sockets, they're really useful and where I learn't most of my winsock knowledge.

    Also, it might be an idea to use winsock2.h instead as it's a lot newer. <edit> I think you may *have* to use winsock2.h when using WSAStartup, but I'm not sure</edit>

    Hope that helps.

    -dan
    Last edited by codec; 01-15-2004 at 11:38 PM.

  3. #3
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Also, although i think your code will work anyway, but when using Windows sockets as apposed to nix sockets define your socket as a SOCKET data type instead of int. As always with Microsoft, they like inventing special data types for just about everything. Have to be different....

    -dan

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Really thanks! I will try it!
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM