?
Solved

Problem Creating Socket In Winsock

Posted on 2003-02-27
9
Medium Priority
?
211 Views
Last Modified: 2010-04-15
I have a routine which is attempting to make a socket connection.  The part which is erroring out is as follows :

s = socket(AF_INET, SOCK_STREAM, 0);
  if (s == INVALID_SOCKET)
    return INVALID_SOCKET;

The problem is, 100% of the time, it's coming up with an invalid socket.  I've tried including both winsock.h and winsock2.h.  I've tried using both AF_INET and PF_INET.  I haven't seen anything out there which would cause socket creation to fail.

So I guess my question is this :

What could cause the socket() call to fail?  Is there a way to see the exact problem that's causing it to fail?

0
Comment
Question by:Hypostatize
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 2

Author Comment

by:Hypostatize
ID: 8035478
Okay, found WSAGetLastError, I'll let you know what it returns.  But I'm still curious what could cause a socket create to fail.
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 8035541
Have you called 'WSAStartup()'?
0
 
LVL 2

Author Comment

by:Hypostatize
ID: 8035555
Yes, and it succeeds.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 8035623
Have you tried 'WSAGetLastError()' already?
0
 
LVL 2

Author Comment

by:Hypostatize
ID: 8035669
Yes, and it succeeds.
0
 
LVL 2

Author Comment

by:Hypostatize
ID: 8035679
Sorry, never mind the last "Yes".  I'll be trying WSAGetLastError shortly.
0
 

Expert Comment

by:Reme
ID: 8035707
   if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
      perror("socket");
      exit(1);
    }
0
 

Expert Comment

by:Reme
ID: 8035715
perror will give you more detailed explanation to why you are receiving an error.
0
 
LVL 2

Author Comment

by:Hypostatize
ID: 8039308
I finally got back the WSAGetLastError, and sure enough, it said that the WSAStartup failed.  I reviewed the code, and found the loophole.  It works great now.  Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question