Jump to content
Sign in to follow this  
RexRomae

[Socket] Client <-> Server - Problems HELP Please!

Recommended Posts

RexRomae

Hello!

I have a problem with Client and Server connection...

My program have to send a message from Client to Server and if the message are recived the server should have to reply client the result ("ok i have read your message! byebye from Server"). But the reply don't arrive to client or it isn't ready to listen.

Can you help me? Thanks!!!

Client.au3

Dim $porta = 33891
;$ip_server = InputBox("Ip Server", "Inserisci l'ip della macchina a cui ti vuoi collegare:")
$ip_server = @IPAddress1

TCPStartUp()

Dim $ConnectS1 = -1

$ConnectS1 = TCPConnect ($ip_server,$porta)

Dim $szData

If @error Then
    MsgBox(4112,"Error","Connessiona fallita con "&$ip_server)
Else
    While 1
        $szData = InputBox("Invio",@LF & @LF & "Scrivi:")
        TCPSend($ConnectS1,$szData); invio il comando
        If $szData == "" Then ExitLoop
        TCPStartUp()
            $socket1 = TCPListen ($ip_server, $porta)
            If $socket1 = -1 Then Exit
            Dim $ConnectS2 = -1
            Do
                $ConnectS2 = TCPAccept($socket1)
            Until $ConnectS <> -1
            
            Dim $ipAccettato = SocketToIP($ConnectS2)
            
            Dim $ricevuto
            
            While 1         
        ; Tenta di ricevere  dal client 2048 bytes
            $ricevuto = TCPRecv( $ConnectS2, 2048 )
        ; Se la ricezione ha luogo etc etc etc
            if $ricevuto <> "" Then
                MsgBox(0,"Message "&$ipAccettato,$ricevuto)
            EndIf
WEnd

    WEnd
EndIf


; ///////////////////////////////////////////////////////////////////////////////////////////////////
; FUNZIONI
; ///////////////////////////////////////////////////////////////////////////////////////////////////


; Function to return IP Address from a connected socket.
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

Server.au3

Dim $porta = 33891
Dim $ip = @IPAddress1
$ipAccettato = $ip

; Inizializzo il TCP
TCPStartUp()

; Mi metto in ascolto
$socket1 = TCPListen ( $ip, $porta)

; Se la conenssione nn avviene Esci
If $socket1 = -1 Then Exit
    
; Inizializzo la variabile che rappresenta la connessione
Dim $ConnectS1 = -1
Do
    $ConnectS1 = TCPAccept($socket1)
Until $ConnectS1 <> -1

Dim $ipAccettato = SocketToIP($ConnectS1)
$ipAccettato = $ip

Dim $ricevuto

While 1
; Tenta di ricevere  dal client 2048 bytes
    $ricevuto = TCPRecv( $ConnectS1, 2048 )

; Se la ricezione ha luogo etc etc etc
    if $ricevuto <> "" Then
        MsgBox(0,"Message: "&$ipAccettato,$ricevuto)
        
        
        TCPStartUp()
        
        Dim $ConnectS2 = -1
        
        $ConnectS2 = TCPConnect ($ipAccettato,$porta)
        
        Dim $szData
        
        If @error Then
            MsgBox(4112,"Error","Connessiona fallita con "&$ipAccettato)
        Else
            While 1
                $szData = "fatto"
                TCPSend($ConnectS2,$szData); invio il comando
            WEnd
        EndIf
    EndIf
WEnd


; ///////////////////////////////////////////////////////////////////////////////////////////////////
; FUNZIONI
; ///////////////////////////////////////////////////////////////////////////////////////////////////


; Function to return IP Address from a connected socket.
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

Share this post


Link to post
Share on other sites
evilertoaster

You might be missing some basic TCP/IP functionality knowledge...

You don't need to connect both of them in any case... try something like this-

client:

Dim $porta = 33891

$ip_server = @IPAddress1

TCPStartUp()

Dim $ConnectS1 = -1

$ConnectS1 = TCPConnect ($ip_server,$porta)
Dim $ipAccettato = SocketToIP($ConnectS1)
Dim $szData

If @error Then
    MsgBox(4112,"Error","Connessiona fallita con "&$ip_server)
Else
    While 1
        $szData = InputBox("Invio",@LF & @LF & "Scrivi:")
        TCPSend($ConnectS1,$szData)
        If $szData == "" Then ExitLoop
        While 1
            $ricevuto = TCPRecv( $ConnectS1, 2048 )
            if $ricevuto <> "" Then MsgBox(0,"Message "&$ipAccettato,$ricevuto)
        WEnd
    WEnd
EndIf

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*",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

Share this post


Link to post
Share on other sites
RexRomae

Thank you very much

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  

×