?
Solved

Problem Creating Socket In Winsock

Posted on 2003-02-27
9
Medium Priority
?
215 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

571 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