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.

rootpanthera
  • 2,731
  • 10
  • 33
  • 65
  • 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. – kevintjuh93 Sep 17 '15 at 13:09
  • Also two clients can only connect if one of them listens to a connection, but that makes it a server lol – kevintjuh93 Sep 17 '15 at 13:10
  • 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. – rootpanthera Sep 17 '15 at 13:11
  • 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. – user902383 Sep 17 '15 at 13:11
  • I think the main issue will be if there is a rooter in front of one of the clients. Please take a look at http://stackoverflow.com/questions/575448/how-does-peer2peer-work-through-a-router – Benoît Sep 17 '15 at 13:11
  • You should really keep a server. You can track the data and check if there is anything suspicious. – kevintjuh93 Sep 17 '15 at 13:12
  • 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 server – ControlAltDel Sep 17 '15 at 13:15
  • 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. – Fildor Sep 17 '15 at 14:56

4 Answers4

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.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • "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 ... – Fildor Sep 17 '15 at 13:38
  • Yes. I am talking with respect to TCP sockets for multi player programming. – Ravindra babu Sep 17 '15 at 13:48
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

Balaji
  • 9,657
  • 5
  • 47
  • 47
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.

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

Community
  • 1
  • 1
Petter
  • 1,327
  • 11
  • 12
  • In above example, multiple clients have been launched in same program. In current problem, the client will be launched from multiple devices – Ravindra babu Sep 18 '15 at 05:30