Jump to content

Chat application


Recommended Posts

Hi all,

I was just modifying the TCPSend and the TCPRecv to fit with my needs. Everything works perfect, BUT when you close TCPSend on the computer where you are sending the message from, then TCPRecv will also close on the computer where you where sending the message to. How to solve the problem? (I want to be able to send a message when I want, so TCPRecv needs to be running all the time) My Codes:

TCPSend:

TCPStartUp()
$msg207 = Inputbox("Cliënt Kiezen", "naar welke computer moet het bericht gestuurd worden? Typ AOPEN of HP. Laat leeg om het bericht te verzenden naar de hoofdserver")
if $msg207 = "AOPEN" Then
$szIPADDRESS = "192.168.0.101" 
ElseIf $msg207 = "HP" Then
$szIPADDRESS = "192.168.0.102"
ElseIf $msg207 = "" Then
$szIPADDRESS = "192.168.0.100"
EndIf

; Set Some reusable info
;--------------------------
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
Dim $nPORT = 33891


; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1


;Attempt to connect to SERVER at its IP and PORT 33891
;=======================================================
$ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT)


Dim $szData

; If there is an error... show it
If @error Then
    MsgBox(4112,"Error","TCPConnect failed with WSA error: " & @error)
; If there is no error loop an inputbox for data
;   to send to the SERVER.
Else
;Loop forever asking for data to send to the SERVER
    While 1
    ; InputBox for data to transmit
        $szData = InputBox("Data for Server", "Enter data to transmit to the SERVER:", "", "", 275, 75)
        
    ; If they cancel the InputBox or leave it blank we exit our forever loop
        If @error Or $szData = "" Then ExitLoop
        
    ; We should have data in $szData... lets attempt to send it through our connected socket.
        TCPSend($ConnectedSocket,$szData)
        
    ; If the send failed with @error then the socket has disconnected
    ;----------------------------------------------------------------
        If @error Then ExitLoop
    WEnd
EndIfoÝ÷ Ù0Eç/jëh×6Dim $szIPADDRESS = @IPAddress1
Dim $nPORT = 33891


; Start The TCP Services
;==============================================
TCPStartUp()

; Create a Listening "SOCKET".
;   Using your IP Address and Port 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)

; If the Socket creation fails, exit.
If $MainSocket = -1 Then Exit


; Create a GUI for messages
;==============================================


; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1


;Wait for and Accept a connection
;==============================================
Do
    $ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1


; Get IP of client connecting
Dim $szIP_Accepted = SocketToIP($ConnectedSocket)

Dim $msg, $recv
; GUI Message Loop
;==============================================


; Try to receive (up to) 2048 bytes
;----------------------------------------------------------------
While 1
    $recv = TCPRecv( $ConnectedSocket, 2048 )
    
; If the receive failed with @error then the socket has disconnected
;----------------------------------------------------------------
    If @error Then ExitLoop

; Update the edit control with what we have received
;----------------------------------------------------------------
    If $recv <> "" Then MsgBox(64, "Bericht van " & $szIP_Accepted, $recv)
WEnd


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

TCPShutDown()


; 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

Thanks!

Edited by PcExpert
Link to comment
Share on other sites

Hi all,

I was just modifying the TCPSend and the TCPRecv to fit with my needs. Everything works perfect, BUT when you close TCPSend on the computer where you are sending the message from, then TCPRecv will also close on the computer where you where sending the message to. How to solve the problem? (I want to be able to send a message when I want, so TCPRecv needs to be running all the time) My Codes:

TCPSend:

TCPStartUp()
$msg207 = Inputbox("Cliënt Kiezen", "naar welke computer moet het bericht gestuurd worden? Typ AOPEN of HP. Laat leeg om het bericht te verzenden naar de hoofdserver")
if $msg207 = "AOPEN" Then
$szIPADDRESS = "192.168.0.101" 
ElseIf $msg207 = "HP" Then
$szIPADDRESS = "192.168.0.102"
ElseIf $msg207 = "" Then
$szIPADDRESS = "192.168.0.100"
EndIf

; Set Some reusable info
;--------------------------
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
Dim $nPORT = 33891
; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1
;Attempt to connect to SERVER at its IP and PORT 33891
;=======================================================
$ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT)
Dim $szData

; If there is an error... show it
If @error Then
    MsgBox(4112,"Error","TCPConnect failed with WSA error: " & @error)
; If there is no error loop an inputbox for data
;   to send to the SERVER.
Else
;Loop forever asking for data to send to the SERVER
    While 1
    ; InputBox for data to transmit
        $szData = InputBox("Data for Server", "Enter data to transmit to the SERVER:", "", "", 275, 75)
        
    ; If they cancel the InputBox or leave it blank we exit our forever loop
        If @error Or $szData = "" Then ExitLoop
        
    ; We should have data in $szData... lets attempt to send it through our connected socket.
        TCPSend($ConnectedSocket,$szData)
        
    ; If the send failed with @error then the socket has disconnected
    ;----------------------------------------------------------------
        If @error Then ExitLoop
    WEnd
EndIfoÝ÷ Ù0Eç/jëh×6Dim $szIPADDRESS = @IPAddress1
Dim $nPORT = 33891
; Start The TCP Services
;==============================================
TCPStartUp()

; Create a Listening "SOCKET".
;   Using your IP Address and Port 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)

; If the Socket creation fails, exit.
If $MainSocket = -1 Then Exit
; Create a GUI for messages
;==============================================
; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1
;Wait for and Accept a connection
;==============================================
Do
    $ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1
; Get IP of client connecting
Dim $szIP_Accepted = SocketToIP($ConnectedSocket)

Dim $msg, $recv
; GUI Message Loop
;==============================================
; Try to receive (up to) 2048 bytes
;----------------------------------------------------------------
While 1
    $recv = TCPRecv( $ConnectedSocket, 2048 )
    
; If the receive failed with @error then the socket has disconnected
;----------------------------------------------------------------
    If @error Then ExitLoop

; Update the edit control with what we have received
;----------------------------------------------------------------
    If $recv <> "" Then MsgBox(64, "Bericht van " & $szIP_Accepted, $recv)
WEnd
If $ConnectedSocket <> -1 Then TCPCloseSocket( $ConnectedSocket )

TCPShutDown()
; 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
EndFuncoÝ÷ Ù8ZK7ßú®¢×©­ëmÊkØ^éi~)^{¦¦W¬Áée«âazƦzènW¦¶0Øhê+®*m±»­ØbKaÉ¢[y«­¢+Ù¼(ÀÌØí
½¹¹ÑM½­ÐôQ
AÁÐ ÀÌØí5¥¹M½­Ð¤)U¹Ñ¥°ÀÌØí
½¹¹ÑM½­Ð±ÐìÐì´ÄoÝ÷ Úí+ºÚ"µÍÈYHXÙZ]HZ[YÚ]Ü[HÛØÚÙ]ÈØÛÛXÝYËKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKBYÜ[^]ÛÜ

sorry fo not testing it but im having f**** exams tomorrow!!!

Link to comment
Share on other sites

Client:

TCPStartUp()
$msg207 = Inputbox("Cliënt Kiezen", "naar welke computer moet het bericht gestuurd worden? Typ AOPEN of HP. Laat leeg om het bericht te verzenden naar de hoofdserver")
if $msg207 = "AOPEN" Then
$szIPADDRESS = "192.168.0.101"
ElseIf $msg207 = "HP" Then
$szIPADDRESS = "192.168.0.102"
ElseIf $msg207 = "" Then
$szIPADDRESS = "192.168.0.100"
EndIf
; Set Some reusable info
;--------------------------
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
Dim $nPORT = 33891
; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1
;Attempt to connect to SERVER at its IP and PORT 33891
;=======================================================
$ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT)
Dim $szData
; If there is an error... show it
If @error Then
    MsgBox(4112,"Error","TCPConnect failed with WSA error: " & @error)
; If there is no error loop an inputbox for data
;   to send to the SERVER.
Else
;Loop forever asking for data to send to the SERVER
    While 1
  ; InputBox for data to transmit
        $szData = InputBox("Data for Server", "Enter data to transmit to the SERVER:", "", "", 275, 75)
       
  ; If they cancel the InputBox or leave it blank we exit our forever loop
        If @error Or $szData = "" Then ExitLoop
       
  ; We should have data in $szData... lets attempt to send it through our connected socket.
        TCPSend($ConnectedSocket,$szData)
       
  ; If the send failed with @error then the socket has disconnected
  ;----------------------------------------------------------------
        If @error Then ExitLoop
    WEnd
EndIf
Func OnAutoitExit()
    TCPSend($ConnectedSocket, "ENDING_BYE")
    TCPCloseSocket($ConnectedSocket)
    TCPShutdown()
EndFunc

Server:

Dim $szIPADDRESS = @IPAddress1, $nPORT = 33891, $ConnectedSocket = -1, $msg, $recv, $szIP_Accepted
; Start The TCP Services
;==============================================
TCPStartUp()
; Create a Listening "SOCKET".
;   Using your IP Address and Port 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)
; If the Socket creation fails, exit.
If $MainSocket = -1 Then Exit
; Create a GUI for messages
;Wait for connection and convert socket into IP
Listen()
; GUI Message Loop
;==============================================
; Try to receive (up to) 2048 bytes
;----------------------------------------------------------------
While 1
    $recv = TCPRecv( $ConnectedSocket, 2048 )
 ; If the receive failed with @error then the socket has disconnected
;----------------------------------------------------------------
    If @error Then ExitLoop
; Update the edit control with what we have received
;----------------------------------------------------------------
If $recv = "ENDING_BYE" Then 
    Listen()
    Else
    If $recv <> "" Then MsgBox(64, "Bericht van " & $szIP_Accepted, $recv)
    EndIf
WEnd
If $ConnectedSocket <> -1 Then TCPCloseSocket( $ConnectedSocket )
TCPShutDown()
; 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
Func Listen()
;Wait for and Accept a connection
;==============================================
Do
    $ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1
; Get IP of client connecting
Dim $szIP_Accepted = SocketToIP($ConnectedSocket)
EndFunc
Edited by poisonkiller
Link to comment
Share on other sites

Now everything works fine I want to make another modification to TCPRecv

I want to be able to shutdown computers across the network. So if i send the text "shutdown" or something simmular is shuts the computer down. Please take a look at the code below. Can this work?

#NoTrayIcon
Dim $szIPADDRESS = @IPAddress1, $nPORT = 33891, $ConnectedSocket = -1, $msg, $recv, $szIP_Accepted
; Start The TCP Services
;==============================================
TCPStartUp()
; Create a Listening "SOCKET".
;   Using your IP Address and Port 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)
; If the Socket creation fails, exit.
If $MainSocket = -1 Then Exit
; Create a GUI for messages
;Wait for connection and convert socket into IP
Listen()
; GUI Message Loop
;==============================================
; Try to receive (up to) 2048 bytes
;----------------------------------------------------------------
While 1
    $recv = TCPRecv( $ConnectedSocket, 2048 )
; If the receive failed with @error then the socket has disconnected
;----------------------------------------------------------------
    If @error Then ExitLoop
; Update the edit control with what we have received
;----------------------------------------------------------------
If $recv = "ENDING_BYE" Then 
    Listen()
Else
 if $recv = "shudown" Then
 shutdown(1)
    If $recv <> "" Then MsgBox(64, "Bericht van " & $szIP_Accepted, $recv)
    EndIf
 EndIf
WEnd
If $ConnectedSocket <> -1 Then TCPCloseSocket( $ConnectedSocket )
TCPShutDown()
; 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
Func Listen()
;Wait for and Accept a connection
;==============================================
Do
    $ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1
; Get IP of client connecting
Dim $szIP_Accepted = SocketToIP($ConnectedSocket)
EndFunc

See line 31 for my modifycation

Thanks!

Link to comment
Share on other sites

That should work, but just now i had another idea:

Select
Case $recv = "ENDING_BYE"
    Listen()
Case $recv = "shutdown"
 shutdown(1)
Case $recv <> ""
MsgBox(64, "Bericht van " & $szIP_Accepted, $recv)
 EndSelect

This is for replacing If...EndIf

Edited by poisonkiller
Link to comment
Share on other sites

Hmm... Try adding TCPCloseSocket($ConnectedSocket) before Listen() function to close that socket. I modified server code a little:

#NoTrayIcon
Dim $szIPADDRESS = @IPAddress1, $nPORT = 33891, $ConnectedSocket = -1, $MainSocket, $msg, $recv, $szIP_Accepted
; Start The TCP Services
;==============================================
TCPStartUp()
; Create a GUI for messages
;Wait for connection and convert socket into IP
Listen()
; GUI Message Loop
;==============================================
; Try to receive (up to) 2048 bytes
;----------------------------------------------------------------
While 1
$recv = TCPRecv( $ConnectedSocket, 2048 )
; If the receive failed with @error then the socket has disconnected
;----------------------------------------------------------------
    If @error Then ExitLoop
; Update the edit control with what we have received
;----------------------------------------------------------------
Select
Case $recv = "ENDING_BYE"
        TCPCloseSocket($ConnectedSocket)
        TCPCloseSocket($MainSocket)
    Listen()
Case $recv = "shudown"
 shutdown(1)
Case $recv <> ""
    MsgBox(64, "Bericht van " & $szIP_Accepted, $recv)
EndSelect
WEnd
If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
TCPShutDown()
; 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
Func Listen()
; Create a Listening "SOCKET".
;   Using your IP Address and Port 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)
; If the Socket creation fails, exit.
If $MainSocket = -1 Then Exit
;Wait for and Accept a connection
;==============================================
Do
    $ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1
; Get IP of client connecting
Dim $szIP_Accepted = SocketToIP($ConnectedSocket)
EndFunc

EDIT: I just noticed, in client code, you have to declare sockets variables.

Edited by poisonkiller
Link to comment
Share on other sites

Everything works fine now, so I thought that it would be nice if you could select the client with radiobuttons. But it doesn't work. It cant connect. My old client code works fine, but this one doesn't. Whats the problem?

;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
; see TCPRecv example
#include <GUIConstants.au3>
#NoTrayIcon
; Start The TCP Services
;==============================================
TCPStartUp()

$gui027 = GUICreate("My GUI radio", 200, 100) ; will create a dialog box that when displayed is centered

$radio1 = GUICtrlCreateRadio ("SERVER", 10, 10, 120, 20)
$radio2 = GUICtrlCreateRadio ("AOPEN", 10, 40, 120, 20)
$radio3 = GUICtrlCreateRadio ("HP", 10, 70, 120, 20)
GUISetState (@SW_SHOW)

GUISetState ()      ; will display an  dialog box with 1 checkbox

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $radio1 And BitAND(GUICtrlRead($radio1), $GUI_CHECKED) = $GUI_CHECKED
            GUISetState(@SW_HIDE, $gui027)
   $szIPADDRESS = "192.168.0.100"
   TCPConnect1()
   ; Set Some reusable info
;--------------------------
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address

        Case $msg = $radio2 And BitAND(GUICtrlRead($radio2), $GUI_CHECKED) = $GUI_CHECKED
            GUISetState(@SW_HIDE, $gui027)
   $szIPADDRESS = "192.168.0.101"
   TCPConnect1()
  
  Case $msg = $radio3 And BitAND(GUICtrlRead($radio2), $GUI_CHECKED) = $GUI_CHECKED
            GUISetState(@SW_HIDE, $gui027)
   $szIPADDRESS = "192.168.0.102" 
   TCPConnect1()
    EndSelect
Wend

Func tcpconnect1()
 
 ; Set Some reusable info
;--------------------------
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
Dim $nPORT = 33891
; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1
;Attempt to connect to SERVER at its IP and PORT 33891
;=======================================================
$ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT)
Dim $szData
; If there is an error... show it
If @error Then
    MsgBox(4112,"Error","TCPConnect failed with WSA error: " & @error)
; If there is no error loop an inputbox for data
;   to send to the SERVER.
Else
;Loop forever asking for data to send to the SERVER
    While 1
  ; InputBox for data to transmit
        $szData = InputBox("Data for Server", "Enter data to transmit to the SERVER:", "", "", 275, 75)
       
  ; If they cancel the InputBox or leave it blank we exit our forever loop
        If @error Or $szData = "" Then ExitLoop
       
  ; We should have data in $szData... lets attempt to send it through our connected socket.
        TCPSend($ConnectedSocket,$szData)
       
  ; If the send failed with @error then the socket has disconnected
  ;----------------------------------------------------------------
        If @error Then ExitLoop
  WEnd 
  EndIf
 EndFunc

Thanks!

Link to comment
Share on other sites

Try replacing this line:

Case $msg = $radio1 And BitAND(GUICtrlRead($radio1), $GUI_CHECKED) = $GUI_CHECKED

With this:

Case GUICtrlRead($radio1) = $GUI_CHECKED

And replace radio2 and radio3 lines too.

EDIT: 200th post! :)

Edited by poisonkiller
Link to comment
Share on other sites

Congrats! :)

It still doesn't work. I'm gonna try it with buttons, i think.

:) buttons don't work either.

Why doesn't the buttons and radiobuttons work?

Edited by PcExpert
Link to comment
Share on other sites

This works for me:

;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
; see TCPRecv example
#include <GUIConstants.au3>
#NoTrayIcon
; Start The TCP Services
;==============================================
TCPStartUp()

$gui027 = GUICreate("My GUI radio", 200, 100); will create a dialog box that when displayed is centered

$radio1 = GUICtrlCreateRadio ("SERVER", 10, 10, 120, 20)
$radio2 = GUICtrlCreateRadio ("AOPEN", 10, 40, 120, 20)
$radio3 = GUICtrlCreateRadio ("HP", 10, 70, 120, 20)
GUISetState (@SW_SHOW)

GUISetState ()   ; will display an  dialog box with 1 checkbox

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $radio1 And BitAND(GUICtrlRead($radio1), $GUI_CHECKED) = $GUI_CHECKED
            GUISetState(@SW_HIDE, $gui027)
   TCPConnect1("192.168.0.100")
  ; Set Some reusable info
;--------------------------
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address

        Case $msg = $radio2 And BitAND(GUICtrlRead($radio2), $GUI_CHECKED) = $GUI_CHECKED
            GUISetState(@SW_HIDE, $gui027)
   TCPConnect1("192.168.0.101")
 
  Case $msg = $radio3 And BitAND(GUICtrlRead($radio2), $GUI_CHECKED) = $GUI_CHECKED
            GUISetState(@SW_HIDE, $gui027)
   TCPConnect1("192.168.0.102")
    EndSelect
Wend

Func TCPConnect1($szIPADDRESS)
 
; Set Some reusable info
;--------------------------
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
Dim $nPORT = 33891
; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1
;Attempt to connect to SERVER at its IP and PORT 33891
;=======================================================
$ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT)
Dim $szData
; If there is an error... show it
If @error Then
    MsgBox(4112,"Error","TCPConnect failed with WSA error: " & @error)
; If there is no error loop an inputbox for data
;   to send to the SERVER.
Else
;Loop forever asking for data to send to the SERVER
    While 1
 ; InputBox for data to transmit
        $szData = InputBox("Data for Server", "Enter data to transmit to the SERVER:", "", "", 275, 75)
       
 ; If they cancel the InputBox or leave it blank we exit our forever loop
        If @error Or $szData = "" Then ExitLoop
       
 ; We should have data in $szData... lets attempt to send it through our connected socket.
        TCPSend($ConnectedSocket,$szData)
       
 ; If the send failed with @error then the socket has disconnected
 ;----------------------------------------------------------------
        If @error Then ExitLoop
  WEnd
  EndIf
 EndFunc
Link to comment
Share on other sites

This code doesn't work fine either. My old code, this one:

;SERVER!! Start Me First !!!!!!!!!!!!!!!
#NoTrayIcon
Dim $szIPADDRESS = @IPAddress1, $nPORT = 33891, $ConnectedSocket = -1, $MainSocket, $msg, $recv, $szIP_Accepted
; Start The TCP Services
;==============================================
TCPStartUp()
; Create a GUI for messages
;Wait for connection and convert socket into IP
Listen()
; GUI Message Loop
;==============================================
; Try to receive (up to) 2048 bytes
;----------------------------------------------------------------
While 1
$recv = TCPRecv( $ConnectedSocket, 2048 )
; If the receive failed with @error then the socket has disconnected
;----------------------------------------------------------------
    If @error Then ExitLoop
; Update the edit control with what we have received
;----------------------------------------------------------------
Select
Case $recv = "ENDING_BYE"
        TCPCloseSocket($ConnectedSocket)
        TCPCloseSocket($MainSocket)
    Listen()
Case $recv = "#shutdown"
shutdown(1)
Case $recv = "#reboot"
shutdown(2)
Case $recv = "#blocky"
BlockInput(1)
Case $recv = "#blockn"
BlockInput(0)
Case $recv <> ""
MsgBox(64, "Bericht van " & $szIP_Accepted, $recv)
EndSelect
WEnd
If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
TCPShutDown()
; 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
Func Listen()
; Create a Listening "SOCKET".
;   Using your IP Address and Port 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)
; If the Socket creation fails, exit.
If $MainSocket = -1 Then Exit
;Wait for and Accept a connection
;==============================================
Do
    $ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1
; Get IP of client connecting
Dim $szIP_Accepted = SocketToIP($ConnectedSocket)
EndFunc

What could be the problem that the version with an inputbox works fine and the version with the buttons/radiobuttons doesn't?

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...