Client - client communication without server?
Asked Answered
F

4

6

I am new to socket programming and I need to clarify some things.

Do you need a server between two client communication? Let me explain what I mean:

Example 1:

  1. Client1: Server, I want to talk with a client2
  2. Server: No problem. Let's just wait until he sends the request to connect
  3. Client2: I'm here. I want to talk with client1.
  4. Server: Okay Client1 here is Client2 IP address. And for you Client2, here is

Client1 IP Address. You can now talk to each other without me.

Example 2:

  1. Client1: Server, please send client2 a message: "Hey client2. How are you?"
  2. Server: Okay no problem. Sending message to client2
  3. Client2: Server thanks for sending client1's message. Send him a reply: "Hey, I'm fine."
  4. Server: Sending message to client1..

So my question is: Do you need a server after you met two clients together to communicate between them? Or I'm on completely wrong track?

EDIT:

The purpose behind this is that I want to expand my very simple mobile game to become a multiplayer. Just for example, I want to show PACMAN2 on PACMAN1 mobile phone and vice versa.

Factional answered 17/9, 2015 at 13:2 Comment(8)
It really depends on what you are trying to achieve. In a lot of cases, you need a server to send data from a to b.Superstructure
Also two clients can only connect if one of them listens to a connection, but that makes it a server lolSuperstructure
I want to expand my game to become multiplayer. It's a very simple game. I just need to show client2 in client1 mobile phone and vice versa.Factional
I believe in good ole mirc program (typical client server application) there was possibility to establish direct connection between two clients. so server was doing more for address book. So yes you could do it.Chenille
I think the main issue will be if there is a rooter in front of one of the clients. Please take a look at #575948Virago
You should really keep a server. You can track the data and check if there is anything suspicious.Superstructure
In practice, because of issues like dynamic IP and firewalls, it is much easier for the conversation with client1 and client2 to be relayed through the serverRiffle
Peer2Peer with mobile devices is quite different from P2P with stationary PCs. I'd consider using at least one "Control-Server" as nearly unavoidable. Except you want to accept some restrictions like players have to be on same net segment etc.Knothole
C
2

If you are using a TCP socket programming, you need central server to facilitate communication between clients.

Reason - You cannot connect to a port on one client from every other client. All clients can connect to one server on a particular port and server can facilitate the communication among clients.

If you move away from socket programming and use advanced features like Messaging; peer to peer communication and broadcasting of messages to multiple clients can be achieved.

EDIT:

Still I prefer TCP over UDP for these reasons especially Reliability

In your case of multi player games, still your clients need to be connected to server on dedicated socket. Since you have to use TCP anyway, server can take care of sending messages between clients with client id.

Canaigre answered 17/9, 2015 at 13:12 Comment(2)
"You cannot connect to a port on one client from every other client" ... in UDP you do not have a "connection" ... I guess you were just thinking in TCP ...Knothole
Yes. I am talking with respect to TCP sockets for multi player programming.Canaigre
C
2

yes, you can do with peer to peer communication does not need any central server or you can also use sockect or you can communicate with user ip address.

Ref peer to peer

Corycorybant answered 16/6, 2020 at 14:35 Comment(0)
C
0

Having the two client applications could theoretically communicate directly and this could work in a LAN but in practice it is unlikely. The main reason this won't work is that in many cases the IP address of client 1/client 2 that the server "sees" is actually the IP address of the network gateway for client 1/client 2 which means that client 1 cannot initiate a connection to client 2. Also you can have the firewall on client 2 machine (or its network) blocking the connection initiated from client 1.

You might find useful information if you read more on XMPP.

Cordage answered 17/9, 2015 at 13:20 Comment(0)
E
0

To put what Kevin Kal said into a answer: no you do not necessarily need a server for Client1 and Client2 to talk to each other. If you use the server in your example to send the necessary data (IP and port) to Client1, Client1 can connect to Client2 via a socket Client2 listens to (and as Kevin said, this makes Client2 into a server, strictly speaking.)

If you want to know more about Client to Client connections in java here's a really good answer to a similar question:

Connect two client sockets

Eure answered 17/9, 2015 at 13:22 Comment(1)
In above example, multiple clients have been launched in same program. In current problem, the client will be launched from multiple devicesCanaigre

© 2022 - 2024 — McMap. All rights reserved.