;Usage : ; push "addres_IP" ; push "serverPortToRun" ; Call ServerReceivedAndSend ; Example: ; push "127.0.0.1" ; push "8180" ; Call ServerReceivedAndSend Function ServerReceivedAndSend ;This Basic Server can do: ; Initialize WSA. ; Create a socket. ; Bind the socket. ; Listen on the socket. ; Accept a connection. ; receive data. ; send data ----> Call ServerSendDataToClient ; Disconnect. Exch $0 ; PortToConect Exch Exch $1 ; ServeIP Push $2 ; address struct wsadata Push $3 ; WSAStartup return Push $4 ; socket return Push $5 ; ntohs return Push $6 ; inet_addr Push $7 ; addres struct sockaddr_in Push $8 ; connect return กก push $9 ; WSACleanup return push $R0 ;listen return push $R1 ;accept return push $R2 ;recv return (number of bytes) push $R3 ;string received push $R4 ;pointer to a buffer containing the data received push $R5 ;send return ;-------------------------- Initialize WSA --------------------------------------------------- !define struct_wsadata '(&i2,&i2,&t257,&t129,i,i,l)i' System::Call '*${struct_wsadata} .r2' ; allocates memory for wsadata struct and writes address to $2 ; 514 =2 + 256*2 = MAKEWORD (bLow,bHigh) = bLow + 256 * bHigh ==> version socket ; note : Socket Version Dll required 2.2 or higher System::Call 'ws2_32::WSAStartup(i 514 ,i r2)i .r3' IntCmp 0 $3 0 DllNotOK DllNotOK System::Call '*$2${struct_wsadata}(,,,,,,)' ;-------------------------- Create a socket. --------------------------------------------------- ; From winsock.h : ; AF_INET = 2 ; IPPROTO_TCP = 6 ; SOCK_STREAM = 1 System::Call 'ws2_32::socket(i 2,i 1,i 6)i .r4' System::Call 'ws2_32::ntohs(i $0) i.r5' System::Call 'ws2_32::inet_addr(t "$1") i.r6' !define struct_sockaddr_in '(&i2,&i2,&i4,&t8)i' System::Call '*${struct_sockaddr_in} .r7' ; allocates memory for sockaddr_in struct and writes address to $7 System::Call '*$7${struct_sockaddr_in}(2,r5,r6,)' ; ;-------------------------- Bind the socket. --------------------------------------------------- System::Call 'ws2_32::bind(i $4,i $7,i 16) i.r8' ;-------------------------- Listen on the socket. --------------------------------------------------- System::Call 'ws2_32::listen(i $4,i 1)i .R0' ;-------------------------- Accept a connection. --------------------------------------------------- StrCpy $R1 -1 ; -1 socket_error ${While} $R1 = "-1" ;wait until there is a client conected System::Call 'ws2_32::accept(i $4,i n,i n)i .R1' ; -1 if there is not a client conected ${If} $R1 != "-1" StrCpy $4 $R1 ${EndIf} ${EndWhile} ;-------------------------- Receive data. --------------------------------------------------- System::Call 'ws2_32::recv(i $4,t .R3,i 256,i 0)i .R2' ; Note : $R3 is the received data ; Note : 256 is the string lenght received if you want you must increase .For exemple 512 or 1024. ;--------------------------- Send data. --------------------------------------------------- push "$R3" push "$4" Call ServerSendDataToClient ;-------------------------- Disconnect. --------------------------------------------------- System::Call 'ws2_32::WSACleanup()i .r9 ' System::Free $7 System::Free $2 Goto done DllNotOK: MessageBox mb_ok "The WinSock DLL is unacceptable" Goto done done: Pop $R5 Pop $R4 Pop $R3 Pop $R2 Pop $R1 Pop $R0 Pop $9 Pop $8 Pop $7 Pop $6 Pop $5 Pop $4 Pop $3 Pop $2 Pop $1 Pop $0 FunctionEnd ;Usage : ; push "dataReceived" ; push "socket" ; Call ServerSendDataToClient Function ServerSendDataToClient Exch $R0 ; socket Exch Exch $R1 ; dataReceived push $R2 ; data to send push $R3 ; send return ${If} $R1 == "hello" System::Call "*(&t256 'hello client') i .s" ; allocate a buffer and put 'hello client' in it Pop $R2 System::Call 'ws2_32::send(i $R0,i $R2,i 256,i 0)i .R3' ${ElseIF} $R1 == "bye" System::Call "*(&t256 'bye client') i .s" ; allocate a buffer and put 'bye client' in it Pop $R2 System::Call 'ws2_32::send(i $R0,i $R2,i 256,i 0)i .R3' ${EndIf} pop $R3 pop $R2 pop $R1 pop $R0 FunctionEnd