?
Solved

error calling socket()

Posted on 2004-09-06
4
Medium Priority
?
1,828 Views
Last Modified: 2008-02-26
My code is as follows:

      /* Create the socket */
      s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
      if(s == INVALID_SOCKET)
      {
            FILE *f = fopen("c:\\temp\\lsp.txt","a");
            fprintf(f,"socket() error: %d\n",WSAGetLastError());
            fclose(f);
            return 0;
      }

The error number returned by WSAGetLastError() is 183.  I looked in the winsock2.h header file, and I cant find any sort of reference to 183.  I'm hoping someone here can help me figure out this error.
0
Comment
Question by:lwinkenb
[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
4 Comments
 
LVL 15

Expert Comment

by:efn
ID: 11987955
First, you should capture the value from WSAGetLastError before calling fopen, because fopen can change the value.  Just save the value in a variable and use the variable after fopen to put out the value.

Second, do you have a call to WSAStartup before the code you showed?  It's necessary to initialize the library before you try to create a socket.

--efn
0
 
LVL 86

Expert Comment

by:jkr
ID: 11989644
Do you call 'WSAStartup()' before calling 'socket()'?

WORD wVersionRequested;
WSADATA wsaData;
int err;
 
wVersionRequested = MAKEWORD( 2, 2 );
 
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
    /* Tell the user that we could not find a usable */
    /* WinSock DLL.                                  */
    return;
}


Apart from that, try

     /* Create the socket */
    s = socket(AF_INET,SOCK_STREAM,0);
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 11991284
I was already calling WSAStartup(), so that isnt the problem.  I moved the WSAGetLastError() to a new line before the called to fopen(), and now it is giving me a legitamate error.  The error is WSAEPROVIDERFAILEDINIT.

I'm not sure exactly why I'm getting this error, but I think it's due to the fact that Im making the socket() call in DllMain() of a Layered Service Provider DLL.  
0
 
LVL 1

Accepted Solution

by:
jonathan6587 earned 2000 total points
ID: 12004973
lwinkenb

Error 183 is:

"Cannot create a file when that file already exists."

Probably something to do with your "lsp.txt"

I suspect it is your use of fopen, specifically "a"

"a" opens the file for appending, but does not remove the end of file marker before writing new data -- thus if this is any type of loop or you've appended the file previously without closing the fclosing the file -- you would probably get an error like this.  I suggest you use "a+" -- it removes the end of file marker before new data is written and restores the marker after your done writing.


You can use the FormatMessage function to convert error number to the error message.

I'm not sure you care about this now, but it's good to know.

Also, you can type "net helpmsg 183" on the command line and it will convert the error number to a meaningful message.

Cheers,

Jonathan

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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 nested-loops in the C programming language.
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.
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