Jump to content

TCP Help


Recommended Posts

Hi all people.

I need your expertise. I am setting up 2 or 3 servers on which clients maybe 50 will connect on any available server by TCP means. Is this possible? I appreciate if someone can post a simple client/server with this setup. I have a script came from this forums but as i understood client connects only to a specific server IP address. e.g. TCPConnect(@IPAddress1, port)

Thank you!

I forgot how to include the script in this forum. Can somebody remind how to post the script. I remember it is in /code.

Link to comment
Share on other sites

can somebody help me? i have the server code which accepts multiple connection taken from this forum. i don't know how will i write the client on which will look for any available servers. if one server is down then it will look for another server.

here is the server code.

HotKeySet("{ESC}","Quit"); Press ESC to quit

Global $CURRENT_SOCKET = 0

Global $CONNECTED_SOCKET[16]

Global $TCP_SERVER

$TCP = TCPStartup()

If $TCP = 0 Then

MsgBox(16, "Error", "Unable to startup TCP Services!")

Exit

EndIf

$TCP_SERVER = TCPListen(@IPAddress1,1777,16)

If $TCP_SERVER = -1 Then

MsgBox(16, "ERROR", "Unable to start listening on port 1777")

Exit

EndIf

While 1

$CONNECTED_SOCKET[$CURRENT_SOCKET] = TCPAccept($TCP_SERVER)

If $CONNECTED_SOCKET[$CURRENT_SOCKET] <> -1 Then

$CURRENT_SOCKET += 1

EndIf

For $INDEX = 0 To 15

$RECV = TCPRecv($CONNECTED_SOCKET[$INDEX],512)

If $RECV <> "" Then

MsgBox(0,"Message Received " & $INDEX, $RECV, 5)

EndIf

Next

Sleep(20)

WEnd

Func Quit()

TCPCloseSocket($TCP_SERVER)

TCPShutdown()

Exit

EndFunc

Link to comment
Share on other sites

can somebody help me? i have the server code which accepts multiple connection taken from this forum. i don't know how will i write the client on which will look for any available servers. if one server is down then it will look for another server.

here is the server code.

HotKeySet("{ESC}","Quit"); Press ESC to quit

Global $CURRENT_SOCKET = 0

Global $CONNECTED_SOCKET[16]

Global $TCP_SERVER

$TCP = TCPStartup()

If $TCP = 0 Then

MsgBox(16, "Error", "Unable to startup TCP Services!")

Exit

EndIf

$TCP_SERVER = TCPListen(@IPAddress1,1777,16)

If $TCP_SERVER = -1 Then

MsgBox(16, "ERROR", "Unable to start listening on port 1777")

Exit

EndIf

While 1

$CONNECTED_SOCKET[$CURRENT_SOCKET] = TCPAccept($TCP_SERVER)

If $CONNECTED_SOCKET[$CURRENT_SOCKET] <> -1 Then

$CURRENT_SOCKET += 1

EndIf

For $INDEX = 0 To 15

$RECV = TCPRecv($CONNECTED_SOCKET[$INDEX],512)

If $RECV <> "" Then

MsgBox(0,"Message Received " & $INDEX, $RECV, 5)

EndIf

Next

Sleep(20)

WEnd

Func Quit()

TCPCloseSocket($TCP_SERVER)

TCPShutdown()

Exit

EndFunc

explane your problem bether to me pleas

ah and use

YOUR SOURCE CODE OF YOUR PROGRAM
pleas:)
Link to comment
Share on other sites

explane your problem bether to me pleas

ah and use

YOUR SOURCE CODE OF YOUR PROGRAM
pleas:)
yucatan.

ok. thanks you replied. i am setting up a TCP communication wherein computers maybe 50 computers will connect to a 2 or 3 TCP servers.

Client computer will check and connect to which TCP server is available or online and then it will send the message to the connected TCP server. if it happens that there is no available server, client will still look for available server until there is avaialable. i don't know how will i write my client code to communicate with the TCP servers.

here is my client code. but i don't know how to post it correctly. please guide me how to post it.

thank you

#include <GUIConstants.au3>

#include <GUIConstantsEx.au3>

#include <EditConstants.au3>

AutoItSetOption("TrayIconHide", 1)

HotKeySet("{ESC}","Quit"); Press ESC to quit

HotKeySet("{PAUSE}","SendCom")

$PORT = 1777

$IP = @IPAddress1

$TCP = TCPStartup()

If $TCP = 0 Then

MsgBox(0, "Error", "Unable to startup TCP Services!")

Exit

EndIf

$TCP_CLIENT = TCPConnect($IP,$PORT)

If $TCP_CLIENT = -1 Then

MsgBox(0, "ERROR", "Unable to connect "& $IP & " on port 1777")

Exit

EndIf

While 1

Sleep(50)

WEnd

Func SendCom()

TCPSend($TCP_CLIENT, "message")

EndFunc

Func Quit()

TCPCloseSocket($TCP_CLIENT)

TCPShutdown()

Exit

EndFunc

Link to comment
Share on other sites

when you reply you will notice the options for bold italic icons etc well to the far right there is an option called "wrap in code tags"

For simplicity paste your code in to the reply.

Highlight it all then click the icon.

You will see [ code ] appear at the start and [ / code ] at the end without spaces.

Click preview post to see if its worked.

Job Done......

We live as we dream alone!

Link to comment
Share on other sites

when you reply you will notice the options for bold italic icons etc well to the far right there is an option called "wrap in code tags"

For simplicity paste your code in to the reply.

Highlight it all then click the icon.

You will see [ code ] appear at the start and [ / code ] at the end without spaces.

Click preview post to see if its worked.

Job Done......

Thanks Datus for guiding me in posting the code. it worked. actually i'm new to AutoIt and still learning from other peoples expertise in this forum.

So here we go. Is my question possible?

Forgive me if my grammar is incorrect. What i wanted to happen is that i want my client to connect to any server which is available. if one TCP server is down, then my client will then search for next available server. When it finds the next available TCP server, it will notify by any means like TrayTip or message that a client has connected on the server. below is my script taken from Andreik. Client successfully connects but i don't know how to tell the script on my client side to do searching for the available TCP servers.

If my explanation for my problem is very confusing let me know so i can write it more in details...

Thank you. I appreciate if somebody can help me here....

Server

HotKeySet("{ESC}","Quit"); Press ESC to quit

Global $CURRENT_SOCKET = 0
Global $CONNECTED_SOCKET[16]
Global $TCP_SERVER

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

$TCP_SERVER = TCPListen(@IPAddress1,1777,16)
If $TCP_SERVER = -1 Then
MsgBox(16, "ERROR", "Unable to start listening on port 1777")
Exit
EndIf

While 1
$CONNECTED_SOCKET[$CURRENT_SOCKET] = TCPAccept($TCP_SERVER)
If $CONNECTED_SOCKET[$CURRENT_SOCKET] <> -1 Then
$CURRENT_SOCKET += 1
EndIf
For $INDEX = 0 To 15
$RECV = TCPRecv($CONNECTED_SOCKET[$INDEX],512)
If $RECV <> "" Then 
MsgBox(0,"Message Received " & $INDEX, $RECV, 5) 
EndIf
Next
Sleep(20)
WEnd

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

Client

#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>

AutoItSetOption("TrayIconHide", 1)
HotKeySet("{ESC}","Quit"); Press ESC to quit
HotKeySet("{PAUSE}","SendCom") 

$PORT = 1777
$IP = @IPAddress1

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

$TCP_CLIENT = TCPConnect($IP,$PORT)
If $TCP_CLIENT = -1 Then
MsgBox(0, "ERROR", "Unable to connect "& $IP & " on port 1777")
Exit
EndIf

While 1
Sleep(50)
WEnd

Func SendCom()
TCPSend($TCP_CLIENT, "message") 
EndFunc

Func Quit() 
TCPCloseSocket($TCP_CLIENT)
TCPShutdown()
Exit
EndFunc
Link to comment
Share on other sites

:) nobody cares to help me? i just want how client will connect to a two TCP servers. if either one is not available then it will look for another TCP server available.

hope someone will take a look on it.

yes, we all do want to help you.

Connect to 2 concurrent servers or try a list until successful ?

either case is easy.

Then what ? what do you wanna do with all these ? be explicit.

Are you gonna send plain text over the net ?!!!

make sure you know what you are doing and possible consequences. 150 users down because of lack of experience can be costly.

I'd , if you don't mind my advice, play around with "non mission critical" environment, evaluate what a malignant user could exploit, etc., until you become experienced .

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

yes, we all do want to help you.

Connect to 2 concurrent servers or try a list until successful ?

either case is easy.

Then what ? what do you wanna do with all these ? be explicit.

Are you gonna send plain text over the net ?!!!

make sure you know what you are doing and possible consequences. 150 users down because of lack of experience can be costly.

I'd , if you don't mind my advice, play around with "non mission critical" environment, evaluate what a malignant user could exploit, etc., until you become experienced .

I think argumentum is correctly getting to the much more fundamental issue here, what is this server that you want to make supposed to do?

In the first place, as argumentum pointed out, at this point in your experience with TCP Servers/Clients you really shouldn't be trying to write up any sort of server/client setup where traffic will leave your intranet... Way too many security concerns with that.

In the second place, what function do you intend for this setup to perform? There's plenty of examples of chat servers, of file servers, even of remote-control servers on the forums - and someone will probably be nice enough to point you in the direction of one - but you have to be more explicit with what you want.

Edited by exodius
Link to comment
Share on other sites

yes, we all do want to help you.

Connect to 2 concurrent servers or try a list until successful ?

either case is easy.

Then what ? what do you wanna do with all these ? be explicit.

Are you gonna send plain text over the net ?!!!

make sure you know what you are doing and possible consequences. 150 users down because of lack of experience can be costly.

I'd , if you don't mind my advice, play around with "non mission critical" environment, evaluate what a malignant user could exploit, etc., until you become experienced .

thanks argumentum for mentioning those possibilities. actually client computers will connect to TCP server as per request. As my client code, it has a hotkey when user press that button, client will notify the server by sending only a text, a messagebox to server or a traytip baloon message to server. i have tested the script to two computers and they were successfully connected to the server and a traytip was succesfully appears on the server giving an info like IP address of client connected. but what i want now to happen, client computers will connect to two or 3 TCP servers. Forgive me if i am mentioning the problem again.

there is no big file or picture will be sent. its just that i want client will send information like IP address of the client that is connected to the server. After information has been sent, user then on the server computer, will connect to it remotely remotely.

Link to comment
Share on other sites

I think argumentum is correctly getting to the much more fundamental issue here, what is this server that you want to make supposed to do?

In the first place, as argumentum pointed out, at this point in your experience with TCP Servers/Clients you really shouldn't be trying to write up any sort of server/client setup where traffic will leave your intranet... Way too many security concerns with that.

In the second place, what function do you intend for this setup to perform? There's plenty of examples of chat servers, of file servers, even of remote-control servers on the forums - and someone will probably be nice enough to point you in the direction of one - but you have to be more explicit with what you want.

thanks exodius and for all you guys giving me points to be consider doing this. i can say that the servers will function only to notify on which client is connected with them. do you think i am making a big traffic on the net for doing this things? as i mentioned to argumentum, clients will not connect simultaneously but only per request as when user on the client computer will press the hotkey.

please point me out for a more sample scripts that i can learn from doing this things.

i appreciate all your response.

Link to comment
Share on other sites

SERVER

TCPStartup()
HotKeySet("{ESC}","Quit");Change ESC with other keys for 2nd and 3rd server

$IP = @IPAddress1
$PORT = 1255 ;Change 1255 with 1256 for 2nd server and 1257 for 3rd server

$SERVER = TCPListen($IP,$PORT)
If @error Then
    MsgBox(0,"ERROR","Error to listen on port " & $PORT)
    Exit
EndIf

Do
    $SOCKET = TCPAccept($SERVER)
    Sleep(25)
Until $SOCKET <> -1
TCPSend($SOCKET,"Connected")

While 1
    $RECV = TCPRecv($SOCKET,512)
    If $RECV <> "" Then
        MsgBox(0,"Message",$RECV)
    EndIf
    Sleep(20)
WEnd

Func Quit()
    TCPShutdown()
    Exit
EndFunc

CLIENT

Global $SERVER[3][2] = [[@IPAddress1,1255],[@IPAddress1,1256],[@IPAddress1,1257]]
Global $CLIENT
Global $CURRENT_SERVER[2] = [$SERVER[0][0],$SERVER[0][1]]

TCPStartup()
$CLIENT = TCPConnect($CURRENT_SERVER[0],$CURRENT_SERVER[1])
If @error Then
    MsgBox(0,"ERROR","Error to connect to " & $SERVER[0][0] & " on port " & $SERVER[0][1])
EndIf

While 1
    $RECV = TCPRecv($CLIENT,512)
    If @error Then
        MsgBox(0,"ERROR","Disconnected to server")
        Call("NextServer")
    EndIf
    If $RECV <> "" Then
        MsgBox(0,"Message",$RECV)
    EndIf
    Sleep(20)
WEnd

Func NextServer()
    For $INDEX = 0 To 2
        If $CURRENT_SERVER[0] = $SERVER[$INDEX][0] And $CURRENT_SERVER[1] = $SERVER[$INDEX][1] Then
            If $INDEX = 2 Then $INDEX = -1
            $CURRENT_SERVER[0] = $SERVER[$INDEX+1][0]
            $CURRENT_SERVER[1] = $SERVER[$INDEX+1][1]
            MsgBox(0,"Server was changed","New address is " & $CURRENT_SERVER[0] & ":" & $CURRENT_SERVER[1])
            $CLIENT = TCPConnect($CURRENT_SERVER[0],$CURRENT_SERVER[1])
            ExitLoop
        EndIf
    Next
EndFunc

When the words fail... music speaks.

Link to comment
Share on other sites

Hello Andreik,

Thanks for the script. I tested it and it worked great. :) Client successfully connected to the server and get notified by the server. :) Thanks a lot.

But i have tried making an array as what you suggested.

$SERVERS[3] = ["192.168.0.1", 192.168.0.2" ,192.168.0.3"]

I used your server code and have tried to make client script with the array of the IP of the 3 servers. Client could not connect. I don't have much knowledge working with arrays and still trying to learn from your example.

Would you mind to take a look on the script.

Here is the CLIENT script i made so far. Unfortunately, i cannot connect to any of the 3 servers.

Thank you for understanding.

CLIENT

HotKeySet("{PAUSE}","SendCom")

Global $SERVER[3] = ["192.168.0.1","192.168.0.2","192.168.0.3"]
Global $PORT = 1255

TCPStartup()
$CLIENT = TCPConnect($SERVER[1], $PORT)
If @error Then  
    MsgBox(0,"ERROR","Error to connect to server")
Else
    MsgBox(0,"Connected","Connected to server" & $SERVER[1])
EndIf

While 1
    $RECV = TCPRecv($CLIENT,512)
    If @error Then
        MsgBox(0,"ERROR","Disconnected to server")
    Else        
        $CLIENT = TCPConnect($SERVER[2], $PORT)
        TCPSend($CLIENT, "Connected to" & $SERVER[2])
            If @error Then              
                MsgBox(0,"ERROR","Error to connect to server")
            Else 
                $CLIENT = TCPConnect($SERVER[3], $PORT)
                TCPSend($CLIENT, "Connected to" & $SERVER[3])
            EndIf
    EndIf
    If $RECV <> "" Then
        MsgBox(0,"Message",$RECV)
    EndIf
    Sleep(20)
WEnd

Func SendCom()
    TCPSend($CLIENT, "Message") 
EndFunc
Link to comment
Share on other sites

Try something like:

HotKeySet("{PAUSE}","SendCom")

Dim $SERVER[3] = ["192.168.0.1","192.168.0.2","192.168.0.3"]
Dim $PORT = 1255
Dim $CLIENT, $RECV

TCPStartup()
For $i = 0 to UBound($SERVER)-1 
    $CLIENT = TCPConnect($SERVER[1], $PORT)
    If Not @error Then    
        MsgBox(0,"Connected","Connected to server" & $SERVER[$i])
        ExitLoop
    EndIf
Next

While 1
    $RECV = TCPRecv($CLIENT,512)
    If @error Then
        MsgBox(0,"ERROR","Disconnected to server")
    EndIf
    
    If $RECV <> "" Then
        MsgBox(0,"Message",$RECV)
    EndIf
    Sleep(20)
WEnd

Func SendCom()
    TCPSend($CLIENT, "Message")    
EndFunc

It doesn't dynamically reconnect because I do not know how your application works and if you can switch between servers at any point.

Link to comment
Share on other sites

thanks manadar for the reply. i'll give your script a try and start to learn from it. i'm really totally new to this and your help and advise from you guys motivates me to go deeper. sorry for bad english.

actually, i am creating array because i have 3 mostly online servers. from what i read on this forum, moslty clients connect to only 1 server. in my case, clients will simultaneously connect to these 3 servers. when client connects, if there is no available server, it will still look for another avaialable server and when it does, it notifies by sending message or traytip on the server to which is connected. i want also the client, when connected will notify 2 other server that the client was connected. in my mind i can see its really possible but i don't know how will i put it in reality. that's why asking from your help gives me more confidence to start doing this.

i'll just give it a try and see to it how will i put some TCPsend command between each successful connections.

thank you. sorry if my explanation is confusing.

Link to comment
Share on other sites

hello Manadar,

i tried running your client script but it still seems not connecting. it always gives me this message in the script

MsgBox(0,"ERROR","Disconnected to server")

even if the server is online, there should be a message box that it is connected but there is none.

MsgBox(0,"Connected","Connected to server" & $SERVER[$i])
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...