The MASM Forum

General => The Campus => Topic started by: gelatine1 on November 26, 2014, 09:10:36 PM

Title: Connection with another computer, winsock
Post by: gelatine1 on November 26, 2014, 09:10:36 PM
 I have some ideas like creating a pong game (vs someone on a different pc) or some chat room but they require me to connect to another computer. I do have a second compankuter at home which I would like to use to test but I have no clue what address or hostname or port I should use. I've been reading on msdn about winsock and also iczelions tutorials (http://win32assembly.programminghorizon.com/asmsockguide.html (http://win32assembly.programminghorizon.com/asmsockguide.html)) but they only explain the functions and their parameters.

I don't know very much about netwerking so could anyone explain me how to find out the ip address I should connect to or the hostname ? and what about the ports ?

Thanks in advance
jannes
Title: Re: Connection with another computer, winsock
Post by: six_L on November 26, 2014, 09:20:01 PM
read this first.
http://www.madwizard.org/programming/tutorials/netasm/ (http://www.madwizard.org/programming/tutorials/netasm/)
Title: Re: Connection with another computer, winsock
Post by: anunitu on November 26, 2014, 11:34:59 PM
Although I haven't looked at it,you might take a look at the Doom source code. It had a multi player function for death match. Not sure what form they used,but it might be interesting to check out.
Title: Re: Connection with another computer, winsock
Post by: gelatine1 on November 27, 2014, 05:29:32 PM
Thanks, I'll read it :) and what is the doom source code ? where can I find it ?
Title: Re: Connection with another computer, winsock
Post by: shaikkareem on November 27, 2014, 07:22:41 PM
Quote from: six_L on November 26, 2014, 09:20:01 PM
read this first.
http://www.madwizard.org/programming/tutorials/netasm/ (http://www.madwizard.org/programming/tutorials/netasm/)
:t
Title: Re: Connection with another computer, winsock
Post by: anunitu on November 27, 2014, 09:18:05 PM
Doom is a video game from the 80's I think,might be 90's,they released the source code a while back. Written in C or C++,it might show how to connect two systems.

Check the source here.
https://github.com/id-Software/DOOM

here is some info about interconnecting.
http://doom.wikia.com/wiki/Doom_networking_engine
Title: Re: Connection with another computer, winsock
Post by: dedndave on November 29, 2014, 10:44:12 PM
it may have been the old forum, but i remember someone posted a simple client-server pair
Title: Re: Connection with another computer, winsock
Post by: gelatine1 on December 01, 2014, 07:50:42 AM
okay, I have read the madwizard.org tutorial now and I tried to write some code for a server. I tried to compile to test a little but somehow the compiler does not seem to recognize the SOCKADDR_IN structure ?
http://msdn.microsoft.com/en-us/library/aa917469.aspx (http://msdn.microsoft.com/en-us/library/aa917469.aspx)
http://msdn.microsoft.com/en-us/library/windows/desktop/ms740496%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/ms740496%28v=vs.85%29.aspx)

Maybe I am just missing some include or something simple ? this is the top part of my code:


.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\ws2_32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\ws2_32.lib

WinMain proto :DWORD,:DWORD,:DWORD,:DWORD

.const
WM_SOCKET equ WM_USER+8

.data
file db "D:\hi.txt",0
ClassName db "Window1",0
AppName db "Circle",0

listen_ip db "127.0.0.1",0
listen_port dd 27015

.data?
wsa WSADATA <>

sock dd ?
clientSock dd ?

sockAddr SOCKADDR_IN <>

.code
;and my code below is irrelevant.


Then the compiler gives as output this:
Quoteserver.asm(33) : error A2008: syntax error : sockAddr

where 33 is the line number of the "sockAddr SOCKADDR_IN <>" line.

Is there anyone who can tell me why I am getting this error ?

Thanks in advance
Jannes
Title: Re: Connection with another computer, winsock
Post by: jj2007 on December 01, 2014, 08:39:54 AM
Jannes,

In such cases, go to \Masm32\include and use a tool that does a case-insensitive search in all *.inc files. You would find out that the struct exists but... it is all lowercase 8)

sockaddr_in STRUCT
  sin_family    WORD      ?
  sin_port      WORD      ?
  sin_addr      in_addr <>
  sin_zero      BYTE 8 dup (?)
sockaddr_in ENDS
Title: Re: Connection with another computer, winsock
Post by: dedndave on December 01, 2014, 11:57:02 AM
there are \masm32\include\winsock.inc (or wsock32.inc) and \masm32\lib\wsock32.lib files

but, if i remember correctly, they are superceded by
        include     \masm32\include\ws2_32.inc
        includelib  \masm32\lib\ws2_32.lib
Title: Re: Connection with another computer, winsock
Post by: gelatine1 on December 02, 2014, 01:10:32 AM
Oh wow thank you very much jj2007  :redface:
Title: Re: Connection with another computer, winsock
Post by: gelatine1 on December 03, 2014, 02:04:06 AM
Okay, now I have created my server and client. The client is trying to connect to the loop-back ip address (127.0.0.1) and it succeeds. I then call the send function (to send a random dword) and the send function returns 4 so it means it worked as it should.

However on the server side who listens for incoming connections I am not receiving anything. Not even an FD_ACCEPT message so I was wondering what is going wrong ? Maybe it is wrong to try to connect with 127.0.0.1 ? Ill try to give some relevant parts of my code below:

Client:

.data
ClassName db "Window1",0
AppName db "Client",0

listen_ip db "127.0.0.1",0
listen_port dd 27015


.data?
wc WNDCLASSEX <>

wsa WSADATA <>

sock dd ?
clientSock dd ?

sockAddr sockaddr_in <>
connected dd ? ; a bool to check if the connection is established, initialised to false

.code
start:
    ;first of all a window is created, but I skipped the code here because it's not relevant (except the messageloop)

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL buffer[4]:byte

mov eax,uMsg

__cont3:
cmp eax, WM_SOCKET
jnz __cont2

mov eax,lParam
cmp ax,FD_CONNECT
jnz _sockerror

mov connected,1

lea esi,buffer
invoke send,sock,esi,4,0

_sockerror:
jmp __cont

__cont2:
cmp eax, WM_DESTROY
jnz __cont1

cmp wParam,1
jz _quit

invoke WSACleanup

_quit:
invoke PostQuitMessage,NULL
jmp __cont
__cont1:
cmp eax,WM_CREATE
jnz __cont0

mov buffer,63

invoke WSAStartup,202h,addr wsa
cmp eax,0
jnz _error

invoke socket,AF_INET,SOCK_STREAM,IPPROTO_TCP
cmp eax,INVALID_SOCKET
jz _error2

mov sock,eax

invoke WSAAsyncSelect,sock,hWnd,WM_SOCKET,FD_READ+FD_WRITE+FD_CONNECT+FD_CLOSE

mov     [sockAddr.sin_family], AF_INET
    invoke  htons,listen_port
    mov     [sockAddr.sin_port], ax
invoke inet_addr,addr listen_ip
    mov     [sockAddr.sin_addr.S_un.S_addr], eax

invoke connect,sock,addr sockAddr,sizeof sockAddr

jmp __cont

_error2:
invoke WSACleanup
_error:
invoke PostMessage,hWnd,WM_DESTROY,1,0
jmp __cont

__cont0:
invoke DefWindowProc,hWnd,uMsg,wParam,lParam     ; Default message processing
ret

__cont:
xor eax,eax
ret
WndProc endp
end start


and the server:


WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL buffer[4]:byte

mov eax,uMsg

__cont3:
cmp eax, WM_SOCKET
jnz __cont2

mov eax,lParam
cmp ax,FD_ACCEPT
jnz _read

invoke accept,sock,0,0
cmp eax,INVALID_SOCKET
jz _sockerror

mov clientSock,eax
jmp __cont

_read:
cmp ax,FD_READ
jnz _sockerror

lea esi,buffer
invoke recv,clientSock,esi,4,0

mov eax,dword ptr [esi]
cmp eax,63
jnz __cont

invoke MessageBox,hWnd,addr file,addr file,MB_OK

_sockerror:
jmp __cont

__cont2:
cmp eax, WM_DESTROY
jnz __cont1

cmp wParam,1
jz _quit

invoke WSACleanup

_quit:
invoke PostQuitMessage,NULL
jmp __cont

__cont1:
cmp eax,WM_CREATE
jnz __cont0

invoke WSAStartup,202h,addr wsa
cmp eax,0
jnz _error

invoke socket,AF_INET,SOCK_STREAM,IPPROTO_TCP
cmp eax,INVALID_SOCKET
jz _error2

mov sock,eax

invoke WSAAsyncSelect,sock,hWnd,WM_SOCKET,FD_READ+FD_WRITE+FD_CONNECT+FD_CLOSE+FD_ACCEPT

mov     [sockAddr.sin_family], AF_INET
    invoke  htons,listen_port
    mov     [sockAddr.sin_port], ax
    mov     [sockAddr.sin_addr.S_un.S_addr], INADDR_ANY ;use default

invoke bind,sock,addr sockAddr,sizeof sockAddr
cmp eax,SOCKET_ERROR
jz _error2

invoke listen,sock,SOMAXCONN
cmp eax,SOCKET_ERROR
jz _error2

jmp __cont

_error2:
invoke WSACleanup
_error:
invoke PostMessage,hWnd,WM_DESTROY,1,0
jmp __cont

__cont0:
invoke DefWindowProc,hWnd,uMsg,wParam,lParam     ; Default message processing
ret

__cont:
xor eax,eax
ret
WndProc endp
end start


Any help is welcome, thanks in advance!
Jannes
Title: Re: Connection with another computer, winsock
Post by: Tedd on December 03, 2014, 03:04:08 AM
Zip and attach full working code to allow for proper testing and debugging.
Title: Re: Connection with another computer, winsock
Post by: gelatine1 on December 03, 2014, 03:17:03 AM
change file extension back to rar if you can't unzip. (I had to change it to zip)
Title: Re: Connection with another computer, winsock
Post by: dedndave on December 03, 2014, 03:37:49 AM
you might find these helpful

http://www.masmforum.com/board/index.php?topic=8292.0 (http://www.masmforum.com/board/index.php?topic=8292.0)

https://gist.github.com/jamesladd/720718 (https://gist.github.com/jamesladd/720718)
Title: Re: Connection with another computer, winsock
Post by: Tedd on December 03, 2014, 02:31:31 PM
The good news is that your server code does work.
It receives what is sent to it - four bytes at a time, but it gets there.

It's actually your client code that's the problem.
Specifically, you're using a local variable in WndProc and assuming it will keep its value. It does not.

What this means is that the contents of 'buffer' (initialised earlier) will no longer be the same when you try to send it (it's in an entirely different invocation of WndProc!) So you're not sending 63, you're sending some random value.
As a quick fix, this will show that it works:

   lea esi,buffer
   mov DWORD PTR [esi],63
   invoke send,sock,esi,4,0


Overall, your code needs many fixes and could be tidied to make it much easier to read (and spot problems.)
Title: Re: Connection with another computer, winsock
Post by: gelatine1 on December 03, 2014, 09:13:18 PM
Could you tell me how do you see that my server code is working ? i run my server in odbg and I put a breakpoint after the WM_SOCKET message (408). then I run the client (which connects to the server) but my server running in odbg never receives any WM_SOCKET message. So that gives me the feeling it is not working but clearly it did work for you ?
Title: Re: Connection with another computer, winsock
Post by: Tedd on December 04, 2014, 08:59:37 AM
Load the server in the debugger, set a breakpoint on the call to accept, and another on recv, then run.
Now start your client and the debugger should hit the breakpoint for accept -- so the connection is being made.
Step over, or continue.
Then the recv breakpoint will be hit and you can see what bytes are actually received.
Title: Re: Connection with another computer, winsock
Post by: gelatine1 on December 05, 2014, 05:24:04 AM
I did exactly as you said but the debugger did not hit the breakpoint for accept nor the breakpoint for recv like you said. Is something wrong or weird going on with my computer that doesn't happen for your computer ? Anyone knows what's going wrong maybe ?
Title: Re: Connection with another computer, winsock
Post by: gelatine1 on December 05, 2014, 06:54:56 PM
Okay, I tried to test using the "telnet 127.0.0.1" command and then I finally had an incoming connection in my server.. :D Then I used the "sen abcd" and I also received a FD_READ message and I received " nes" so a little unexpected but it works :p
Still I am bugged why I can't make a connection between my client and my server.. :/ anyone who can help ?
Title: Re: Connection with another computer, winsock
Post by: Tedd on December 05, 2014, 11:33:27 PM
Check windows' firewall isn't blocking connections;
check anti-virus isn't blocking anything;
try using a lower port number (between 1024 and 5000).


You received " nes" because you sent the bytes 's','e','n',' ','a','b','c','d' and then extracted the first four as a dword (dwords are stored 'backwards,') if you took the next four it would be "dcba".