Advertisement

socket

Started by September 25, 2002 11:04 PM
32 comments, last by Tran Minh Quang 22 years, 4 months ago
quote:
Original post by Tran Minh Quang
this is my code:

//Socket descriptor
SOCKET sock;

if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == INVALIDE_SOCKET)
{
perror("connect() failed");
exit(0);
}



For starters try INVALID_SOCKET instead of INVALIDE_SOCKET

You might also want to change the perror call to

perror("socket() failed")

And also note that on Windows NT/Windows 2000, raw socket support requires administrative privileges.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
This is all assuming you''ve called WSAStartup...
Advertisement
This is what I did when making my winsock program
http://home.earthlink.net/~bjonesds12/Programming/Misc/SocketTest/main.cpp"
http://home.earthlink.net/~bjonesds12/Programming/Misc/ClientSocket/main.cpp

Change the IPs defined in both sources to your own, build and exectute the app. It "should" run.

I don't have a signature

[edited by - Brian Jones on September 25, 2002 12:44:59 AM]
I don't have a signature
I can not try it right now because i am using network far from my computer.
But can you tell me when a socket() function failed?. I don;t find any document about it
When a socket() call fails?

Usually it could be because you're passing an invalid parameter, or since you're using winsock, you didn't call WSAStartup().

In order to get what the actual error message was (socket() returns INVALID_SOCKET which is defined as 0xFFFF) you need to call WSAGetLastError();

These are the values that could be returned:

WSANOTINITIALISED A successful WSAStartup must occur before using this function.

WSAENETDOWN The network subsystem or the associated service provider has failed.

WSAEAFNOSUPPORT The specified address family is not supported.

WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.

WSAEMFILE No more socket descriptors are available.

WSAENOBUFS No buffer space is available. The socket cannot be created.

WSAEPROTONOSUPPORT The specified protocol is not supported.

WSAEPROTOTYPE The specified protocol is the wrong type for this socket.

WSAESOCKTNOSUPPORT The specified socket type is not supported in this address family.


[edited by - Brian Jones on September 25, 2002 12:57:16 AM]
I don't have a signature
You must be confuse with my code.
Because above that code I use WSAStartup() and of course my syntax is correct. Those were wrong because i type it to demonstrate.
So what i mean is: in my programming i use correctly what you said (because i''ve follow by the book i read) but i still get error
Advertisement
I''ve download that Internet programming Course. It is exelent. Thanks very much. I will try to use that server you''ve suggested
Is the error returned by WSAGetLastError() 10065?
I don't have a signature
quote:
Original post by Brian Jones
http://home.earthlink.net/~bjonesds12/Programming/Misc/SocketTest/main.cpp

For starters, you''re using the older WinSock library (prefer WinSock2). Header: <winsock2.h> Lib: ws2_32.lib.

Secondly (consequence of first), you''re using WinSockInit. WinSock2 (which, again, is preferred) uses WSAStartup.

Microsoft Windows Sockets API Reference
Yeah, the error is often 10065 and if i don''t confuse a saw something like: 10072(or 79). But 10065 is very often

This topic is closed to new replies.

Advertisement