Jump to content

General Server/Client Chat


Recommended Posts

I dont plan on using GUI's with this as of now im taking baby steps in learning so help where you can.

Server:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.0.0
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------
; Host or Ip to Connect to
$Server_IP = "127.0.0.1"
$Server_Port = "65432"

; Start The TCP Services
TCPStartup ( ) ; To start TCP services

; Time to Listen for Connections
$MainSocket = TCPListen($Server_IP, $Server_Port,  100 )
If $MainSocket = -1 Then Exit

; Accept a Incoming Connection
While 1
$ConnectedSocket = TCPAccept( $MainSocket)
If $ConnectedSocket >= 0 Then
    $ClientIp = @IpAddress1
            msgbox(0,"Server","Client" & " " & $ClientIp & " " & "Connected!")
EndIf
WEnd

; Server will Run continously

Client:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.0.0
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------
; Host or Ip to Connect to
$Server_IP = "127.0.0.1"
$Sever_Port = "65432"

; Start The TCP Services
TCPStartup ( ) ; To start TCP services

; Connect to The Server
$socket = TCPConnect( $Server_IP, $Sever_Port )
If $socket = -1 Then 
    MsgBox(0, "Client", "Unable to Contact Server!")
    Exit
EndIf

If $socket >= 0 Then
    msgbox(0,"Client","Successfully Connected to the Server!")
EndIf

While 1
; InputBox for data to transmit
$data = InputBox("Client", "Message to Send to Server")

; If they cancel the InputBox or leave it blank we exit our forever loop
If @error Or $data = "" Then 
    Exit
EndIf

; We should have data in $data, lets attempt to send it through our connected socket.
TCPSend($socket, $data)

WEnd

Im looking for a way to Send whatever the Client types into the InputBox -> to the Server and have it displayed in a Message Box. What I've tried so far hasn't worked so I thought to ask you.

Link to comment
Share on other sites

I wrote for you one client and one server.

SERVER

HotKeySet("{PAUSE}","Quit");Press PAUSE key to exit

Global $SERVER_IP = @IPAddress1
Global $SERVER_PORT = 65432
Global $SERVER

TCPStartup()
$SERVER = TCPListen($SERVER_IP,$SERVER_PORT,4)
If @error Then Exit
Do
    $SOCKET = TCPAccept($SERVER)
    Sleep(10)
Until $SOCKET > 0

While True
    $RECV = TCPRecv($SOCKET,256)
    If $RECV <> "" Then
        MsgBox(0,"Received data","Client:" & $RECV)
        TCPSend($SOCKET,"Your message has arrive")
    EndIf
    Sleep(20)
WEnd

Func Quit()
    TCPCloseSocket($SERVER)
    TCPShutdown()
    Exit
EndFunc

CLIENT

HotKeySet("^s","SendMsg");Press CTRL+S to send a message to server.
HotKeySet("^q","Quit");Press CTRL+Q to quit

Global $SERVER_IP = @IPAddress1
Global $SERVER_PORT = "65432"
Global $CLIENT

TCPStartup()
$CLIENT = TCPConnect($SERVER_IP,$SERVER_PORT)
If @error Then
    MsgBox(0, "Client", "Unable to Contact Server!")
    Exit
Else
    TrayTip("Client","Successfully Connected to the Server!",1)
EndIf

While True
    $RECV = TCPRecv($CLIENT,256)
    If $RECV <> "" Then TrayTip("Server Message",$RECV,1)
    Sleep(20)
WEnd

Func SendMsg()
    $MESSAGE = InputBox("CLIENT","Type here a message to server:","TEST")
    TCPSend($CLIENT,$MESSAGE)
EndFunc

Func Quit()
    TCPCloseSocket($CLIENT)
    TCPShutdown()
    Exit
EndFunc

Hope that help you.

When the words fail... music speaks.

Link to comment
Share on other sites

Updated Code:

Server:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.0.0
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------
HotKeySet("{PAUSE}","Quit");Press PAUSE key to exit

Global $SERVER_IP = "127.0.0.1"
Global $SERVER_PORT = 65432
Global $SERVER

TCPStartup()
$SERVER = TCPListen($SERVER_IP,$SERVER_PORT,4)
If @error Then Exit
Do
    $SOCKET = TCPAccept($SERVER)
    Sleep(10)
Until $SOCKET > 0

If $SOCKET > 0 Then
    $ClientIP = @IPAddress1
    $ClientUN = @UserName
    $ClientCN = @ComputerName
    $ClientOS = @OSVersion
    MsgBox(0,"Server", $ClientUN & " on " & $ClientCN & " running " & $ClientOS & " has connected with the following Ip Address:" & $ClientIP )
EndIf

While True
    $RECV = TCPRecv($SOCKET,256)
    If $RECV <> "" Then
        MsgBox(0,"Server: Message from Client", $RECV)
        TCPSend($SOCKET,"Your message was received!")
    EndIf
    Sleep(20)
WEnd

Func Quit()
    TCPCloseSocket($SERVER)
    TCPShutdown()
    Exit
EndFunc

Client:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.0.0
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------
HotKeySet("^s","SendMsg");Press CTRL+S to send a message to server.
HotKeySet("^q","Quit");Press CTRL+Q to quit

Global $SERVER_IP = "127.0.0.1"
Global $SERVER_PORT = "65432"
Global $SOCKET

; Start The TCP Services
TCPStartUp()

; Connect to The Server
$socket = TCPConnect( $SERVER_IP, $SERVER_PORT )
If $socket = -1 Then Exit
    
If @error Then
    MsgBox(0, "Client", "Unable to Contact Server!")
    Exit
Else
    MsgBox(0, "Client","Successfully Connected to the Server!",1)
EndIf

While True
    $RECV = TCPRecv($SOCKET,256)
    If $RECV <> "" Then 
        MsgBox(0, "Client", $RECV,1)
    Sleep(20)
EndIF
WEnd

Func SendMsg()
    $MESSAGE = InputBox("Client","Message to Send to Server:","Your Message Here")
    TCPSend($SOCKET,$MESSAGE)
EndFunc

Func Quit()
    TCPCloseSocket($SOCKET)
    TCPShutdown()
    Exit
EndFunc

Thanks to Andreik for Contributions.

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