Question:
socket programing in c in windows enviroment?
Amit A
2008-10-21 08:11:01 UTC
hello

i have a problem

actually my code is

#include
#include
#include

int main()
{ int i=socket(AF_INET,SOCK_STREAM,0);
printf("%d",i);
getch();
}

i got the value of i = -1

does it mean socket function called successfully
or it mean it return an error

i know in unix it is an error

but i dun know in windows

socket programing

ALERT:

i have inlucded all lib files so there is no such error


help me and if it is a error then cn u tell me a valid reason for the same ?
thanx in advance
Three answers:
Rah-Mon Heur
2008-10-21 12:14:12 UTC
The windows socket stack must first be initialized by a call to WSAStartup() first with the version of the windows lib you want to use (normally 2.0) Look it up for exact syntax.



If you've done that, try using WSAGetLastError() to get a explication of why the call failed.



good luck!
Syx
2008-10-21 15:20:09 UTC
-1 indicates an error.

If it was opened correctly, you would have a socket descriptor returned by the function.



http://beej.us/guide/bgnet/examples/client.c

and

http://beej.us/guide/bgnet/examples/server.c

show code examples of the client and server sides of a socket connection.
Germann A
2008-10-21 15:50:05 UTC
Is your server socket listening?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...