Page 1 of 1
Socket question
Posted: Wed May 01, 2013 10:16 pm
by dunbarx
I am finally playing with sockets, to be able to send messages from one machine to another.
So I can send message from a stack on machine "A" to a stack on machine "B" by opening a socket on both, and writing from "A" to "B". Works fine.
But if I have another stack on machine "B" listening to the same socket, no dice. Only if I close the socket on the first stack and open it on the second can I then send a message to the second. Is it true that only the first stack to open a socket on a particular machine can listen to that socket?
May I assume that if I had two or more different machines, each listening to that socket, stacks on both would receive the message from "A"? In other words, many machines can receive a message on a single socket, but only one stack on any single machine can.
Just wondering. I can always manage such things within LC itself on the target machine, and I guess that is normal practice?
Thanks.
Craig Newman
Re: Socket question
Posted: Wed May 01, 2013 10:43 pm
by sturgis
Yes, you can only have 1 process on a machine listening to a socket.
If you have multiple machines listening to the same socket, no you can't send out a message and have them all pick it up (if you look at the chatrev stacks you'll see where it cycles through the open sockets and pops a message out to each machine on the list.. Packets are tagged using NIC mac addresses so a nic will only respond to packets that are intended for it. (if I understand all this correctly) The exception to this is if you have your network cards running in promiscuous mode at which point they can snag pretty much anything floating around the network.
If you want a machine to be able to talk to itself (IE connect multiple stacks to 1 listening server stack.. still need to only have 1 listening..) look at named sockets. I can't find where I saw it before but if I recall correctly you can name a socket like so:
open socket to "192.168.254.1:10000|uniqueidentifieryoumadeupjustnow" This way you can have multiple stacks that connect to the server stack using the same listening port and have data end up at the right stack since the server will use the unique identifying info to keep the ports seperate.
I'm not real solid on this stuff of course so please everyone, correct the mistakes i'm sure i've made!
Re: Socket question
Posted: Thu May 02, 2013 2:10 am
by dunbarx
Thanks Sturgis.
So the "basic" way to send a message to more than one machine is to have a different socket for each? That seems easy enough.
Are there limits to the way ports are numbered? Are there ports one must not touch? I know that, for example, 25 and 587 are reserved for SMTP. Are these even within the same protocol?
So much I don't know.
Craig
Re: Socket question
Posted: Thu May 02, 2013 2:25 am
by sturgis
10k and up is pretty safe. (not always, check the result when you start to listen and see if you get an error)
Then, for multiple clients, when they connect they hit the socket you designate and an additional socket just for that client is created after they talk to each other for a second. Then yes, like in the chatrev stack, the server can use that specific socket to talk to each machine. Each machine that hits the server will negotiate and get its own assigned socket.
I forget where the breakover point is for the "don't use these" socket numbers but as you mention don't use the obvious ones, and high numbers are safer. I also don't recall if there is a cap on socket numbers.
If you are using the chat sampler from the resources as your guide there are a couple issues with it. First is that if you're using both server and client on the same machine you run into the "can't have 2 sockets with the same name on 1 machine" issue. If you run just 1 client and 1 server thats no biggy, but if you want to run several clients at once you need to adjust the script to give unique names to each socket using the server:port|uniqueName method. Also there is a bug in the code, that I think was caused at some point by a change in how variable deletions occur.
Sec, gonna pop the stacks up here so i can find where to fix the issue again..
K. the location to fix is the start server button, look for the line like so:
delete lChatterArray[s] -- this is where a disconnecting client is removed from the array of connected systems for message broadcasting
Change it to:
delete local lChatterArray[s] -- proper form to prune the array tree
To adjust the client stack so that you can clone it on a single machine and have them all work, look in the stack script for chatConnect and adjust the open socket so that you can generate a unique id of some type. (I use the milliseconds to make it easy)
The change to use milliseconds as a name doesn't hurt the functioning of the stack if its on different machines, just allows it to work with multiple connects on a single machine.
Re: Socket question
Posted: Thu May 02, 2013 10:58 pm
by dunbarx
Sturgis.
Good stuff, thank you. On a simpler note, and I had a feeling this was so, the largest port I can make work is 65535. That is a lot of ports.
Craig