View previous topic :: View next topic |
Author |
Message |
bornsoft Contributor
Joined: 19 Feb 2009 Posts: 113 Location: Germany
|
Posted: Mon Nov 15, 2010 3:25 am Post subject: Need help making a struct for WinSock |
|
|
Hi,
please can someone help me with the code below.
The script creates a socket but when i try to bind it to
an ip-address it fails with error 10049 ( WSAEADDRNOTAVAIL Can't assign requested address ).
(WSAEADDRNOTAVAIL sounds like the address is not in the right format, I tested host and network byte order but normally the funktion "inet_addr" should output the right format.)
It must have something to do with the structs "sockaddr_in" and "sockaddr". I did research on
internet for hours and this is I came up with. There must be something wrong, but I have no
Idea anymore.
Here is a link to a particular tutorial about this item.
http://www.cpp-home.com/tutorials/26_3.htm
And as attachment i add the winsock helpfile.
Thanks a lot in advance.
bornSoft
Code: |
# API Functions
#DEFINE Function,htons
#DEFINE Function,htonl
#DEFINE Function,inet_addr
#DEFINE Command,WSAStartup
#DEFINE Command,WSACleanup
#DEFINE Command,WSAError
%%WS_VERSION_REQD = $101
%%AF_INET = 2
%%SOCK_STREAM = 1
%%IPPROTO_TCP = 6
# %%IP_Addr = 0.0.0.0
%%IP_Addr = "127.0.0.1"
%%Port = 5001
# WSAStarup
loadlib ws2_32.dll
%B = @fill(384)
%R = @LIB(ws2_32,WSAStartup,INT:,%%WS_VERSION_REQD,@addr("%B"))
WSAError
# Create Socket
%%Socket = @LIB(ws2_32,socket,INT:,%%AF_INET,%%SOCK_STREAM,%%IPPROTO_TCP)
WSAError
# structure sockaddr_in
%%in_family = @binary(WORD,%%AF_INET)
%%in_port = @binary(WORD,@htons(%%Port))
%%in_addr = @inet_addr(%%IP_Addr)
%%in_zero = @fill(8,00000000)
%%sockaddr_in = %%in_family%%in_port%%in_addr%%in_zero
# structure sockaddr
%%sa_family = @binary(WORD,%%AF_INET)
%%sa_data = @fill(14,%%sockaddr_in)
%S = %%sa_family%%sa_data
%L = @len(%S)
%R = @LIB(ws2_32,bind,INT:,%%Socket,@addr("%S"),@len(%S))
WSAError
info Binding Socket %%Socket to Address %%IP_Addr on Port %%Port was successful. @tab()
:WSACleanup
%R = @LIB(ws2_32,WSACleanup,INT:)
freelib ws2_32.dll
Stop
:WSAError
%R = @LIB(ws2_32,WSAGetLastError,INT:)
if @not(@zero(%R))
warn WSA-Error: %R @tab()
goto WSACleanup
end
exit
# API Functions
:htons
exit @LIB(ws2_32,htons,INT:,%1)
:htonl
exit @LIB(ws2_32,htonl,INT:,%1)
:inet_addr
exit @LIB(ws2_32,inet_addr,INT:,%1)
|
Description: |
|
Download |
Filename: |
winsock_hlp.zip |
Filesize: |
139.2 KB |
Downloaded: |
1653 Time(s) |
|
|
Back to top |
|
|
bornsoft Contributor
Joined: 19 Feb 2009 Posts: 113 Location: Germany
|
Posted: Tue Nov 16, 2010 12:48 am Post subject: |
|
|
Forget it, I must have been drunk when I studied the tutorials
The sockaddr_in struct must not be included in the sockaddr struct, it is a replacement.
FYI here is the correct one.
Code: |
# structure sockaddr_in
%%in_family = @binary(WORD,%%AF_INET)
%%in_port = @binary(WORD,@htons(%%Port))
%%in_addr = @binary(DWORD,@inet_addr(%%IP_Addr))
%%in_zero = @fill(8,00000000)
%S = %%in_family%%in_port%%in_addr%%in_zero
|
I'm talking to myself. Crazy, isn't it?
.
|
|
Back to top |
|
|
|
|
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 You can attach files in this forum You can download files in this forum
|
|