I am trying out a simple socket programming example. I was able to run a server application and client application to communicate with each other. I now need to know a tutorial that explains how 2 clients could communicate with each other through the server.
How can I do this ? Can someone point me to a good tutorial or an explanation on how this can be done in Java
It's not that different and difficult than writing client/server pair. You just have to create threads on server just there, where you accept connections from clients. If your clients should communicate each other, than you surely need a list to store them. And you have to implement, what your server does (communication) in this thread.Here is a good chat programm tutorial: http://www.dreamincode.net/forums/topic/259777-a-simple-chat-program-with-clientserver-gui-optional/
Related
I want to have multiple clients communicating via a server AND for the clients to be able to affect each other via what information they pass to the server (like a chat maybe).
Background:
I am working on an assignment in regards to socket programming in Java, with a server and multiple clients, and since it is an assignment I will refer to public documentation to describe my problem. The assignment is to write a game and it has been going quite well. To set up the sockets I have been following the format from this tutorial:
https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html
In the documentation above we have one server that keeps calling a serverThread object, using the accept() method to establish contact with a client that connects. The server is just listening and creating threads for the clients to hop in, we also have an object that is a protocol of communication but I don't mind that for now. All in all the documentation provides the following classes:
KKMultiServer (https://docs.oracle.com/javase/tutorial/networking/sockets/examples/KKMultiServer.java)
KnockKnockClient (https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/networking/sockets/examples/KnockKnockClient.java)
KKMultiThread (https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/networking/sockets/examples/KKMultiServerThread.java)
(KKProtocol)
Problem:
I have managed to write a game that works very well, but here is my problem, every client is in isolation within their own thread. I want multiple clients to be able to interact with each other via the server, if player 1 moves it should update the screen for player 2, and vice versa is my intention. Right now, if multiple clients connect to the serverThread it is as if they are in two separate games, which is not what I want.
In the java documentation above we can have the same problem, with KnockKnock jokes. I want one client to be able to write something, and then for the server to pass on to another client what the first client wrote. However, how can we have data from one client that can be passed to another client via the server, if they are all isolated in different threads (as per the documentation)?
I'm trying to create a desktop app with RMI (Java) for school. It's a simple chat app and I found plenty of examples about creating a chat app with RMI.
The "problem" is that I have to make this app fault tollerant, so my professor asked me to create also a peer-to-peer connection between clients if the server doesn't work.
So I have to create two types of connections: client/server and peer-to-peer.
I have two questions:
1) Which is the best way to save the data in the local client side in order to access them if the server doesn't work?
2)Can I create peer-to-peer connection with sockets or there is another way on doing this?
Thank you very much.
1.You should detect a remote exception error or server is when unreacehable using
try {
}
catch(RemoteException ex){
//do some peer - to -peer look up here or other way
}
in your client code which tries to connect to server and when accessing a STUB on RMI. Second , you need a central server location that Peer-TO-Peer data exchange is possible.AT least when every peers comes in to connecting to the central server(RMI server) or want to chat additionally it needs to record some information about him self in some other shared data repository.This not the best answer but ,just an ice breaker.
2.Sockets and Yes sockets are pretty enough for peer-to-peer connection!!
I know my IP address, and that of my friend.
How can I transfer objects/files between the two machines?
I am an advanced Java programmer, but have never worked with networks before.
EDIT:
I am now using an API called jnmp2p ( http://code.google.com/p/jnmp2p/ ).
It works fine when I use internal IPs, but fails when I give the external ones.
How do I connect to a computer that isn't on my private network?
If you looking for communication between two java applications and do not want to meddle with the low level networking details, then you can use following two approaches, depending on the type of applications you are dealing with.
If both the application (on two machines) are java standalone applications, then RMI is the best bet. Check out the basics from these links (1,2)
If your application (receiving files/objects) is a web application then its you can write the Servlet on the serve side and then write a client application to send files/objects(binary) to server. Commons FileUpload is very popular library for this purpose.
Author of jnmp2p here. I don't maintain the library any more because I've moved onto other things. However I had some comments.
Peer to peer communication with IPs outside your private network is a hard problem. This is because stateful NATs and firewalls on both ends have become common-place, which prevent you from establishing connections between machines directly.
For example skype uses a rendezvous service where both machines start outbound connections to a third machine and communicate via that. Aside from setting up additional infrastructure that any peer to peer solution is going to be limited to subnets within your NAT, so solutions like JNMP2P or RMI (with gross modifications) are going to be your best bet.
Considering application_A on a machine_1 needs information about machine_2, provided by application_B (which is located on machine_2) and that both machines are in the same network, what would you guys suggest to be the easiest way of implementing the communication between the 2? (I was thinking simple socket connection).
Note: the information required is something in the lines of a few bytes, so nothing big.
You can either use socket based communication or Java RMI.
I would recommend Java RMI as its easier and saves you from handling raw socket communication.
If you are familiar with Spring framework, then writing RMI application in spring is very easy. Check Exposing services using RMI (Heading 17.2)
There are different ways to implement this but they all come down to one thing: communication over sockets.
If the information is only some bytes, implementing the sockets themselves is probably your best bet, if things start to get bigger, you might want to look into some middleware.
You can run a server program on Machine2 using ServerSocket and a client program in Machine1 can request for info.
You can try web services. JAX-RS would be the simplest.
I want to build a chat application and am confused about deciding whether to use sockets or RMI to build the application. I have heard that RMI is difficult to configure and deploy over the Internet, since that is my intention I was wondering what would be more appropriate to go with, sockets or RMI. Also is it easier to solve issues because of NAT in sockets or RMI ?
What if I want to add voice support at some later point, does it help deciding which way to go ?
1. For applications like Chat Messenger, my bet will be on Sockets.
2. RMI will be an over kill here.
3. Moreover NAT issue is not about Socket or RMI, its about Static IPs.
4. If you want to deploy a Chat Server over the net, then first you must have a Static IP, you need to have to ask your ISP to provide you with one of them at extra cost, or there are sites over internet, that makes your dynamic ips as static.
5. But if your server is locally located in a LAN environment, then i think you won't have a problem in doing it.
Both are reasonable choices that could be used to build a chat server/client. A socket can be set up to take incoming connections and start a new thread for each "chatter" alternatively RMI can be used to create a distributed object on which the client can call methods.
RMI is basically a layer over sockets often used in distributed computing where some transparency is needed and remote methods need to be called. It also allows for stateless connections to the server.
If you choose to implement the server in RMI just be warned that thread safety may be an issue.
For a local server it is probably easier to use pure sockets.
For more details on RMI:
http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136424.html