i'm writing a program that connects to a server. heres the breakdown:



-connect_port _11111 [SYN]
-receive_port [SYN, ACK]
-send_packet_11111_ [ACK]
-send_packet_11111_login [PSH, ACK]
-receive_port [ACK] ****
-receive_port_2 [PSH, ACK] ****
-send_packet_11111 [ACK] ****
-connect_port _11112 [SYN]
-receive_port [SYN, ACK]
-send_packet_11112 [ACK]
-send_packet_11112_login [PSH, ACK]
-receive_port [ACK]
-receive_port_2 [PSH, ACK]
-send_packet_11112 [ACK]
-receive_port_11112_main_character [PSH, ACK]
-send_packet_11112 [ACK]
-receive_port_main_menu [PSH, ACK]
-send_packet_11112_commands [ACK]


i've been reading all these winsock help files, and tuts, and i still have a headache. everything goes fine until you see the asterisks *

at this very moment, i am using blocking sockets, in threads--anyhow, its probably best to use non-blocking in thread, with event/msg'n but i'll get there soon enough

okay here then...



.486
.model flat,stdcall
option casemap:none

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

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

WinMain proto :DWORD,:DWORD,:DWORD


.data?
hInstance dd ?
hDlg HWND ?
wsaData WSADATA <?>
hSocket_11111 dd ?
hSocket_11112 dd ?
sockAddr_11111 sockaddr_in <?>
sockAddr_11112 sockaddr_in <?>
hEditInfo HWND ?
buffer db 256 dup(?)

ThreadID DWORD ?

.data
szIPAddress db "204.176.33.66",0
AppName db "xClient 0.0.1",0
ClassName db "DFC_GameClient_Class",0
MenuName db "DFC_GameClient_Menu",0
DlgName db "DFC_GameClient_Dialog",0
ConnectToken db 1Bh, 1Bh, 00h, 73h, 00h, 02h, 00h, 80h, 0FFh, 0F2h
db 00h, 0Fh, 00h, 1Eh, 01h, 09h, 03h, 69h, 77h, 70h, 34h, 32h, 35h, 00h, 00h, 00h
db 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h
db 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h
db 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 44h, 41h, 52h, 4Bh, 4Eh, 45h, 53h
db 53h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 72h, 6Fh, 68h
db 00h, 50h, 39h, 35h, 61h, 72h, 69h, 33h, 36h, 30h, 53h, 00h, 00h, 00h, 00h, 00h
db 00h, 00h, 00h, 00h, 00h, 0ADh, 9Ah

PacketOut_1 db 1Bh, 1Bh, 00h, 04h, 00h, 03h, 00h, 80h, 0FFh, 0F2h, 00h, 0Bh, 00h, 00h, 00h, 00h
db 0FBh, 04h

PacketOut_2 db 1Bh, 1Bh, 00h, 04h, 00h, 04h, 00h, 80h, 0FFh, 0F2h, 00h, 0Bh, 00h, 00h, 00h, 00h
db 0EFh, 0Fh

.const
IDM_CONNECT equ 3000
IDM_DISCONNECT equ 3001
IDM_EXIT equ 3002
IDC_EDIT_INFO equ 2000
CR equ 0Dh
LF equ 0Ah

.code

ThreadProc PROC
invoke WSAStartup,0002h, addr wsaData
test eax,eax
jz _startupSucceeded
ret

_startupSucceeded:
cmp byte ptr [wsaData.wVersion],2
jae _versionOK
invoke MessageBox,hDlg,SADD("Initialization failed."),addr AppName,MB_OK
jmp _exitThread

_versionOK:
invoke socket,AF_INET,SOCK_STREAM,IPPROTO_TCP
cmp eax,INVALID_SOCKET
jne _socket_created_11111
invoke MessageBox,hDlg,SADD("Error creating socket."),addr AppName,MB_OK
jmp _exitThread

_socket_created_11111:
mov [hSocket_11111],eax
mov [sockAddr_11111.sin_family],AF_INET
invoke htons,11111
mov [sockAddr_11111.sin_port],ax
invoke inet_addr,addr szIPAddress
mov [sockAddr_11111.sin_addr],eax

invoke socket,AF_INET,SOCK_STREAM,IPPROTO_TCP
cmp eax,INVALID_SOCKET
jne _socket_created_11112
invoke MessageBox,hDlg,SADD("Error creating socket."),addr AppName,MB_OK
jmp _exitThread

_socket_created_11112:
mov hSocket_11112,eax
mov [sockAddr_11112.sin_family],AF_INET
invoke htons,11112
mov [sockAddr_11112.sin_port],ax
invoke inet_addr,addr szIPAddress
mov [sockAddr_11112.sin_addr],eax

invoke connect,[hSocket_11111],addr sockAddr_11111,sizeof sockAddr_11111
test eax,eax
jz _connectSucceeded
invoke MessageBox,hDlg,SADD("Connection failed."),addr AppName,MB_OK
jmp _exitThread

_connectSucceeded:
invoke MessageBox,hDlg,SADD("Successfully connected to 11111."),addr AppName,MB_OK
invoke send,[hSocket_11111],addr ConnectToken,113,0
cmp eax,SOCKET_ERROR
jne _sendSucceeded
invoke MessageBox,hDlg,SADD("Send failed."),addr AppName,MB_OK
jmp _exitThread

_sendSucceeded:
invoke recv,[hSocket_11111],addr buffer,256,0
test eax,eax
jz _connectionClosed
invoke send,[hSocket_11111],addr PacketOut_1,18,0
invoke connect,[hSocket_11112],addr sockAddr_11112,sizeof sockAddr_11112
test eax,eax
jz _connectSucceeded2
jmp _connectionClosed

_connectSucceeded2:
invoke MessageBox,hDlg,SADD("Successfully connected to 11112."),addr AppName,MB_OK
invoke send,[hSocket_11112],addr ConnectToken,113,0
cmp eax,SOCKET_ERROR
jne _loop_Receive_11112
jmp _exitThread

_loop_Receive_11111:
invoke recv,[hSocket_11111],addr buffer,256,0
test eax,eax
jz _connectionClosed
jmp _loop_Receive_11111

_loop_Receive_11112:
invoke recv,[hSocket_11112],addr buffer,256,0
jmp _loop_Receive_11112

_connectionClosed:
invoke MessageBox,hDlg,SADD("Closing connection '11111'."),addr AppName,MB_OK
invoke closesocket,[hSocket_11111]
invoke MessageBox,hDlg,SADD("Closing connection '11112'."),addr AppName,MB_OK
invoke closesocket,[hSocket_11112]

_exitThread:
invoke MessageBox,hDlg,SADD("Cleaning up sockets."),addr AppName,MB_OK
invoke WSACleanup
xor eax,eax
ret

ThreadProc endp

start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke WinMain,hInstance,NULL,SW_SHOWDEFAULT
invoke ExitProcess,eax

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
mov wc.cbSize,SIZEOF WNDCLASSEX
mov wc.style,CS_HREDRAW or CS_VREDRAW
mov wc.lpfnWndProc,OFFSET WndProc
mov wc.cbClsExtra,NULL
mov wc.cbWndExtra,DLGWINDOWEXTRA
push hInst
pop wc.hInstance
mov wc.hbrBackground,COLOR_BTNFACE+1
mov wc.lpszMenuName,OFFSET MenuName
mov wc.lpszClassName,OFFSET ClassName
invoke LoadIcon,hInstance,0
mov wc.hIcon,eax
mov wc.hIconSm,eax
invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,eax
invoke RegisterClassEx,addr wc
invoke CreateDialogParam,hInstance,addr DlgName,NULL,NULL,NULL
mov hDlg,eax
invoke ShowWindow,hDlg,SW_SHOWNORMAL
invoke UpdateWindow,hDlg
.WHILE TRUE
invoke GetMessage,addr msg,NULL,0,0
.BREAK .IF (!eax)
invoke IsDialogMessage,hDlg,addr msg
.if eax==FALSE
invoke TranslateMessage,addr msg
invoke DispatchMessage,addr msg
.endif
.ENDW
mov eax,msg.wParam
ret
WinMain endp
WndProc proc hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
.if uMsg==WM_DESTROY
invoke WSACleanup
invoke PostQuitMessage,NULL
.elseif uMsg==WM_CREATE
invoke GetDlgItem,hDlg,IDC_EDIT_INFO
mov hEditInfo,eax
.elseif uMsg==WM_COMMAND
mov eax,wParam
.if ax==IDM_CONNECT
mov eax,OFFSET ThreadProc
invoke CreateThread,NULL,NULL,eax,\
NULL,NORMAL_PRIORITY_CLASS,\
addr ThreadID
.elseif ax==IDM_DISCONNECT
invoke SendMessage,hDlg,WM_DESTROY,0,0
.elseif ax==IDM_EXIT
invoke SendMessage,hDlg,WM_DESTROY,0,0
.endif
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
xor eax,eax
ret
WndProc endp
end start


hopefully (i doubt it) if u have gone down this far, i will explain what's going on.

1. it sends a connect packet
2. it gets confirmation its okay to send
3. it sends the login packet
4. *** it suppose to read back the packet sent after that, but my program only stops on like the header
5. after that, it sends another packet, and the 2nd packet connects on another port
6. and theres more other stuff that need to be dealth with ;x
Posted on 2003-04-13 22:24:42 by xkardisx
oih yea im aware there som eproblems near the end, ,with the recvloop but i'll get there l8r
Posted on 2003-04-13 22:25:27 by xkardisx
Straight away, your problem is that winsock version 2 is started using the value 202h .
Version 1 - 0101h
Version 2 - 0202h

It's major and minor versioning for winsock.

Heh.
Posted on 2003-04-16 23:50:04 by Homer