News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

WinSock Help

Started by Titan, July 04, 2005, 12:13:54 AM

Previous topic - Next topic

Titan

Can anyone help me get my first WinSock program running smoothly!  First I'll post code:

.data
wsadata WSADATA <>
wsadata2 WSADATA <>
sin sockaddr_in <>
sin2 sockaddr_in <>
IPAddress db "127.0.0.1",0
TestData db 4
Port dd 80                    ; We use port 80, HTTP port for demonstration purpose

.data?

sock dd ?
listeningSocket dd ?
theClient dd ?
recv1 dd ?

.code
...........
This is first button to create "server"
...........
.elseif eax==Serv
invoke MessageBox,hWnd,CTEXT("Initializing Server..."),CTEXT("Step 1"),MB_OK
invoke WSAStartup, 101h,addr wsadata ;Startup WinSock32
.if eax!=NULL
    invoke MessageBox,hWnd,CTEXT("Error 1"),CTEXT("Error"),MB_ICONERROR
.else
invoke socket,AF_INET,SOCK_STREAM,0 ; Create a stream socket for internet use
.if eax!=INVALID_SOCKET
    mov listeningSocket,eax
    mov sin.sin_family, AF_INET
    invoke inet_addr, addr IPAddress
    mov sin.sin_addr, eax
    invoke htons, Port
    mov sin.sin_port, ax
    invoke bind,listeningSocket,addr sin, sizeof sin
    .if eax == SOCKET_ERROR
    invoke MessageBox,hWnd,CTEXT("Error 7"),CTEXT("Error"),MB_ICONERROR
    .endif
    invoke listen,listeningSocket,5
    .if eax == SOCKET_ERROR
    invoke MessageBox,hWnd,CTEXT("Error 8"),CTEXT("Error"),MB_ICONERROR
    .endif
    invoke accept,listeningSocket,0,0
    .if eax == INVALID_SOCKET
    invoke MessageBox,hWnd,CTEXT("Error 9"),CTEXT("Error"),MB_ICONERROR    
    .endif
    mov theClient, eax
   
    invoke recv,listeningSocket,recv1,1,0
    .if eax != 0
    invoke MessageBox,hWnd,CTEXT("Pop up!"),CTEXT("Error"),MB_ICONERROR
    .endif
.else
    invoke MessageBox,hWnd,CTEXT("Error 2"),CTEXT("Error"),MB_ICONERROR
.endif
.endif

...................
Second button to connect to server
...................
.elseif eax==Client
invoke WSAStartup, 101h,addr wsadata2 ;Startup WinSock32
.if eax!=NULL
    invoke MessageBox,hWnd,CTEXT("Error 1"),CTEXT("Error"),MB_ICONERROR
.else
invoke socket,AF_INET,SOCK_STREAM,0 ; Create a stream socket for internet use
.if eax!=INVALID_SOCKET
    mov sock,eax
mov sin2.sin_family, AF_INET
invoke htons, Port                    ; convert port number into network byte order first
mov sin2.sin_port,ax                  ; note that this member is a word-size param.
invoke inet_addr, addr IPAddress    ; convert the IP address into network byte order
mov sin2.sin_addr,eax
invoke connect,listeningSocket,addr sin2,sizeof sin2
.if eax==SOCKET_ERROR            ; assuming we use non-blocking mode, connect
                                  ; will always return SOCKET_ERROR
    invoke WSAGetLastError            ; retrieve the actual error code
    .if eax!=WSAEWOULDBLOCK        ; if it's not WSAEWOULDBLOCK
        invoke MessageBox,hWnd,CTEXT("Could not connect."),CTEXT("Error"),MB_ICONERROR
    .endif
.endif

   
.else
    invoke MessageBox,hWnd,CTEXT("Error 2"),CTEXT("Error"),MB_ICONERROR
.endif
.endif

.endif

....................
Third button to send a packet to the server
....................
.elseif eax==Popup1
invoke send,sock,addr TestData,1,0
.if eax==SOCKET_ERROR
invoke MessageBox,hWnd,CTEXT("Send didn't work."),CTEXT("Error"),MB_ICONERROR
.endif



So what I do is startup 2 running EXEs of my app.  I click to create a server on 1, and no error occurs.  But when I go to my second app and click "Client" to connect to the server... I get "Unable to Connect".  I don't know if anyone here is good and can see what I did wrong. :-\

I made this through a combination of Iczelion's tutorial and a C++ one.

Thanks!

James Ladd

Titan,

A couple of things:

1. Can you post a zip of the code. Its easier to help when you do this.
2. Always clear out the sockaddr_in structures before use. See RtlZeroMemory api
3. Can you tell us what the error is ?  ie: What does WSAGetLastError return in eax
4. You should probably ask for Winsock 2.2 in your WSAStartup. Change 101h to 202h
5. Read the FAST Server code here: http://www.jamesladdcode.com

Rgs, striker.

Titan

Okay I will check out some of the things you suggested.  If you want to take a look I attached it.

Beware, I don't have clean code. :red

[attachment deleted by admin]

Titan

Update!
I changed under client:

invoke connect,listeningSocket,addr sin2,sizeof sin2

to

invoke connect,sock,addr sin2,sizeof sin2

And now right when I connect.. the server makes a popup.  Also, pressing "Popup" does not work.  Am I not receiving messages correctly?

James Ladd

Titan,
Ill have a look at your code as soon as I can.
In the interim, why dont you do this as a console application to make the winsock stuff work, then make
a GUI arounf it later ?
You could cut and paste the guts of the FAST Server if you want ?
Rgs, striker.

Titan

Well, here's what I have so far.  The server seems to set up fine.  When I go to connect the server gets an error in the "recv".

I don't think I'm receiving messages right...  I'm going to bed now but I'll work on it more tommorrow and look at your FAST code.  Your help is greatly appreciated striker. :)

[attachment deleted by admin]

Danesh

Hi Titan,

As James said, the FastServer is the best solution for implementing a client/server based application, but for your information I have attached a very simple code which just implements a simple server and a client which connects to server and send data and server receives sent data. It may help... :U

Regards,

Danesh



[attachment deleted by admin]

James Ladd

Danesh,
Thanks for your kind words about FAST and thanks for giving Titan an simple example.
Rgs, striker.

Titan

Yes, thank you for the example. :)

My program works fine.. the server creates and the client connects.  However, when I go to send a packet I am getting an error.

.elseif eax==Send

mov buffer1,1
invoke send,s1,buffer1,1,0
.if eax == SOCKET_ERROR
invoke MessageBox,hWnd,CTEXT("Unable to send"),CTEXT("Error"),MB_ICONERROR

invoke WSAGetLastError
mov ErrorNum, eax
invoke dw2a, ErrorNum, addr bufEax
;mov ErrorNum, eax
invoke MessageBox, hWnd,addr bufEax, CTEXT("Error"), MB_ICONERROR

invoke WSACleanup
.endif

I'm getting an error "1813".. but that doesn't make any sense as I cannot find a WinSock error 1813.  Anyone know what this means?

Rifleman

Error 1813 is a socket error, it usually means 'cannot establish connection'

It is possible to have an active connection die and then you will get this error.  I will look at the code in the morning but that is the error.

hth,
Paul

Titan

Well if you're going to look my code.. better give you the latest version. :green

[attachment deleted by admin]

Rifleman

I will look at the code.  I tested the version posted by Danesh and it does not work.  The client runs for about 3 seconds while trying to connect to the server and then quietly exits.

Paul

Rifleman

Titan,
In my first tests, I can tell you that WinSockTest2.exe has serious issues.  It will start and the client says it connects but then if you do anything at all in the server dialog whether you click send or the menues, the server crashes.  Run the taskmanager and you can see this happen.  Also, neither dialog box refreshes itself properly which is very frustrating if you accidently open another task over them or if you start them both without moving one first.

Paul

Titan

Paul I've never had that problem.  They work fine for me.  The only time it should freeze is when you click "Start Server", because it is listening.  After you click "Connect" on the client, the listening server is closed and a new asyncronous server is started on the server side.  I've never had any of the problems you mention... my only problem is the send function simply giving me a 1813 error. :-\

Since I don't name my files very well:
WinSockTest2 = Server
WinSock2Client = Client

Rifleman

Titan,
Actually, the first instance tries to close but crashes and if you start the taskmanager you will see that you wind up with two instances of the server and both are 'not responding.'

BTW:  Are you using XP?  I am.

Paul