News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Connection with another computer, winsock

Started by gelatine1, November 26, 2014, 09:10:36 PM

Previous topic - Next topic

gelatine1

 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) 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

six_L

Say you, Say me, Say the codes together for ever.

anunitu

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.

gelatine1

Thanks, I'll read it :) and what is the doom source code ? where can I find it ?


anunitu

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

dedndave

it may have been the old forum, but i remember someone posted a simple client-server pair

gelatine1

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/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

jj2007

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

dedndave

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

gelatine1

Oh wow thank you very much jj2007  :redface:

gelatine1

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

Tedd

Zip and attach full working code to allow for proper testing and debugging.
Potato2

gelatine1

change file extension back to rar if you can't unzip. (I had to change it to zip)