Sign in to follow this  
Followers 0
Oishi

TCP Client/server

3 posts in this topic

#1 ·  Posted (edited)

I have a client/server program set up. Everything works fine except for the server getting the ip of the connecting client. It writes everything else fine to the log file except that it's returning '0'. I had it working before, but then I did some editing and now I can't figure out what I screwed up. If anyone could help, that would be much appreciated. Here's the server code.

#Include <Date.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#notrayicon

$GOOEY = GUICreate("Update server", 300, 200)
$edit = GUICtrlCreateEdit("", 10, 10, 280, 180, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_READONLY,$ES_WANTRETURN,$WS_HSCROLL,$WS_VSCROLL))
GUISetState()

GUICtrlSetData($edit, "> GUI created.")

global $userver = IniRead("server.ini", "settings", "host", "not found")
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF & "> Update server is " & $userver)
global $client = IniRead("server.ini", "settings", "cversion", "not found")
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF &"> Client version is " & $client)
global $patcher = IniRead("server.ini", "settings", "pversion", "not found")
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF &"> Patch version is " & $patcher)
global $port = IniRead("server.ini", "settings", "port", "599")
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF &"> Port set to " & $port)

Global $CURRENT_SOCKET = 0
Global $CONNECTED_SOCKET[99]
Global $TCP_SERVER

$TCP = TCPStartup()
If $TCP = 0 Then
    MsgBox(0, "Error", "Unable to startup TCP Services!")
    Exit
EndIf
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF &"> TCP Started successfully.")

$TCP_SERVER = TCPListen(@IPAddress1,$port,16)
If $TCP_SERVER = -1 Then
    MsgBox(0, "ERROR", "Unable to start listening on port " & $port)
    Exit
EndIf

GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF &"> Listening on port " & $port)
$log = fileopen("log.txt", 9)
if $log > -1 then
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF &"> Log file opened for writing")
endif

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Quit()
    $CONNECTED_SOCKET[$CURRENT_SOCKET] = TCPAccept($TCP_SERVER)
    If $CONNECTED_SOCKET[$CURRENT_SOCKET] <> -1 Then
        if $CURRENT_SOCKET = 98 Then
            $CURRENT_SOCKET = 0
        endif
        $CURRENT_SOCKET += 1
    EndIf
    For $INDEX = 0 To 98
        $RECV = TCPRecv($CONNECTED_SOCKET[$INDEX],512)
        if $RECV = "request" then 
            TCPSend($CONNECTED_SOCKET[$INDEX], $userver & "," & $client & "," & $patcher)
            TCPCloseSocket($CONNECTED_SOCKET[$INDEX])
            $ip = SocketToIP($CONNECTED_SOCKET[$INDEX])
            filewriteline($log, _NOW() &  ", SR, " & $ip & ", " & $RECV)
        Elseif $RECV <> "" then
            $ip = SocketToIP($CONNECTED_SOCKET[$INDEX])
            filewriteline($log, _NOW() & ", BR, " & $ip & ", " & $RECV)
            GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF &"> WARNING: Bad request from " & $ip & " - DIE sent - connection terminated.")
            TCPSend($CONNECTED_SOCKET[$INDEX],"DIE")
            TCPCloseSocket($CONNECTED_SOCKET[$INDEX])
        endif
    Next
    Sleep(20)
WEnd

Func Quit()
    TCPCloseSocket($TCP_SERVER)
    TCPShutdown()
    fileclose($log)
    Exit
EndFunc

Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
    
    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc   ;==>SocketToIP
Edited by Oishi

Share this post


Link to post
Share on other sites



Hey there!

Did you check help file under TCPRecv reference ?

Share this post


Link to post
Share on other sites

I got it fixed. Thanks anyways.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0