Jump to content

TCP question


waltv7
 Share

Recommended Posts

How do I detect a computer connecting to my server(code below) and then TCPSend the contents of welcome.txt file to the person that connected. The person connecting will be useing a standard tellnet or MUD client to connect to the server.

Global $Recvm
Global $CurrentSocket = 0 
Global $ListenSocket

;#include <GUIConstantsEx.au3>
; Create a GUI for messages
   ;==============================================
   ;$GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
   ;$edit = GUICtrlCreateEdit("", 10, 10, 280, 180)

$TCP = TCPStartup()
If $TCP = 0 Then
    MsgBox(0, "Error", "Unable to startup TCP Services!")
    Exit
EndIf

$ListenSocket = TCPListen(@IPAddress1,1777,16)
If $ListenSocket = -1 Then
    MsgBox(0, "ERROR", "Unable to start listening on port 1777")
    Exit
EndIf
    
While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = $CurrentSocket + 1
    EndIf
    For $INDEX = 0 To 15
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Recv = TCPRecv($ConnectedSocket[$INDEX],1024)
            If $Recv <> "" Then
                MsgBox(0,"CON#" & $INDEX,$Recv)
            EndIf
        EndIf
    Next
    Sleep(20)

WEnd
Link to comment
Share on other sites

Right after you accept the client, you can just use TCPSend to send information to that client.

While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = $CurrentSocket + 1
    EndIf

    ; -------- ADD WHATEVER YOU WANT TO SEND HERE
    TCPSend($ConnectedSocket[$CurrentSocket], FileRead("welcome.txt"))
    ; -------------------------------------------------

    For $INDEX = 0 To 15
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Recv = TCPRecv($ConnectedSocket[$INDEX],1024)
            If $Recv <> "" Then
                MsgBox(0,"CON#" & $INDEX,$Recv)
            EndIf
        EndIf
    Next
    Sleep(20)

WEnd
Link to comment
Share on other sites

Right after you accept the client, you can just use TCPSend to send information to that client.

While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = $CurrentSocket + 1
    EndIf

    ; -------- ADD WHATEVER YOU WANT TO SEND HERE
    TCPSend($ConnectedSocket[$CurrentSocket], FileRead("welcome.txt"))
    ; -------------------------------------------------

    For $INDEX = 0 To 15
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Recv = TCPRecv($ConnectedSocket[$INDEX],1024)
            If $Recv <> "" Then
                MsgBox(0,"CON#" & $INDEX,$Recv)
            EndIf
        EndIf
    Next
    Sleep(20)

WEnd
Just tested this and I didn't get the message from welcome.txt on the client.
Link to comment
Share on other sites

Heres A Really Good Example Of A Working TCP Server And Client.. :D; Hope It Helps! :o

Source : Server.Au3

; Start Tcp 
TcpStartUp ()

; Set Variables
Global $Connected_Socket = ('-1')
Global $IP = @IpAddress1 ; This Is The Local IP..  
Global $Port = ('1234')
Global $Server = TcpListen ($IP, $Port, '1')

; Check For Error..
If $Server = ('-1') Then Exit MsgBox ('16','Error Code 01','Cannot Start Server On IP : ' & $IP & ', Port : ' & $Port, '0')

; Wait For Client Connection..
_Accept_Connection ()

; Wait.. 
Sleep ('1000')

; Send Data From Welcome.txt 
If FileExists ('Welcome.txt') Then 
$Welcome_Msg = FileRead ('Welcome.txt')
Else
$Welcome_Msg = ('Welcome.. To My Server')
EndIf 

TcpSend ($Connected_Socket, $Welcome_Msg)

; Wait..
Sleep ('2000')

; Tell The Client To Exit 
TcpSend ($Connected_Socket, '/Command.Exit')

; Disconnect Client.. " Close Connection "
TcpCloseSocket ($Connected_Socket)

; Exit.. 
Exit 

Func _Accept_Connection ()
Do 
Sleep ('10')
$Connected_Socket = TcpAccept ($Server)
Until $Connected_Socket <> ('-1')
EndFuncoÝ÷ Ù*.­Ç'§´·jëh×6; Start Tcp 
TcpStartUp ()

; Set Variables
Global $Connected_Socket = ('-1')
Global $IP = @IpAddress1 ; This Is The Local IP..  
Global $Port = ('1234')
Global $Connect = TcpConnect ($IP, $Port)

; Check For Error..
If $Connect = ('-1') Then Exit MsgBox ('16','Error Code 01','Cannot Connect To Server On.. IP : ' & $IP & ', Port : ' & $Port, '0')
    
While ('1')
; Make CPU Usage 0.. 
Sleep ('150')
; Recv Data..
$Data = TcpRecv ($Connect, '1000')
; Filter For " /Command.Exit "
If StringInStr ($Data, '/Command.Exit') = '1' Then 
TcpCloseSocket ($Connect)
Exit 
; Else Display Data 
ElseIf $Data <> '' Then 
MsgBox ('0','Notice - Data','Server Say(s) : ' & $Data, '0')
EndIf 
WEnd

If You Need More Help.. Post A Message, Or If You Have An Msn Add Me : Pk4Fun@Live.Com :)

Latest Projects :- New & Improved TCP Chat

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