Thread: [Dev-C++] How to use WSAGetLastError?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Vlada D. <dj...@im...> - 2003-01-27 12:42:47
|
Hi.
I'm trying to create a WinSock app. I created a socket, but bind() returns
an error, so I tried WSAGetLastError to get error code. Here's the code:
case ID_SOCK_OPEN:
{
OpenSocket(PORT);//in this function I create socket
int error=WSAGetLastError();
char buffer[100];
itoa(error,buffer,2);
MessageBox(NULL,buffer,"Error Code",MB_OK);
}
break;
In message box I get 0, so I suppose something is wrong about this. Can
anybode tell me better way to get error code?
Another question: Here's the code for OpenSocket function. Can somebody
figure out why call to bind() fails?
BOOL OpenSocket(int port)
{
if(sock=socket(AF_INET,SOCK_STREAM,0)==-1)
{
MessageBox(NULL,"Could not create
socket","Error",MB_OK|MB_ICONSTOP);
return FALSE;
}
server.sin_family=AF_INET;
server.sin_port=htons(port);
server.sin_addr.s_addr=INADDR_ANY;
if(bind(sock,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1)
{
MessageBox(NULL,"Bind error","Error",MB_OK|MB_ICONSTOP);
return FALSE;
}
if(listen(sock,3)==-1)
{
MessageBox(NULL,"Listen error","Error",MB_OK|MB_ICONSTOP);
return FALSE;
}
else
{
MessageBox(NULL,"Socket opened","Success",MB_OK);
return TRUE;
}
}
Thanks in advance.
------------
Vlada
|
|
From: <qa...@ho...> - 2003-01-27 15:36:21
|
The only difference in your code compared to mine is this.
Your code:
if(bind(sock,(struct sockaddr*)&server,sizeof(struct sockaddr))=3D=3D-1)
My code:
if(bind(sock,(struct sockaddr*)&server,sizeof(server))=3D=3D-1)
Hope it helps...
/J
*********** REPLY SEPARATOR ***********
On 2003-01-27 at 13:40 Vlada Djurovic wrote:
>Hi.
>I'm trying to create a WinSock app. I created a socket, but bind() returns
>an error, so I tried WSAGetLastError to get error code. Here's the code:
>
>case ID_SOCK_OPEN:
> {
> OpenSocket(PORT);//in this function I create socket
> int error=3DWSAGetLastError();
> char buffer[100];
> itoa(error,buffer,2);
> MessageBox(NULL,buffer,"Error Code",MB_OK);
> }
> break;
>In message box I get 0, so I suppose something is wrong about this. Can
>anybode tell me better way to get error code?
>Another question: Here's the code for OpenSocket function. Can somebody
>figure out why call to bind() fails?
>
>BOOL OpenSocket(int port)
>{
> if(sock=3Dsocket(AF_INET,SOCK_STREAM,0)=3D=3D-1)
> {
> MessageBox(NULL,"Could not create
>socket","Error",MB_OK|MB_ICONSTOP);
> return FALSE;
> }
> server.sin_family=3DAF_INET;
> server.sin_port=3Dhtons(port);
> server.sin_addr.s_addr=3DINADDR_ANY;
>
> if(bind(sock,(struct sockaddr*)&server,sizeof(struct=
sockaddr))=3D=3D-1)
> {
> MessageBox(NULL,"Bind error","Error",MB_OK|MB_ICONSTOP);
> return FALSE;
> }
>
> if(listen(sock,3)=3D=3D-1)
> {
> MessageBox(NULL,"Listen error","Error",MB_OK|MB_ICONSTOP);
> return FALSE;
> }
> else
> {
> MessageBox(NULL,"Socket opened","Success",MB_OK);
> return TRUE;
> }
>}
>
>Thanks in advance.
>------------
>Vlada
>
>
>
>-------------------------------------------------------
>This SF.NET email is sponsored by:
>SourceForge Enterprise Edition + IBM + LinuxWorld =3D Something 2 See!
>http://www.vasoftware.com
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
L
|
|
From: Vlada D. <dj...@im...> - 2003-01-28 07:48:14
|
I found the error, it was a missing bracket. Thanks for your effort.
----- Original Message -----
From: "Jörgen" <qa...@ho...>
To: "Vlada Djurovic" <dj...@im...>;
<dev...@li...>
Sent: Monday, January 27, 2003 4:34 PM
Subject: Re: [Dev-C++] How to use WSAGetLastError?
The only difference in your code compared to mine is this.
Your code:
if(bind(sock,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1)
My code:
if(bind(sock,(struct sockaddr*)&server,sizeof(server))==-1)
Hope it helps...
/J
*********** REPLY SEPARATOR ***********
On 2003-01-27 at 13:40 Vlada Djurovic wrote:
>Hi.
>I'm trying to create a WinSock app. I created a socket, but bind() returns
>an error, so I tried WSAGetLastError to get error code. Here's the code:
>
>case ID_SOCK_OPEN:
> {
> OpenSocket(PORT);//in this function I create socket
> int error=WSAGetLastError();
> char buffer[100];
> itoa(error,buffer,2);
> MessageBox(NULL,buffer,"Error Code",MB_OK);
> }
> break;
>In message box I get 0, so I suppose something is wrong about this. Can
>anybode tell me better way to get error code?
>Another question: Here's the code for OpenSocket function. Can somebody
>figure out why call to bind() fails?
>
>BOOL OpenSocket(int port)
>{
> if(sock=socket(AF_INET,SOCK_STREAM,0)==-1)
> {
> MessageBox(NULL,"Could not create
>socket","Error",MB_OK|MB_ICONSTOP);
> return FALSE;
> }
> server.sin_family=AF_INET;
> server.sin_port=htons(port);
> server.sin_addr.s_addr=INADDR_ANY;
>
> if(bind(sock,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1)
> {
> MessageBox(NULL,"Bind error","Error",MB_OK|MB_ICONSTOP);
> return FALSE;
> }
>
> if(listen(sock,3)==-1)
> {
> MessageBox(NULL,"Listen error","Error",MB_OK|MB_ICONSTOP);
> return FALSE;
> }
> else
> {
> MessageBox(NULL,"Socket opened","Success",MB_OK);
> return TRUE;
> }
>}
>
>Thanks in advance.
>------------
>Vlada
>
>
>
>-------------------------------------------------------
>This SF.NET email is sponsored by:
>SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
>http://www.vasoftware.com
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
L
-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld =omething 2 See!
http://www.vasoftware.com
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|