Jump to content

Recommended Posts

Posted

Greetings everyone.

I'm new in scripting AutoIt, and therefore you'll have to excuse my ignorance. I'm trying to learn this language, so please be gentle.

I'm also a user of Multimedia Builder, which I've been using for over some years now to script various programs.

Now, to the point - I'm in the development of a Multiplayer Game (2 players only) in MMB. For this I'll of course need communications between to MMB application, via local area network. I could do this in a stupid way without 3rd party programs; however I've decided to make TCP functions for this, which unfortunately is not supported by MMB.

So I've been studying some TCP scripting by AutoIt Smith (thank you very much). The core purpose of my AutoIt Script is to open a connection between the Host of the game, and the Joining Player (or to say; Server and Client). The Server program will constantly update/send contents of "host.txt" (containing game variables) to the Client, and constantly receive "joined.txt" from the Client. The Client should contain the same function, just vice versa.

However I've bumped into a problem; When the Host/Server sends the "host.txt" (from the host computer) info to the Client, the client must then overwrite this data, into the "host.txt" (on the client computer). In my case, each time the script loops, the contents are not overwritten to the target file, but added. So after a few loops, the destination file gets huge.

I don't know what causes this. Please remember I'm a novice in Autoit :P

These are my temporarily, unfinished scripts (again, so far based upon the script of AutoIt Smith), but I hope you can get the idea. So far the Server only sends, and the Client only receives.

Host/Server:

Global $MainSocket = -1
Global $ConnectedSocket = -1
Local $MaxConnection = 1; Maximum Amount Of Concurrent Connections
Local $MaxLength = 2048; Maximum Length Of String
Local $Port = 1000; Port Number
Local $Server = @IPAddress1; Server IpAddress
TCPStartup()
$MainSocket = TCPListen($Server, $Port)
If $MainSocket = -1 Then Exit MsgBox(16, "Error", "Unable to intialize socket.")
While 1
    If FileExists("host.txt") Then      ; Send function
        $File = FileOpen("host.txt", 0)
        $SendData = FileRead($File)
        FileClose ($File)
        TCPSend($ConnectedSocket, $SendData)
        Local $SendData = ""
    EndIf
    $Data = TCPRecv($ConnectedSocket, $MaxLength)
    If $Data = "~bye" Then
        MsgBox(16, "Server", "Connection Terminated.")
        Exit
    ElseIf $Data <> "" Then
; Unconditional Receive
        MsgBox(0, "Server", $Data)
    EndIf
    If $ConnectedSocket = -1 Then
        $ConnectedSocket = TCPAccept($MainSocket)
        If $ConnectedSocket <> -1 Then
; Someone Connected
            TCPSend($ConnectedSocket, "Connected!")
        EndIf
    EndIf
WEnd
Func OnAutoItExit()
    If $ConnectedSocket <> - 1 Then
        TCPSend($ConnectedSocket, "~bye")
        TCPCloseSocket($ConnectedSocket)
    EndIf
    If $MainSocket <> -1 Then TCPCloseSocket($MainSocket)
    TCPShutdown()
EndFunc;==>OnAutoItExit

Joined Player/Client:

Global $MainSocket
Local $MaxLength = 2048
Local $Port = 1000
Local $Server = InputBox("Enter Server IP Adress", "Server IP:"); Server IpAddress
TCPStartup()
$MainSocket = TCPConnect($Server, $Port)
If $MainSocket = -1 Then Exit MsgBox(16, "Client", "Unable to connect.")
While 1
    Local $Data= "" 
    $Data = TCPRecv($MainSocket, $MaxLength)
    If $Data = "~bye" Then
        MsgBox(16, "Client", "Connection Terminated.")
        Exit
    ElseIf $Data <> "" Then
; Unconditional Receive
        MsgBox(0, "Client", $Data)      ;Recieve Function
        $File = FileOpen("host.txt", 2)
        FileWrite("host.txt", $Data)
        FileClose ($File)
    EndIf
WEnd
Func OnAutoItExit()
    If $MainSocket <> - 1 Then
        TCPSend($MainSocket, "~bye")
        TCPCloseSocket($MainSocket)
    EndIf
    TCPShutdown()
EndFunc;==>OnAutoItExit

I hope all of this makes sense, and that someone can help me :nuke:

- Thanks in advance.

Posted

This does not solve my problem unfortunately.

I'm wondering if this has anything to do with the $MaxLenght which I've in this case set to 2048. Last run i noticed that the destination file would never go above 2048 bytes. Also, when the Client views the message box "Connected", connected is also copied to that file?

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...