TCP socket / LAN connection.

Hi all, I need some help
me and my classmate are trying to do a little "Game" to play in LAN.
I have searched on internet a lot, but I have find nothing helpful (or maybe I can't unsderstand) so I'm asking to you, any information that may be it.

we can see the game as a "chat" to do it easier, both users need to send and receive data.
but we need to write only one code that can be used from both Pcs, and not one host and one client.

I have thought to this: (I don't know if it is possible please help me)

____
GameA| (Game is the game app)
_____|
./|\
_|__
hostA| (Host is the server, i think it can be also in another code)


than it will be like this:

_____ . . . . _____
GameA| . . |GameB|
______| . .|______|
. |. . . .\ . . / . . |
. |. . . . .\ / . . . |
._|___. / . \ ___|__
hostA|. . . . |host B|

I don't know if it is possible but i have thought about it like:
client A sends to host B, client B sends to host A
the host save all on a txt file, and the client can read from it.

My question are:
1- Is this possible?
2- If not: Is it possible to do it with only one code?
3- If yes: can you explain me how? or do an example?
4- Am I a good designer?


thank you all.

P.S.
my problem is How to do the "host" code
Last edited on
yes, its possible.
you can just do sendto & receivefrom at each other. I would consider UDP if you don't NEED TCP. But each machine must have a listener (probably, a thread) running. This can be very light, really just 'try to read, non blocking, if anything was read, handle it, sleep a tiny bit, repeat forever'. The logic to trigger sends to each other will be where your main program logic is doing stuff.

This is a 2-pc (scale to N-pcs later) design. Its just machine 1 and machine 2 talking to each other and listening for each other's replies. Ill be honest I don't really get your diagram, but if 2 machines talking to each other is all you want …

and if you want a server in the mix, you can do that too. but start small. build one client and one server talking to each other, or 2 clients talking to each other depending on what makes sense.
Last edited on
Thank you, i get it.
yes, I thought about UDP but I'm not working alone...so I can't choose by myself.
ok my "diagram" is so ugly ahahaha you're right ahah.

ok, I have understood the logic that you're talking about (I was thinking aobout something similar). but I don't know how to write it, I don't know what libraries to use to.
All that i have found on internet, doesn't work...
I would consider UDP if you don't NEED TCP.

You do need TCP if it is important to know the addressee got the message. You may use UDP if it is of minor importance if a message goes lost or not.

To avoid a server-client "hierarchy" your game app must be able to be both, sender and receiver, to get equal takers playing together. You may find an example of same time server and client programs here: https://hp.giesselink.com/hpil.htm -- Alas, to make it work you have to config IP adresses and port numbers first. At least the port numbers could be negotiated automatically using UDP.
any examples not talking to itself (same computer via localhost ports) will need you to make minor changes like the ip address / name /etc of the machines before they will work.

I will restate it: read up on blocking vs non-blocking. The default is to block (or it was on the libraries I used way back when) which is most likely not what you want.
Last edited on
any examples not talking to itself (same computer via localhost ports) will need you to make minor changes like the ip address / name /etc of the machines before they will work.

Alas, yes. See https://de.wikipedia.org/wiki/Zeroconf or if you prefer https://en.wikipedia.org/wiki/Zero-configuration_networking
And: "any examples not talking to itself..." ... or other programs running on the same machine.
And: "localhost ports" -- are there ports reserved or defined for localhost? Nothing found in https://tools.ietf.org/html/rfc1122 about this. Ports are typically related to aplications -- https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers -- and yes, there are 5..6 related to localhost.
Besides the internal host loopback address ({127, <any>}) the communication may use also (any of) the host's IP address(es).
Topic archived. No new replies allowed.