Jump to content

TCP Chatting


Recommended Posts

Hi there

im trying to make a TCP Chat

heres the first program source

Opt('MustDeclareVars', 1)

Local $msg, $recv, $Delmod
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891
Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted, $Label1, $Label2, $KeyMod, $Delmod, $ConnectedSocket2, $WS_GROUP, $Button1, $msg
$GOOEY = GUICreate("Start1", 300, 249, 192, 125)
$Button1 = GUICtrlCreateButton("Send", 16, 200, 137, 33, $WS_GROUP)
$edit = GUICtrlCreateEdit("", 10, 58, 280, 132)
GUISetState()
Example()

Func Example()
    Local $ConnectedSocket, $szData, $ConnectedSocket2, $MainSocket
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 33891, $nPORTl = 33892
    TCPStartup()

    $MainSocket = TCPListen($szIPADDRESS, $nPORTl)
    If $MainSocket = -1 Then Exit


    $ConnectedSocket = -1

    Do
        Sleep(100)
        $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)
        $ConnectedSocket2 = TCPAccept($MainSocket)
    Until Not @error And $ConnectedSocket2 <> -1

    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
    Else
        While 1
            $msg = GUIGetMsg()

            $recv = TCPRecv($ConnectedSocket2, 2048)

            If @error Then ExitLoop
            If $recv <> "" Then GUICtrlSetData($edit, $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
            If $msg = $Button1 Then
                $szData = InputBox("Data for Start2", @LF & @LF & "Enter data to transmit to the SERVER:")
                If @error Or $szData = "" Then ExitLoop
                TCPSend($ConnectedSocket, $szData)
                If @error Then ExitLoop
            EndIf
        WEnd
    EndIf
EndFunc   ;==>Example

and heres the 2nd one

Opt('MustDeclareVars', 1)

Local $msg, $recv, $Delmod
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891
Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted, $Label1, $Label2, $KeyMod, $Delmod, $ConnectedSocket2, $WS_GROUP, $Button1, $msg
$GOOEY = GUICreate("Start2", 300, 249, 192, 125)
$Button1 = GUICtrlCreateButton("Send", 16, 200, 137, 33, $WS_GROUP)
$edit = GUICtrlCreateEdit("", 10, 58, 280, 132)
GUISetState()
Example()

Func Example()
    Local $ConnectedSocket, $szData, $ConnectedSocket2, $MainSocket
    Local $szIPADDRESS = @IPAddress1
    Local $nPORTl = 33891, $nPORT = 33892
    TCPStartup()

    $MainSocket = TCPListen($szIPADDRESS, $nPORTl)
    If $MainSocket = -1 Then Exit


    $ConnectedSocket = -1

    Do
        Sleep(100)
        $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)
        $ConnectedSocket2 = TCPAccept($MainSocket)
    Until Not @error And $ConnectedSocket2 <> -1

    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
    Else
        While 1
            $msg = GUIGetMsg()

            $recv = TCPRecv($ConnectedSocket2, 2048)

            If @error Then ExitLoop
            If $recv <> "" Then GUICtrlSetData($edit, $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
            If $msg = $Button1 Then
                $szData = InputBox("Data for Start1", @LF & @LF & "Enter data to transmit to the SERVER:")
                If @error Or $szData = "" Then ExitLoop
                TCPSend($ConnectedSocket, $szData)
                If @error Then ExitLoop
            EndIf
        WEnd
    EndIf
EndFunc   ;==>Example

now the problem is, only the first lunched program can send to the other one

so if i lunched "start1" first, it only can successfully send data to "start2", same thing happens when i lunch "start2" first

but i dont understand where is the problem

both of the programs are running on the same pc

any help ? :blink:

ty

Link to comment
Share on other sites

the thing about tcp is that you can send data back and forth on the same socket so you don't need both

TCPConnect()
TCPAccept()

in both scripts... one should be the server and another the client (one has TCPListen and TCPAccept and the other TCPConnect)

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Link to comment
Share on other sites

yea thx

but im trying to make both of them, send and receive at the same time

Read the last post again. You can send and receive with both scripts even if they do not contain both TcpConnect() and TcpAccept().

In other words, when script1 TcpConnects to script2 which TcpAccepts, both scripts can send and receive data.

If you want to set up a chat between more than 2 people, you will still have 1 server that TcpAccepts all the other programs TcpConnects. Whenever this server receives a message from one of its clients it has to send that message to all the remaining clients for them to accept it.

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