flat assembler
Message board for the users of flat assembler.
Index
> Windows > [Solved] Socket bind Port |
Author |
|
revolution 24 Jan 2011, 20:55
You need to set the structure
Code: invoke htons,IPPORT_HTTP ;your port goes here mov [STATE.sock_addr.sin_port],ax |
|||
24 Jan 2011, 20:55 |
|
Ianis 24 Jan 2011, 21:01
I'm writning server code and i listen after bind. ( that work )
Your code is to specify a port, i don't need this. The computer automaticly set a free port to the socket bind. I want to be able to use it in my app. |
|||
24 Jan 2011, 21:01 |
|
ManOfSteel 24 Jan 2011, 21:04
Ianis wrote: Your code is to specify a port, i don't need this. That's not how a server usually works. |
|||
24 Jan 2011, 21:04 |
|
Ianis 24 Jan 2011, 21:14
ManOfSteel wrote:
Yes but for my project i don't want a constant port for my server. That's why i must let it bind on random ports. otherwise i could be interested in a random number generator. Generate a random number between min and max. To bind my port on it. |
|||
24 Jan 2011, 21:14 |
|
revolution 24 Jan 2011, 21:18
I have never seen a way to set a wildcard port. I think that with the standard Windows socks API you will need to set a listener for every port you want to capture.
|
|||
24 Jan 2011, 21:18 |
|
Ianis 24 Jan 2011, 22:12
Then, do you know a way to get binded port from msock / saddr ?
Or a way to generate random numbers and set them as port to bind ? Thanks. |
|||
24 Jan 2011, 22:12 |
|
revolution 24 Jan 2011, 22:17
You have to choose a port first and then listen on that port. The Windows socks API enforces that behaviour. You can listen on multiple ports at the same time, but each port will need a separate listener running.
Last edited by revolution on 25 Jan 2011, 11:04; edited 1 time in total |
|||
24 Jan 2011, 22:17 |
|
SFeLi 25 Jan 2011, 07:43
Ianis, just bind to port 0 and Winsock would choose a port number automatically. You could then retrieve it by calling getsockname.
|
|||
25 Jan 2011, 07:43 |
|
revolution 25 Jan 2011, 07:58
Oh, I misunderstood what you wanted to do. I thought you wanted to connect to all incoming port requests. But actually you need a single port and don't want to dedicate the assignment.
In that case SFeLi is correct. But you will only get back a limited port range of something like 1024-5000 (IIRC). This may or may not be a problem but is something to be aware of. |
|||
25 Jan 2011, 07:58 |
|
Picnic 25 Jan 2011, 07:59
Hi Ianis, SFeLi, revolution,
That might not necessarily work according to msdn If the Internet address is equal to INADDR_ANY or in6addr_any, getsockname cannot necessarily supply the address until the socket is connected |
|||
25 Jan 2011, 07:59 |
|
Ianis 25 Jan 2011, 22:14
SFeLi wrote: Ianis, just bind to port 0 and Winsock would choose a port number automatically. You could then retrieve it by calling getsockname. Code: ... start: mov ecx, 0 invoke WSAStartup, 0202h, wsadata invoke socket, AF_INET, SOCK_STREAM, 0 cmp eax, -1 je _exit mov [msock], eax mov [saddr.sin_family], AF_INET invoke bind, [msock], saddr, sizeof.sockaddr_in test eax, eax jnz _exit invoke getsockname, [msock], saddr, sizeof.sockaddr_in mov [saddrs.sin_port], ax push ax invoke MessageBox,0,ax,sTitle,MB_OK ... Thank you SFeLi I would like to do something like that but it don't work.. My code seem wrong. |
|||
25 Jan 2011, 22:14 |
|
Picnic 26 Jan 2011, 07:19
MessageBox requires a long pointer to a null-terminated string. You can't pass a 16-bit register to a Windows API function as parameter. Use wsprintf to convert register value to string.
|
|||
26 Jan 2011, 07:19 |
|
SFeLi 26 Jan 2011, 12:53
Additionally remember that sin_port is big-endian and x86 works with little-endian so you need either to do xchg ax,al or call ntohs before passing ax to wsprintf.
Edit: your code contains a lot of errors. Here's is the working version: Code: format pe gui 4.1 entry start include 'win32a.inc' start: invoke WSAStartup,0202h,wsadata invoke socket, AF_INET, SOCK_STREAM, 0 cmp eax, -1 je _exit mov [msock], eax mov [saddrs.sin_family], AF_INET invoke bind, [msock], saddrs, sizeof.sockaddr_in test eax, eax jnz _exit mov [salen],sizeof.sockaddr_in invoke getsockname, [msock], saddrs, salen movzx eax,[saddrs.sin_port] xchg al,ah cinvoke wsprintf,portbuff,portfmt,eax invoke MessageBox,0,portbuff,sTitle,MB_OK _exit: invoke ExitProcess,0 data import library kernel32,'KERNEL32.DLL',\ user32,'USER32.DLL',\ wsock32,'WS2_32.DLL' include 'api/kernel32.inc' include 'api/user32.inc' include 'api/wsock32.inc' end data sTitle db 'bind to 0',0 portfmt db 'Port number is %d',0 wsadata WSADATA msock dd ? salen dd ? saddrs sockaddr_in portbuff rb 32 |
|||
26 Jan 2011, 12:53 |
|
Ianis 26 Jan 2011, 19:18
SFeLi wrote: Additionally remember that sin_port is big-endian and x86 works with little-endian so you need either to do xchg ax,al or call ntohs before passing ax to wsprintf. Thanks a lot SFeLi with fews modifications your code solved my problem. |
|||
26 Jan 2011, 19:18 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.