Jump to content

tcp problems


Recommended Posts

please help

the server receives the messages from the client

but the problem is that the server receives messages only if the client is in the same computer...

where is the problem? should i use external IPs?

SERVER:

#include <GUIConstants.au3>

$waitin_ip = InputBox("enter", "ip to connect to..")


Dim $nPORT = 33891
Dim $szIPADDRESS = $waitin_ip

TCPStartUp()

$MainSocket = TCPListen($szIPADDRESS, $nPORT)

if $mainsocker = -1 then exit

Dim $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")",300,200)
Dim $edit = GUICtrlCreateEdit("",10,10,280,180, $ES_READONLY + $WS_VSCROLL)
GUISetState()

Dim $ConnectedSocket = -1

Do
    $ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1

Dim $szIP_Accepted = SocketToIP($ConnectedSocket)

Dim $msg, $recv

While 1
   $msg = GUIGetMsg()

    If $msg = $GUI_EVENT_CLOSE Then ExitLoop

    $recv = TCPRecv( $ConnectedSocket, 2048 )
    
    If @error Then ExitLoop

    If $recv <> "" Then GUICtrlSetData($edit, _
            $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
WEnd



If $ConnectedSocket <> -1 Then TCPCloseSocket( $ConnectedSocket )

TCPShutDown()



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

    Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _
            "ptr",DLLStructGetPtr($sockaddr),"int_ptr",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

thanks in advance...

Link to comment
Share on other sites

Here's both server and client, fixed:

Server:

#include <GUIConstants.au3>
Dim $nPORT = 33891
Dim $szIPADDRESS = @IPAddress1
TCPStartUp()
$MainSocket = TCPListen($szIPADDRESS, $nPORT)
if $MainSocket = -1 then exit
$GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")",300,200)
$edit = GUICtrlCreateEdit("",10,10,280,180, $ES_READONLY + $WS_VSCROLL)
GUISetState()
Do
    $ConnectedSocket = TCPAccept($MainSocket)
    $msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then
    TCPShutdown()
    Exit
EndIf
Until $ConnectedSocket <> -1
Dim $szIP_Accepted = SocketToIP($ConnectedSocket)
While 1
   $msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
$recv = TCPRecv( $ConnectedSocket, $nPORT)
   If @error Then ExitLoop
If $recv <> "" Then GUICtrlSetData($edit, $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
WEnd
If $ConnectedSocket <> -1 Then TCPCloseSocket( $ConnectedSocket )
TCPShutDown()
Func SocketToIP($SHOCKET)
    Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]")
Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _
            "ptr",DLLStructGetPtr($sockaddr),"int_ptr",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

Client:

#include <GUIConstants.au3>
TCPStartUp()
Dim $ipconnected = InputBox("Connect", "Write the ip of the computer that you would like to connect to...", @IPAddress1);Here write that computers IP where server is running
Dim $szIPADDRESS = $ipconnected
Dim $nPORT = 33891
Dim $ConnectedSocket = -1
$ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT)
If @error Then
    MsgBox(0,"Error","Cannot connect to server.")
    Exit
EndIf
$GOOEY = GUICreate("Connect to server (IP: " & $szIPADDRESS & ")",400,40)
$szData = GUICtrlCreateInput("", 10, 10, 300, 25)
$send = GUICtrlCreateButton("Send", 320, 10, 60, 25)
GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exitloop   
        Case $msg = $send        
       TCPSend($ConnectedSocket, GUICtrlRead($szData))
        EndSelect
    WEnd
    TCPCloseSocket($ConnectedSocket)
    TCPShutdown()

Btw, I tried to post this in your another topic, but it was locked before i could finish posting. :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...