Thread: Socket question

  1. #1
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738

    Socket question

    I can't create a socket no matter what i do! Must i have an internet connection to do that? .....
    I'm using Windows by the way. Please take it easy, just yesterday i started socket programming!...
    Devoted my life to programming...

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Have you tried any of the sample code from the tutorials?
    No you don't need an internet connection but you do need a network card.

    Basically it's a stepwise process...
    1) Get a socket handle with the socket() call.
    2) Get your local IP address using one of the host lookup functions (eg. gethostbyname())
    3) Set the desired port into the resulting sockaddr struct using htons(port).
    4) Bind the socket using bind()
    5) listen for connections with listen() or wait for news with recvfrom() (for udp)

    You will definately find UDP communications a whole lot easier for your first project. Your best bet is to do the steps one at a time testing for success after each function call. Once the first is working, move on to the second and so on...

    Hope that helps...

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Thanks "CommonTater". My problem is that socket() always returns -1! I do have a network card so with what you said i can't see where's the problem. Maybe i'm passing wrongs parameters to the function? I do this:
    Code:
    sID = socket(AF_INET , SOCK_DGRAM, 0);
    and "sID" is always -1!!!
    Devoted my life to programming...

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I think i found the problem. I didn't know i had to initialize winsock before using it!
    Devoted my life to programming...

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    Thanks "CommonTater". My problem is that socket() always returns -1! I do have a network card so with what you said i can't see where's the problem. Maybe i'm passing wrongs parameters to the function? I do this:
    Code:
    sID = socket(AF_INET , SOCK_DGRAM, 0);
    and "sID" is always -1!!!
    If you're using SOCK_DGRAM the protocal (3rd parameter) should be set to IPPROTO_UDP (17), otherwise you may not be able to bind the socket to a port.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    I think i found the problem. I didn't know i had to initialize winsock before using it!
    Ok... one step closer ...

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    You need WSAStartup().
    Post your code if the problem isn't resolved.
    Check out MSDN.

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Stop bumping old thread

    CLOSED

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Question
    By BENCHMARKMAN in forum C Programming
    Replies: 15
    Last Post: 03-12-2008, 09:57 PM
  2. Socket class OOD
    By lord mazdak in forum C++ Programming
    Replies: 6
    Last Post: 12-17-2005, 01:11 AM
  3. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 02:51 AM
  4. socket programming in linux
    By crazeinc in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 07:40 PM
  5. Simple Socket Question
    By SourceCode in forum Linux Programming
    Replies: 2
    Last Post: 06-22-2003, 09:20 PM