Connecting two separate sockets through serversocket - java

I'm developing a chat application in Java that enables client to connect to a predefined port and when two clients are connected the server should connect those two sockets and data should be exchanged between them.
I know to create a ServerSocket and Socket that will connect and establish a communication between them through a separate port and a server socket. But, how can I connect those two connected clients?

If you are creating a chat application then Chat server will not connect client socket in order to communicate.
You can try following approach:
On server side Use a HashTable to store connected clients.
When you receive a message from client, include a an id of CLient to whom message is to be sent and simply pass on the message to other client.
There will be separate threads running for these clients, so you need to have a policy to control the load on server.

This questions sounds like a homework assignment.
If you would like to see a great contemporary demo of a chat system you may learn from Play's WebSocket chat sample application.

Related

Java showing servers existance on client part (list)

I want to develop network application (servers-clients) with some process:
server creating instance on specific port and sending to other client in network information about existance (can be invitation message);
clients are checking about existing servers in LAN network on specific port. Choose which one to connect and do other actions.
I tried ServerSocket and Socket and I know how to connect one server to one client. But to do procedures described above I need to do something like broadcating. Which method should I use and try? Is MulticastServer good for that one?
Thanks for help.

Client - client communication without server?

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:
Client1: Server, I want to talk with a client2
Server: No problem. Let's just wait until he sends the request to connect
Client2: I'm here. I want to talk with client1.
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:
Client1: Server, please send client2 a message: "Hey client2. How are you?"
Server: Okay no problem. Sending message to client2
Client2: Server thanks for sending client1's message. Send him a reply: "Hey, I'm fine."
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.
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.
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
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.
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

how can I make a server connect to another server in java

How can I make a server connect to another server in java
My program is replicated file system. So , I need the client to connect to the server in order to read or write in the text file. which is easy to implement throw TCP connection . but the problem is how can i make the server connect other servers to avoid conflicting in the text file.
I have read about multicast throw UDP connection. can I make the server have to kind of connection one using TCP with client and another connection using TCP to multicast request to other servers in the group ?
Or if there are anther way to implement that?
Thank you
I would suggest you to look at group communication frameworks, such as jgroups.
Look here for explanation on TCP transport.

Multiple java socket connections

I have created this game bot where it connects to the game, and starts playing.
My problem is that i can't start more than one of these as the other then won't work.
Is is possible that if i run 2 instances of the same program the sockets are interfering with each other ? After all, they do connect to the same IP with the same port ?
And sometimes after i close(just closing cmd) the program is unable to connect again. Is that cause i didn't close the connections right ?.
I hope this is enough else i'll just have to post my source code
Best regards.
It's possible to connect to the same socket/port several times. Actually a socket is a double peer: {client ip/ client port}{server ip/server port}. When you connect to a server, your client port is assigned dynamically. You will have a new and different client port per client. So it should work unless the server side forbid it.
You should have a server that listens for multiple connections. A server is bound to a port and once that port is in use another application cannot use it. So for the server just have one instance. Multiple clients can connect to this IP/Port as long as the Server accepts multiple connections.
If a client connects to the server and the other clients stop working this may be because the server does not support multiple clients. To do this you need to use multi threading in the server. The server should accept a client socket and create a new instance of a client with it's own StreamReader/Writer objects.
http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html
if you are working with a specific TCP port, then there is a close-wait period that this port cannot be claimed temporariliy for some time. also multiple programs cannot listen the same TCP port. Use threads.

Java tcp socket server with j2me client socket

I want to create java server socket and j2me client socket which can communicate with each other.
Does anyone know how to do this?
Are you trying to share data between two MIDlets running on one mobile phone?
Are you trying to exchange data between a mobile phone and a remote server?
Have you read the documentation for the javax.microedition.io package?

Categories