I want to implement a multi-threaded client socket in JAVA which will connect with multiple servers..
For eg..
The client will accept an array of numbers and split that array into two.. The two arrays will be sent to two servers and the result from the servers will be combined by the client to get the Final sorted array..
Any help guys???
Let's split the "client" meaning in 2:
client application, the app which you're creating which will communicate with one or more servers
client-socket, a client side of a communication channel, which can be connected to maximum one server at any time
As a side note, only server-sockets can handle multiple clients "at the same time"
Now, you can not have a client-socket connect to multiple servers, but you can have a client application connecting to multiple servers by having an instance of client-socket connect to each server.
So what your client application needs to do, is manage a list of client-sockets that connect to your servers, and upon receiving all the replies, aggregate the answer.
Apache hadoop is what you have to use in this scenario.
Related
While creating a Secure Instant messenger in java , if there are many clients
for example
3 clients
client A,B,C
A wants to connect to B and C both. So do i need to create different socket connection for both of them separately?
If so than is not it is a restriction like if there are 10,000 clients and each wants to connect to rest of them so i will need million ports?
Yes, your solution would work. But when you add many more users, the instant messager will lag and be hard to maintain.
The better solution is to have one connection for each client, a connection to a server. clients will connect to this server (which will also handle authentication), and the server will sort out the messages and deliver them to another client(the messages destination).
I'm going to create an authentication server which itself interacts with
a set of different Oauth2.0 servers.
Netty seems to be a good candidate to implement network part here.
But before start I need to clear some details about netty as I'm new to it.
The routine will be as follows:
The server accepts an HTTPS connection from a client.
Then, not closing this first connection, it makes another connection
via HTTPS to a remote Oauth2.0 server and gets data
After all, the server sends the result back to the client which is supposed to keep the connection alive.
How to implement this scenario with Netty?
Do I have to create a new netty client and/or reconnect it each time I need to connect to a remote Oauth2.0 server?
If so, I'll have to create a separate thread for every
outgoing connection which will drastically reduce performance.
Another scenario is to create a sufficient number of Netty clients
within a server at the beginning (when server starts)
and keep them constantly connected to the Oauth2.0 servers via HTTPS.
That's easily done with Netty. First you set up your Netty server using the ServerBootstrap and then in a ChannelHandler that handles your connection from the client you can use e.g. the client Bootstrap to connect to the OAuth server and fetch the data. You don't need to worry about creating threads or similar. You can do it all in a non-blocking fashion. Take a look at and try to understand how this example works:
https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/proxy/HexDumpProxyFrontendHandler.java#L44.
Is it possible to fetch the IP addresses of connected clients to a server from the client side? I know it's possible server sided, but is it client sided?
Note: I'm talking about server-client connection using a basic Socket.
Only if the server purposely provides that list.
Otherwise, it is not possible to get any information of other clients connected to a server you are connected to (this, of course, applies to Java, but can also be understood as a general concept of networking -- in the context of peer to peer, client/server, sockets).
Not without a script or program on the client end of the socket having code to retrieve it (e.g. Javascript on a web page). You cannot tell this strictly from the server side.
You are talking with your server, and only server talks with other clients directly. So only server can send you clients ip addresses.
So, this is possible, but must be implemented on server.
I am trying to write an application which will run in many machines, and these machines will communicate with each other through sending a stream of UDP packets. Usually one machine will be sending many packets to another machine, until they switch and another one will do the same to another machine and so forth. Choosing UDP is due to the nature of the application (real-time/speed more important than reliability). I also understand that UDP is a connectionless protocol.
By doing a quick prototype, I managed to create a NIO Datagram Server (bind), and a NIO Datagram Client (connect). However, I realized that I can only send one way only from the client to the server (or maybe I am missing something?). I couldn't send in the opposite direction.
Also, since UDP is a connectionless protocol, I thought that it was supposed to accept many clients sending packets to it (n-client to one server), and the other way around (server sending to clients one-by-one or multi/broad-cast).
Should I create a server in the client listening to a different port to acheive two-way? Can n-client send packets to one server at the same time?
I just want someone to clear this thing to me. No need for sample code (although it will be greatly appreciated), you can give me some pointers.
Thank you.
For what you are describing, you need one server thread and one more client thread in EACH and EVERY instance of your program running on different machines.
Create a multi threaded program, with a serving thread, and a client thread - the client thread needs to know about all servers - IP and Port. The server simply listens on a port on the current machine.
If you run this same program on multiple machines, You will get a p2p group.
You can also set up a tracker on a free server in the internet. A tracker program will rest in a well known URL, and will maintain a list of participating machines. Each instance of your program when started on a machine will update the tracker of details necessary to connect to it, and the tracker can maintain a list of this data and share it all with any new instance coming up later.
I have a spreadsheet application in Java, and one of the features it provides (which I developed) is sheet sharing. Basically, anyone can be a client or a server because the app has both server and client code. The user who is the server creates the share, specifies the IP, and then the share is created and active (best case scenario) with the server listening for clients on its IP and selected port.
At the moment, the client needs to enter the IP and port of the server that's listening in order to connect. The server then creates a new socket for that client and communicates with in on a separate thread, while the server continues listening on another (traditional TCP behavior). This is all working fine.
What I need to develop is auto-discovery, e.g. a client does not need to type in an IP or port, they simply select 'Join a share...' from the menu and then it starts looking for servers. When one is found, it should send its list of active shares on that IP. The user then selects which share to join from the list, and is connected.
However, I have doubts on how to tackle this issue. Should I use broadcast to poll servers, like DHCP does? Or is there an easier way?
What I'd like to implement is:
Client -> polls local network -> finds a server -> server sends active share list to client -> client selects share to join -> connected!
Technically, what you're looking for is active servers that are running your spreadsheet application.
One possibility would be for your server code to send out an "alive" message to the network every so often (say every 15 seconds). Your client code would listen for these "alive" messages, and produce a meaningful list of spreadsheet servers.
Another possibility would to to use the same database engine that you're using to store the spreadsheets to store the IP and port of the connected server code. The client code would just read the database table to get the connections.