BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using Winsock 2.0

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket)
View previous topic :: View next topic  
Author Message
Eduardo Jauch
Guest





PostPosted: Wed Feb 21, 2007 9:07 pm    Post subject: Using Winsock 2.0 Reply with quote



Hi!

I'm trying to do some code for use winsock.

I write this:

//---------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>
#include <winsock2.h>

#pragma argsused

int main(int argc, char* argv[])
{
int sockfd;

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd == INVALID_SOCKET)
printf("Error: %d", WSAGetLastError());
else
printf("OK");

getch();

return 0;
}
//---------------------------------------------------------------------------


But always get sockfd with a -1 value and the ESAGetLastError with the
value 10093.

Anyone could help me to solve this?

Thanks Smile
Back to top
PaoloItaly
Guest





PostPosted: Wed Feb 21, 2007 9:20 pm    Post subject: Re: Using Winsock 2.0 Reply with quote



int myWSAErrorCode(int code)
{
switch(code)
{
case WSANOTINITIALISED:
myMessageBox("A successful WSAStartup must occur before using this
function");
break;

case WSAENETDOWN:
myMessageBox("The network subsystem has failed");
break;

case WSAEADDRINUSE:
myMessageBox("The specified address is already in use");
break;

case WSAEINTR:
myMessageBox("The (blocking) call was canceled through
WSACancelBlockingCall");
break;

case WSAEINPROGRESS:
myMessageBox("A blocking Windows Sockets 1.1 call is in progress,\n
or the service provider is still processing a callback function");
break;

case WSAEALREADY:
myMessageBox("A nonblocking connect call is in progress on the
specified socket.\nNote In order to preserve backward compatibility, this
error is reported as WSAEINVAL\nto Windows Sockets 1.1 applications that
link to either WINSOCK.DLL or WSOCK32.DLL.");
break;

case WSAEADDRNOTAVAIL:
myMessageBox("The specified address is not available from the local
machine");
break;

case WSAEAFNOSUPPORT:
myMessageBox("Addresses in the specified family cannot be used with
this socket");
break;

case WSAECONNREFUSED:
myMessageBox("The attempt to connect was forcefully rejected");
break;

case WSAEFAULT:
myMessageBox("The name or the namelen argument is not a valid part
of\nthe user address space, the namelen argument is too small,\nor the name
argument contains incorrect address format for the associated address
family");
break;

case WSAEINVAL:
myMessageBox("The parameter s is a listening socket, or the
destination address specified\nis not consistent with that of the
constrained group the socket belongs to");
break;

case WSAEISCONN:
myMessageBox("The socket is already connected (connection-oriented
sockets only)");
break;

case WSAENETUNREACH:
myMessageBox("The network cannot be reached from this host at this
time");
break;

case WSAENOBUFS:
myMessageBox("No buffer space is available. The socket cannot be
connected");
break;

case WSAENOTSOCK:
myMessageBox("The descriptor is not a socket");
break;

case WSAETIMEDOUT:
myMessageBox("Attempt to connect timed out without establishing a
connection");
break;

case WSAEWOULDBLOCK:
myMessageBox("The socket is marked as nonblocking and the
connection cannot be completed immediately.\nIt is possible to select the
socket while it is connecting by selecting it for writing");
break;

case WSAEACCES:
myMessageBox("Attempt to connect datagram socket to broadcast
address failed because\nsetsockopt SO_BROADCAST is not enabled");
break;

default:
code = 0;
break;
}

if(code)
WSASetLastError(code);

return code;
}

SOCKET openSocket(unsigned short port, char ipAddr[], struct sockaddr_in
*addr)
{
WORD wVersionRequested;
WSADATA wsaData;
SOCKET tempSock = INVALID_SOCKET;

::WSASetLastError(::WSAGetLastError());

wVersionRequested = MAKEWORD(2, 0);
if(::WSAStartup(wVersionRequested, &wsaData) == 0)
{
tempSock = ::socket(AF_INET, SOCK_STREAM, 0);
if(tempSock != INVALID_SOCKET)
{
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
addr->sin_addr.s_addr = inet_addr(ipAddr);
}

myWSAErrorCode(::WSAGetLastError());
}

return tempSock;
}
Back to top
Eduardo Jauch
Guest





PostPosted: Wed Feb 21, 2007 9:21 pm    Post subject: Re: Using Winsock 2.0 Reply with quote



Never mind...

I found the problem...
Not like in linux, on windows the socket libriry must be initialized
using WSAStartup function...

The help provides a good exemple :)

Thanks! Smile
Back to top
Eduardo Jauch
Guest





PostPosted: Wed Feb 21, 2007 9:23 pm    Post subject: Re: Using Winsock 2.0 Reply with quote

PaoloItaly escreveu:
Quote:
int myWSAErrorCode(int code)
{
switch(code)
{
case WSANOTINITIALISED:
myMessageBox("A successful WSAStartup must occur before using this
function");
break;

case WSAENETDOWN:
myMessageBox("The network subsystem has failed");
break;

case WSAEADDRINUSE:
myMessageBox("The specified address is already in use");
break;

case WSAEINTR:
myMessageBox("The (blocking) call was canceled through
WSACancelBlockingCall");
break;

case WSAEINPROGRESS:
myMessageBox("A blocking Windows Sockets 1.1 call is in progress,\n
or the service provider is still processing a callback function");
break;

case WSAEALREADY:
myMessageBox("A nonblocking connect call is in progress on the
specified socket.\nNote In order to preserve backward compatibility, this
error is reported as WSAEINVAL\nto Windows Sockets 1.1 applications that
link to either WINSOCK.DLL or WSOCK32.DLL.");
break;

case WSAEADDRNOTAVAIL:
myMessageBox("The specified address is not available from the local
machine");
break;

case WSAEAFNOSUPPORT:
myMessageBox("Addresses in the specified family cannot be used with
this socket");
break;

case WSAECONNREFUSED:
myMessageBox("The attempt to connect was forcefully rejected");
break;

case WSAEFAULT:
myMessageBox("The name or the namelen argument is not a valid part
of\nthe user address space, the namelen argument is too small,\nor the name
argument contains incorrect address format for the associated address
family");
break;

case WSAEINVAL:
myMessageBox("The parameter s is a listening socket, or the
destination address specified\nis not consistent with that of the
constrained group the socket belongs to");
break;

case WSAEISCONN:
myMessageBox("The socket is already connected (connection-oriented
sockets only)");
break;

case WSAENETUNREACH:
myMessageBox("The network cannot be reached from this host at this
time");
break;

case WSAENOBUFS:
myMessageBox("No buffer space is available. The socket cannot be
connected");
break;

case WSAENOTSOCK:
myMessageBox("The descriptor is not a socket");
break;

case WSAETIMEDOUT:
myMessageBox("Attempt to connect timed out without establishing a
connection");
break;

case WSAEWOULDBLOCK:
myMessageBox("The socket is marked as nonblocking and the
connection cannot be completed immediately.\nIt is possible to select the
socket while it is connecting by selecting it for writing");
break;

case WSAEACCES:
myMessageBox("Attempt to connect datagram socket to broadcast
address failed because\nsetsockopt SO_BROADCAST is not enabled");
break;

default:
code = 0;
break;
}

if(code)
WSASetLastError(code);

return code;
}

SOCKET openSocket(unsigned short port, char ipAddr[], struct sockaddr_in
*addr)
{
WORD wVersionRequested;
WSADATA wsaData;
SOCKET tempSock = INVALID_SOCKET;

::WSASetLastError(::WSAGetLastError());

wVersionRequested = MAKEWORD(2, 0);
if(::WSAStartup(wVersionRequested, &wsaData) == 0)
{
tempSock = ::socket(AF_INET, SOCK_STREAM, 0);
if(tempSock != INVALID_SOCKET)
{
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
addr->sin_addr.s_addr = inet_addr(ipAddr);
}

myWSAErrorCode(::WSAGetLastError());
}

return tempSock;
}




HeY!!!

Thanks Paolo!!! Smile
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Feb 22, 2007 12:28 am    Post subject: Re: Using Winsock 2.0 Reply with quote

"Eduardo Jauch" <eduardo.jauch (AT) gmail (DOT) com> wrote in message
news:45dc601b$1 (AT) newsgroups (DOT) borland.com...

Quote:
int sockfd;

socket() returns a SOCKET handle, not an int.

Quote:
sockfd = socket(AF_INET, SOCK_STREAM, 0);

You did not call WSAStartup() before calling socket(). You must do
so, in order to specify the version of WinSock that you want to use,
and to validate that it is actually available.

Quote:
But always get sockfd with a -1 value and the ESAGetLastError
with the value 10093.

10093 is WSANOTINITIALISED.


Gambit
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Feb 22, 2007 12:29 am    Post subject: Re: Using Winsock 2.0 Reply with quote

"Eduardo Jauch" <eduardo.jauch (AT) gmail (DOT) com> wrote in message
news:45dc63c7$1 (AT) newsgroups (DOT) borland.com...

Quote:
HeY!!!

Thanks Paolo!!! Smile

For future reference, please trim your replies. Do not overquote.


Gambit
Back to top
Eduardo Jauch
Guest





PostPosted: Thu Feb 22, 2007 6:48 am    Post subject: Re: Using Winsock 2.0 Reply with quote

Remy Lebeau (TeamB) escreveu:
Quote:
For future reference, please trim your replies. Do not overquote.


Sorry. Smile
I'll remember that Smile
Back to top
Eduardo Jauch
Guest





PostPosted: Thu Feb 22, 2007 6:49 am    Post subject: Re: Using Winsock 2.0 Reply with quote

Remy Lebeau (TeamB) escreveu:
Quote:
socket() returns a SOCKET handle, not an int.

Ok Smile
Quote:

You did not call WSAStartup() before calling socket(). You must do
so, in order to specify the version of WinSock that you want to use,
and to validate that it is actually available.

Yes. That was my fault. :)

Quote:
10093 is WSANOTINITIALISED.


Now I know ;)

Thanks Smile
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.