Jump to content

TCP server sending info back to client


 Share

Recommended Posts

Is this possible?

I've got a server that recieves information from many clients. However i don't know how to send some info back to clients if they request it. Because for them to connect to the server - they must know the ip but the server can't know the IPs of all the clients...

thanks

Link to comment
Share on other sites

I've had a go at this, the server accepts the clients message but when it sends one back through the open socket - the client doesn't recieve it

Server

TCPStartup()

$TCPListen = TCPListen ("IP", port, 100)

While 1

;MsgBox (0, "LISTEN", $TCPListen)

Do

$TCPAccept = TCPAccept($TCPListen)

;MsgBox (0, "Accept", $TCPAccept)

Until $TCPAccept <> -1

Do

$TCPRec = TCPRecv ($TCPAccept, 1000000)

Until $TCPRec <> ""

MsgBox (0, "test", $TCPRec)

If $TCPRec = "test" Then

Sleep (3000)

TCPSend ($TCPListen, "CLIENT HAS RECIEVED THE MESSAGE")

MsgBox (0, "test", @error)

Endif

WEnd

CLIENT

$myip = "IP"

TCPStartup ()

$TCPConnect = TCPConnect ($myip, port)

If $TCPConnect = -1 Then Exit

TCPSend($TCPConnect, "test")

While 1

MsgBox (0, "Connection", $TCPConnect)

Do

$TCPAccept = TCPAccept($TCPConnect)

;MsgBox (0, "Accept", $TCPAccept)

Until $TCPAccept <> -1

Do

$TCPRec = TCPRecv ($TCPAccept, 1000000)

Until $TCPRec <> ""

If not $TCPRec <> "" Then MsgBox (0, "test", $TCPRec)

WEnd

Edited by tsolrm
Link to comment
Share on other sites

see if this post can help you

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

soory i totaly forgot about this topic

now to b more specific

client side

TCPStartup ()
$TCPConnect = TCPConnect ("127.0.0.1", 8080)
If $TCPConnect = -1 Then Exit
TCPSend($TCPConnect, "Hi server how are you")
While 1
    $TCPRec = TCPRecv ($TCPConnect, 1000000)
    If $TCPRec <> "" Then
        MsgBox (0, "Server sended me response", $TCPRec,9);9 seconds close, server have sleep 10 seconds before msgbox so its important to get that msg so that client can exit
        If $TCPRec = 'bye' Then Exit
        TCPSend($TCPConnect, "Yes i'm enjoying if ty for all the help")
    EndIf
WEnd

as you see client have 9 seconds displaying msgbox so that we can enshure that its ready when the next message arrive, i suggest using something that dont pause script so that you can display results, something like gui commands.

You dont need TCPAccept in client, because its already connected to server so it will recive and send msgs based on TCPConnect server, TCPAccept is only server side to add one or more new connections and it dont have place in client script.

and now the server, so i set this to resamble your trying to comunicate with server

TCPStartup()
$TCPListen = TCPListen ("127.0.0.1", 8080, 100)
Do
    $TCPAccept = TCPAccept($TCPListen)
Until $TCPAccept <> -1
TrayTip("Someone need to talk to me, i wonder who he is?!!!", "it must be my old friend with socket ID "&$TCPAccept, 5, 1)
Do
    $TCPRec = TCPRecv ($TCPAccept, 1000000)
Until $TCPRec <> ""
MsgBox (0, "Client connected and tolded me", $TCPRec)
TCPSend ($TCPAccept, "Hi client, i'm fine today, hope your enjoying yourself learning on autoitscript forum")
;~ Sleep (6000)
Do
    $TCPRec = TCPRecv ($TCPAccept, 1000000)
Until $TCPRec <> ""
MsgBox (0, "Connected client tolded me", $TCPRec)
TCPSend ($TCPAccept, "Glad to be of service, please feel free to ask if you have other questions, i'm sure that someone will jump into your help if he is familiar with codes that your using")
Sleep (10000)
TCPSend ($TCPAccept, "bye")

as you can see server wait to someone connect to him and record his ID and after it its looking for msges from that connection (or connections) or can send msg before someone answer him, so basicly server sends and recive data based on ID that connected (so client connect to servers IP and sends and recive data from returned socket identifier)

Adding array to $TCPAccept = TCPAccept($TCPListen)

On server side that array will need loop while server is trying to get if there is any msg, that will enshure that you can record more than one socket identifier and put it into array.

after that server will need to loop on all socket identifier array to recive msg from clients and correspond with them

thats why i posted you link on previous post

clients send data to server and server log them into GUI. after that server sends some data to client and client (or clients) display them as tooltip

so im using on server TCPAccept($TCPListen) and record connecting ID to array and then i loop array to see did any ID from array sended me msg if it did i send msg to that array element that holds senders connecting ID.

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

  • 8 years 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...