I'm new at java but I 've experience on as3. When I was using as3 , I was using some callbacks to notify both server and client when I send either of them a message from the other. But it was a pre-build server named player.io. (as I recall, server was built with c#). There were listeners for messages on both client and server.
But as I started practicing sockets on java, all the examples I saw implement BufferedReader and readLine methods to recieve data from server or client. Problem is , this method waits until it receives data. And this waiting is ofcourse is a big problem, especially for the client.
Is there any way to use callbacks or other methods for communicating between server & client which doesn't cause either of programs to wait?
Related
I have to make a java program in which there are some processes that communicate using HTTP
sockets. But I found that there are only TCP/UDP sockets in Java.
I am little bit confused here. Is there anything like HTTP Sockets ?
Is there any library in java that provides HTTP sockets ?
The processes in my program run on the same system spawned by a single main java program.
I need some way to communicate one process with the other.
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 have a requirement where I need to send message from multiple clients and those clients can be C clients or Java clients to server written in java.
Why I need to send over socket? Because there can be any process in any app on a same system who wants my app to capture there alarms and react accordingly.
Now, I can implement multiple Java clients who can connect to single Java server but how to do the same thing irrespective of it is Java client r C client.
If not socket programming, then what could be the other best way to make this communication happen
All low level network programming is written using sockets. you can have server written in any language communicate with client written in any language. To make this happen we have certain protocol which governs the communication like udp, tcp etc.
high level language provides api which will you to connect any server with single line of code without you needing to create socket. But in C you can create socket and connect it to the server. see this. All you need to know connect server and client is server ip and port...
While your requirements are very broad it looks a lot like a prime candidate for protocol buffers.
https://developers.google.com/protocol-buffers/
Language agnostic.
Platform neutral.
Fast.
Running on ZeroMQ (http://zeromq.org/) where you can push and pretty much run on top of everything.
I am planing to develop JavaScript client application that will connect to Java server using websocket. Server should handle many connected clients.
After some reading I found out websocket single thread. This is not good if I want to run databases query that can block everything for a while.
What I am thinking about is to opening separated websocket for each JavaScript client. One socket is listening for new connection and when connection is established creates some unique id. After that opens new websocket and send id to client using listener socket. When client received id close first socket and connect to new one.
What do you think, is it good solution? Maybe I am missing something?
Spring 4 gives you the chance to use a thread pool. The documentation is here:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html
You could use Akka to manage all the concurrency and thread management for you. Or you could use the Play Framework that already builds on Akka and that supports WebSocket quite nicely. With Play you can choose between Java and Scala on the server side.
You should use NodeJS on the server to handle the socket i/o. You can connect to it via your javascript client apps, and then make calls to your Java based API. NodeJS is non blocking (async) and you should be able to leverage your existing Javascripting skills to quickly build a Node app. You could even use a full MEAN stack to build the client/server app. http://meanjs.org/ or http://mean.io/#!/ are two popular places to start.
I am looking to build an instant messenger in Java.
Clients will connect to the server to log in.
They will start a conversation with one or more other clients.
They will then post messages to the server that will relay the messages to all the clients.
The client needs to be continually updated when users post messages or log in.
so the way I see it, the client needs to run a server itself in a separate thread so that the main server can send stuff to it. Otherwise the client will have to the poll the main server every xyz seconds to get the latest updates. And that would need a separate thread anayway, as that would be purely for getting updates whereas the 'main' thread would be used for when the client initiates actions such as posting messages/inviting others to conversations etc...
So anyone recommendations on how to write this instant messenger? Does it sound like a good idea to make the connection a 'two-way' connection where both the client and server act as servers? Or is polling a better option? Anyone know how the IRC protocol does this?
There's no real advantage of having 2 connections unless they can be handled independently (for example receiving / sending a file usually done in a separate connection). A connection itself is already a two-way communication channel so it can be used to both send and receive messages, events etc. You don't need to poll server since client is able to maintain persistent connection and just wait for data to appear (optionally sending periodic PING-like message to ensure connection is alive).
IRC uses a single connection to server to exchange text commands. For example one of the main commands:
PRIVMSG <msgtarget> <message>
This command can be originated either by client or by server. Client sends PRIVMSG to notify that it wants to deliver message to one or more destination (in IRC this either user(s) or channel(s)). Server's task here is to properly broadcast this message to appropriate clients.
If you're using raw InputOutput streams then yes this is a good way of doing it. You create one thread on the clientside that acts in a similar fashion as the server thread - waits for any incoming updates and when it does it updates the client. I wouldn't call it a server though. So you'd ideally have 2 TCP/UDP connections one for requests made by the client and one to notify the client of server changes.
This solution in an enterprise environment would probably be done through some kind of messaging framework such as Spring Integration but dig deep enough and it will essentially be a similar way to how you mentioned.
Do you need a fully custom protocol or would it be sufficient to use the XMPP? There are several open source libraries implementing XMPP.
http://xmpp.org/xmpp-software/libraries/
e.g. http://www.igniterealtime.org/projects/smack/
For me, to develop instant messaging service, I will use websocket protocol instead of normal java socket because the normal socket can not work well with HTTP protocol and moreover some network providers and firewalls banned custom ports. If you develop it in normal socket, your service could not be accessed by web clients.
Did you plan to develop the instant messaging service yourself? How about using other protocols such as Jabber?