Jump to content

Remake of Server/Client Example, need help.


Recommended Posts

Hey. =3

I have used AutoIt for some while now, and suddenly i felt like i wanted to do some chatsystem application...

Since i never had worked with networking before, i took a look in the Helpfile, where i found two exemples for TCPSend() and TCPRecv().

The examples were like Server/Client programs, and to study them, i opened them up, tested them and started to add some own things...

Now, I have created a client that when requested sends name and message to the server, and the servers duty is to send this over back to the client and also to all the other clients connected.

But ive run into a problem...

Im trying to FOR-Loop the incoming messages so that it sends it out to all the other clients connected, but im failing. :)

The servercode that is, like i said, from the beginning an example: (Note that its very messy written, and specially with all those wierd for-loops ive made to desperatly make it work...)

CODE

For $i = 0 To $nsock -1

$recv[$i] = TCPRecv($ConnectedSocket[$i], 2048)

If @error Then

logg("Error...")

MsgBox(0,"Error","Error in recieving data from Connected Socket.")

Exit

EndIf

If $namecheck = 0 Then

If $recv[$i] <> "" Then

;logg("Recieved namedata")

;logg("Sending namedata back to...")

For $t = 0 To $nsock -1

;MsgBox(0,"name",$recv[$t]) ;Debug stuff

TCPSend($ConnectedSocket[$t],$recv[$t])

logg("Client" & $t & ".")

Next

;logg("Namedata sent back to all clients.")

$namecheck = 1

Endif

Else

If $recv[$i] <> "" Then

;logg("Recieved messagedata.")

;logg("Sending back messagedata to...")

For $a = 0 to $nsock -1

MsgBox(0,"meddelande",$recv[$a])

TCPSend($ConnectedSocket[$a],$recv[$a])

logg("Klient " & $a & ".")

Next

;logg("Messagedata sent back to all clients.")

$namecheck = 0

EndIf

Endif

Next

Complete serverscript:

CODE

#include <GUIConstantsEx.au3>

Global $szIPADDRESS = @IPAddress1

Global $nPORT = *myport* ;My port, none but me must see it... :P

Global $MainSocket, $szIP_Accepted

Dim $ConnectedSocket[5]

Dim $recv[5]

Global $msg, $recv_name

global $nsock = 1

global $namecheck = 0

$GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)

$edit = GUICtrlCreateEdit("", 10, 10, 280, 180)

GUISetState()

logg("Booting server...")

TCPStartup()

$MainSocket = TCPListen($szIPADDRESS, $nPORT)

If $MainSocket = -1 Then

MsgBox(0,"Error","Error in MainSocket, TCPListen")

Exit

EndIf

$ConnectedSocket[0] = -1

logg("Server boot successful.")

Do

$ConnectedSocket[0] = TCPAccept($MainSocket)

Until $ConnectedSocket[0] <> -1

$ConnectedSocket[1] = -1

logg("First client has connected.")

While 1

$msg = GUIGetMsg()

If $msg = $GUI_EVENT_CLOSE Then ExitLoop

For $i = 0 To $nsock -1

$recv[$i] = TCPRecv($ConnectedSocket[$i], 2048)

If @error Then

logg("Error...")

MsgBox(0,"Error","Error in recieving data from Connected Socket.")

Exit

EndIf

If $namecheck = 0 Then

If $recv[$i] <> "" Then

;logg("Recieved namedata")

;logg("Sending namedata back to...")

For $t = 0 To $nsock -1

;MsgBox(0,"name",$recv[$t]) ;Debug stuff

TCPSend($ConnectedSocket[$t],$recv[$t])

logg("Client" & $t & ".")

Next

;logg("Namedata sent back to all clients.")

$namecheck = 1

Endif

Else

If $recv[$i] <> "" Then

;logg("Recieved messagedata.")

;logg("Sending back messagedata to...")

For $a = 0 to $nsock -1

MsgBox(0,"meddelande",$recv[$a])

TCPSend($ConnectedSocket[$a],$recv[$a])

logg("Client " & $a & ".")

Next

;logg("Messagedata sent back to all clients.")

$namecheck = 0

EndIf

Endif

Next

$ConnectedSocket[$nsock] = TCPAccept($MainSocket)

If $ConnectedSocket[$nsock] <> -1 Then

$nsock += 1

$ConnectedSocket[$nsock] = -1

logg("En klient har anslutit till servern.")

MsgBox(0,"",$nsock)

EndIf

WEnd

If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

TCPShutdown()

Func logg($entlog)

GUICtrlSetData($edit, GUICtrlRead($edit) & $entlog & @CRLF)

EndFunc

(I can provide clientscript if you really need it. D:)

Yep. Like i said, this is very messywritten, and so...

And the things with all those for-loops is just desperate tries to make things work.

Please help me, and if there were something you didnt understand due to my not that very good english, i'll try to explain better if you just tell. :3

(If it was unclear, my problem is that the servercode dont send out to all of the ConnectedSocket[]'s. =O)

Link to comment
Share on other sites

  • 2 weeks later...

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