Thread View: borland.public.cppbuilder.internet.socket
8 messages
8 total messages
Started by Eduardo Jauch
Wed, 21 Feb 2007 15:07
Using Winsock 2.0
Author: Eduardo Jauch
Date: Wed, 21 Feb 2007 15:07
Date: Wed, 21 Feb 2007 15:07
37 lines
1538 bytes
1538 bytes
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 :)
Re: Using Winsock 2.0
Author: PaoloItaly
Date: Wed, 21 Feb 2007 15:20
Date: Wed, 21 Feb 2007 15:20
132 lines
4664 bytes
4664 bytes
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;
}
Re: Using Winsock 2.0
Author: Eduardo Jauch
Date: Wed, 21 Feb 2007 15:21
Date: Wed, 21 Feb 2007 15:21
9 lines
1126 bytes
1126 bytes
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! :)
Re: Using Winsock 2.0
Author: Eduardo Jauch
Date: Wed, 21 Feb 2007 15:23
Date: Wed, 21 Feb 2007 15:23
140 lines
5042 bytes
5042 bytes
PaoloItaly escreveu:
> 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!!! :)
Re: Using Winsock 2.0
Author: Remy Lebeau (TeamB)
Date: Wed, 21 Feb 2007 18:28
Date: Wed, 21 Feb 2007 18:28
21 lines
1427 bytes
1427 bytes
"Eduardo Jauch" <eduardo.jauch@gmail.com> wrote in message news:45dc601b$1@newsgroups.borland.com... > int sockfd; socket() returns a SOCKET handle, not an int. > 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. > But always get sockfd with a -1 value and the ESAGetLastError > with the value 10093. 10093 is WSANOTINITIALISED. Gambit
Re: Using Winsock 2.0
Author: Remy Lebeau (TeamB)
Date: Wed, 21 Feb 2007 18:29
Date: Wed, 21 Feb 2007 18:29
12 lines
1177 bytes
1177 bytes
"Eduardo Jauch" <eduardo.jauch@gmail.com> wrote in message news:45dc63c7$1@newsgroups.borland.com... > HeY!!! > > Thanks Paolo!!! :) For future reference, please trim your replies. Do not overquote. Gambit
Re: Using Winsock 2.0
Author: Eduardo Jauch
Date: Thu, 22 Feb 2007 00:48
Date: Thu, 22 Feb 2007 00:48
6 lines
1146 bytes
1146 bytes
Remy Lebeau (TeamB) escreveu: > For future reference, please trim your replies. Do not overquote. > Sorry. :) I'll remember that :)
Re: Using Winsock 2.0
Author: Eduardo Jauch
Date: Thu, 22 Feb 2007 00:49
Date: Thu, 22 Feb 2007 00:49
17 lines
1286 bytes
1286 bytes
Remy Lebeau (TeamB) escreveu: > socket() returns a SOCKET handle, not an int. Ok :) > > 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. :) > 10093 is WSANOTINITIALISED. > Now I know ;) Thanks :)
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads