Jump to content

Simple Server / Client


Recommended Posts

I have absolutely no error checking at the moment, just trying to get the base working. I'm assuming my problem is in the server, but I can't spot it...

Also how can I change the title of a form / gui at run time?

Server:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$frmMain = GUICreate("Server - Not Connected", 375, 304, 192, 124)
$txtPort = GUICtrlCreateInput("", 35, 7, 46, 21)
$Label1 = GUICtrlCreateLabel("Port:", 5, 10, 26, 17)
$btnListen = GUICtrlCreateButton("&Listen", 215, 5, 75, 25)
$edtMain = GUICtrlCreateEdit("", 5, 35, 365, 264)
$btnExit = GUICtrlCreateButton("&Exit", 295, 5, 75, 25)
GUISetState(@SW_SHOW)

Local $sockMain = "", $sockClient = 0

If TCPStartup() <> 1 Then Exit

While 1
    If $sockMain = "" Then
        $sockMain = TCPListen("127.0.0.1", GUICtrlRead($txtPort))
    Else
        If $sockClient = 0 Then
            $sockClient = TCPAccept($sockMain)
        Else
            $szData = TCPRecv($sockClient, 255)
            GUICtrlSetData($edtMain, $szData)
        EndIf
    EndIf
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            If $sockMain <> "" Then TCPCloseSocket($sockMain)
            TCPShutdown()
            Exit
        Case $btnListen
            If GUICtrlRead($txtPort) = "" Then
                MsgBox(0, "Server", "You must enter a port to listen on!")
            Else
                $sockMain = TCPListen(@IPAddress1, GUICtrlRead($txtPort))
                If $sockMain = -1 Then
                    MsgBox(0, "Server", "Failed to set listening state!")
                Else
                    GUICtrlSetData($frmMain, "Server - Listening")
                EndIf
            EndIf
        EndSwitch
WEnd

Client

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$frmMain = GUICreate("Client", 402, 62, 192, 124)
$Label1 = GUICtrlCreateLabel("IP:", 5, 10, 17, 17)
$txtIp = GUICtrlCreateInput("", 25, 7, 121, 21)
$Label2 = GUICtrlCreateLabel("Port:", 160, 10, 26, 17)
$txtPort = GUICtrlCreateInput("", 190, 7, 121, 21)
$btnConnect = GUICtrlCreateButton("&Connect", 325, 5, 75, 25)
$txtSend = GUICtrlCreateInput("", 5, 35, 316, 21)
$btnSend = GUICtrlCreateButton("&Send", 335, 35, 65, 25)
GUISetState(@SW_SHOW)

If TCPStartup() <> 1 Then Exit

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btnConnect
            $sockMain = TCPConnect(GUICtrlRead($txtIp), GUICtrlRead($txtPort))
        Case $btnSend
            TCPSend($sockMain, GUICtrlRead($txtSend))
            GUICtrlSetData($txtSend, "")
    EndSwitch
WEnd
Link to comment
Share on other sites

Any hint for us about the symptoms you observe?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • 5 weeks later...

None? :idea:

I found some bugs and modified the server part.

Compare old & new to see the difference.

Server

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$frmMain = GUICreate("Server - Not Connected", 375, 304, 192, 124)
$txtPort = GUICtrlCreateInput("", 35, 7, 46, 21)
$Label1 = GUICtrlCreateLabel("Port:", 5, 10, 26, 17)
$btnListen = GUICtrlCreateButton("&Listen", 215, 5, 75, 25)
$edtMain = GUICtrlCreateEdit("", 5, 35, 365, 264)
$btnExit = GUICtrlCreateButton("&Exit", 295, 5, 75, 25)

$ip = @IPAddress1 ;use the public ip
;$ip = "127.0.0.1" ;use local ip

GUISetState(@SW_SHOW)
_debugConsoleWrite("Server listening on IP: " & $ip & @CRLF)

Local $sockMain = -1, $sockClient = -1

If TCPStartup() <> 1 Then Exit

While 1
    If $sockMain = -1 Then
        $sockMain = TCPListen($ip, GUICtrlRead($txtPort))
        _debugConsoleWrite("Server sockMain: " & $sockMain & @CRLF)
   Else
        If $sockClient = -1 Then
            $sockClient = TCPAccept($sockMain)
            _debugConsoleWrite("Server sockMain: " & $sockMain & " sockClient: " & $sockClient & @CRLF)
        Else
            $szData = TCPRecv($sockClient, 255)
            if $szData <> "" Then
                GUICtrlSetData($edtMain,  @HOUR & ":" & @MIN & ":" & @SEC & " >> " & $szData & @CRLF & GUICtrlRead($edtMain))
                _debugConsoleWrite("Server sockMain: " & $sockMain & " sockClient: " & $sockClient & " szData: '" & $szData & "'" & @CRLF)
            EndIf
        EndIf
    EndIf
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            If $sockMain <> -1 Then TCPCloseSocket($sockMain)
            TCPShutdown()
            Exit
        Case $btnListen
            If GUICtrlRead($txtPort) = "" Then
                MsgBox(0, "Server", "You must enter a port to listen on!")
            Else
                $sockMain = TCPListen($ip, GUICtrlRead($txtPort))
                If $sockMain = -1 Then
                    MsgBox(0, "Server", "Failed to set listening state!")
                Else
                    WinSetTitle($frmMain, "", "Server - Listening on " & $ip)
                EndIf
            EndIf
        EndSwitch
    WEnd

Func _debugConsoleWrite($txt)
    If @Compiled = 0 Then
        ConsoleWrite(@HOUR & ":" & @MIN & ":" & @SEC & " >> " & $txt)
    EndIf
EndFunc
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...