Sign in to follow this  
Followers 0
Blackmental

Problem with online TCP Chat. Server sends no data.

1 post in this topic

#1 ·  Posted (edited)

Hi guys,

I've a big problem with TCP. I've find a Tutorial in the web on youtube. It shows how to make a TCP Chat, but lokal. I've tried to make it online, but it don't work. I've already change the settings of the Port rules of my router, I've added a exception for the Port which used the server in the firewall and I've install the Dyndns Updater. The Client sends data to the Server, Server sends no data to the Client. I hope you can help me.

Here is the Chat script:

#include <INet.au3>
$ip = _GetIP()
$port = 7661
TCPStartup()
$serverIP = TCPNameToIP("autoitpokemon.dyndns.org")
$serverport = 33891
AdlibRegister('recieve',5000)
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Chat Client 1", 625, 336, 10, 10)
$Edit1 = GUICtrlCreateEdit("", 16, 16, 601, 281, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL))
$Input1 = GUICtrlCreateInput("", 24, 304, 497, 21)
$Button1 = GUICtrlCreateButton("Senden", 528, 304, 89, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
$mySocket = TCPListen($ip,$port)

While 1
$con = TCPAccept($mySocket)
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $Button1
   $srvSocket = TCPConnect($serverIP, $serverport)
   If $srvSocket = -1 Then Exit
   TCPSend($srvSocket, GUICtrlRead($Input1))
   TCPCloseSocket($srvSocket)
   recieve()
  EndSwitch
If $con >=0 Then
  $rev = ''
  While True
   $rev &= TCPRecv($con,2048)
   If @error Then ExitLoop
  WEnd
  If $rev <> '' Then
   $str = $rev
   GUICtrlSetData($Edit1,$str)
  EndIf
EndIf
Sleep(50)
WEnd
Func recieve()
AdlibUnRegister()
$srvSocket = TCPConnect($serverIP, $serverport)
If $srvSocket = -1 Then Exit
TCPSend($srvSocket, 'MSGPORT#' & $port)
TCPCloseSocket($srvSocket)
AdlibRegister('recieve',5000)
EndFunc
TCPShutdown()

And here is the Server:

#include <date.au3>
TCPStartup()
$serverIP = @IPAddress1
$serverport = 33891
$srvSocket = TCPListen($serverIP,$serverport)
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Server Kontrolle", 625, 443, 700, 50)
$Edit1 = GUICtrlCreateEdit("", 16, 16, 593, 417)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $con = TCPAccept($srvSocket)
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    If $con >=0 Then
        $rev = ''
        $clientIP = SocketToIP($con)
        ConsoleWrite(@LF & $clientIP)
        While True
            $rev &= TCPRecv($con,2048)
            If @error Then ExitLoop
        WEnd
        If StringLeft($rev,8) = 'MSGPORT#' Then
            $sp = StringSplit($rev,'#')
   MsgBox(0,"",$clientIP&":"&$sp[UBound($sp) - 1])
            $cltSocket = TCPConnect($clientIP,$sp[UBound($sp) - 1])
            If $clientIP <> -1 Then
                TCPSend($cltSocket,GUICtrlRead($Edit1))
                TCPCloseSocket($cltSocket)
            EndIf
        ElseIf $rev <> '' Then
            $str = GUICtrlRead($Edit1)
            $str &=_Now() & @TAB & $clientIP & @TAB & $rev & @CRLF
            GUICtrlSetData($Edit1,$str)
        EndIf
    EndIf
Sleep(50)
WEnd

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

I will upload in 5 minutes pictures of the settings from my router.

Best regards,

Blackmental

Edited by Blackmental

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