forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Need help making a struct for WinSock

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> Advanced Help for VDS 5 & Up
View previous topic :: View next topic  
Author Message
bornsoft
Contributor
Contributor


Joined: 19 Feb 2009
Posts: 113
Location: Germany

PostPosted: Mon Nov 15, 2010 3:25 am    Post subject: Need help making a struct for WinSock Reply with quote

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)




winsock_hlp.zip
 Description:
The WinSock Helpfile

Download
 Filename:  winsock_hlp.zip
 Filesize:  139.2 KB
 Downloaded:  1433 Time(s)

Back to top
View user's profile Send private message Visit poster's website
bornsoft
Contributor
Contributor


Joined: 19 Feb 2009
Posts: 113
Location: Germany

PostPosted: Tue Nov 16, 2010 12:48 am    Post subject: Reply with quote

Forget it, I must have been drunk when I studied the tutorials Wink

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> Advanced Help for VDS 5 & Up 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
You can attach files in this forum
You can download files in this forum

Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group