Problem Statement: In this article, we are going to write a program that illustrates the Client-Server Model using fork() system call which can handle multiple clients concurrently.
I just got an assignment.It is to design a java socket server which accepts incoming client connections.The server's job is to accept it and keep track of all the connected clients and it has to advertise the connected client list to all the clients that connect.I am a newbie to Java.I just know that this task has to be completed with the help of threads.I have some knowledge in Java I/O,Networking and Threads.But I am unable to collaborate all these concepts in one single application.It would be helpful if someone would give a model program,or perhaps how to proceed,then it would be helpful.
This is one of the possible solutions, you can design yours in many ways:Every thread manages one connection with the client. You have a main class which accepts connections and keeps the list of active connectionsAt every new connection you fork one thread which manages it. You define a class which manages the thread and which exposes a synchronized method to be notified of new clients.Every time the main class gets a new connection, it informs the other through the method defined at the previous step.
PAY ATTENTION: when you call methods one class which represents a thread (thus it is accessed from a different thread than the one you are calling the method from) you must be careful not to incur in concurrency problems (you can see some examples in the second link I sent).
Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract event-driven asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.
NIO framework library,client server framework library, ora networking socket library.However, it's much more than that. Please take a look around the list of the features that enable rapid network application development, and what people says about MINA. Please grab yourself a download, try our Quick Start Guide, surf our FAQ or start join us on our community
Is the code below sufficient to accept concurrent UDP transmissions? More specifically, if 2 clients transmit concurrently, will DatagramSocket queue up the transmissions and deliver them one by one as I call receive(), or will only one make it through?
If the packets make it to your networking interface (imagine lost packets on a congested wireless channel) they will passed up and the blocking method socket.receive(p) will be called. If there is a collision of packets on the channel because of two clients transmitting at the same time you will not get any of the two packets. But this is most likely not possible because the access technology of networking interfaces will take care of this, checkCSMA/CA or CSMA/CD
A basic solution would have on thread responsible for handling a number of incoming requests (with your desired limit) and then handing them off to other worker/request handler threads. This basic structure is very much the same with most servers: a main thread responsible for handing off requests to worker threads. When each of these worker threads is finished, the you can update a shared/global counter to let the main thread know that it can establish a new connection. This will require synchronization, but it's a neat and simple abstraction.
This is just to give you an idea of what you can start out with. But when you get the hang of it, you'll be able to make optimizations and enhancements to make sure the synchronization is all correct.
Ran Luer wrote:Hello. I am fairly new to Java RMI and I have some questions regarding how a client can interact to particular server
I have developed a working program consist of 1 server and 1 client. I was asked to modify the program into multiple server and 1 client where 1 client send request to master server, then master server divide the task to several other sub server.
Paul Clapham wrote:[
Hi Ran, welcome to the Ranch!
I'm confused about whether the master server is supposed to send requests to the other sub servers, and then return the results to the client, or whether the clients are supposed to connect directly to the sub servers themselves. But from what you've written in your post, it looks to me like you have the same confusion. At the beginning of your post it looks like it's the former version, but by the time you get to the end of the post it looks like you're talking about changing the client software, which would be the latter version.
So to get your clear view on how to achieve your goal, I think it would be very helpful to consider what exactly the goal is. I know it sounds obvious, but there's no point in writing code when you don't know what the code is supposed to do. The hard part is to realize that you don't know.
Paul Clapham wrote:Good, that's much simpler.
The second part is for the master server to split a request into several tasks. That's very context-dependent, by which I mean it depends very much on the nature of the request and the tasks it's supposed to be split into.
Ran Luer wrote:My problem is, I don't really know how to make master server interact with sub server (they're in different port but in the same host). I know sub server is successfully created but to send all the task is quite blur to me.
Paul Clapham wrote:
Sure you do. The master server is acting as a client. The sub server is acting as a server. You already know how to write code for a client to communicate with a server.
Ran Luer wrote:After checking the time taken, it seems that sometime time taken for 1 thread is shorter than 2 - 5 thread. And there are time when 1 is longer than 2 but is shorter than 3-5 (i hope you get what i mean) and rarely 5 is shorter than 1-4 thread.
Did i do something wrong in the code? I thought 5 thread will always take shorter time compare to 1-4 thread.
Paul Clapham wrote:
Well, for this post I'll just point out that line 34 is unnecessary. Just call join() on each of the threads you're waiting for, it doesn't matter whether the threads are alive or not. But otherwise, yes, start a new thread.
This section shows you how to write a server and the client that goes with it. The server in the client/server pair serves up Knock Knock jokes. Knock Knock jokes are favored by children and are usually vehicles for bad puns. They go like this:
The server program begins by creating a newServerSocket object to listen on a specific port (see the statement in bold in the following code segment). When running this server, choose a port that is not already dedicated to some other service. For example, this command starts the server program KnockKnockServer so that it listens on port 4444:
ServerSocket is ajava.net class that provides a system-independent implementation of the server side of a client/server socket connection. The constructor for ServerSocket throws an exception if it can't listen on the specified port (for example, the port is already being used). In this case, the KnockKnockServer has no choice but to exit.
Theaccept method waits until a client starts up and requests a connection on the host and port of this server. (Let's assume that you ran the server program KnockKnockServer on the computer named knockknockserver.example.com.) In this example, the server is running on the port number specified by the first command-line argument. When a connection is requested and successfully established, the accept method returns a newSocket object which is bound to the same local port and has its remote address and remote port set to that of the client. The server can communicate with the client over this new Socket and continue to listen for client connection requests on the original ServerSocket This particular version of the program doesn't listen for more client connection requests. However, a modified version of the program is provided inSupporting Multiple Clients.
After the KnockKnockProtocol is created, the code calls KnockKnockProtocol's processInput method to get the first message that the server sends to the client. For this example, the first thing that the server says is "Knock! Knock!" Next, the server writes the information to thePrintWriter connected to the client socket, thereby sending the message to the client.
Step 3 is encoded in the while loop. As long as the client and server still have something to say to each other, the server reads from and writes to the socket, sending messages back and forth between the client and the server.
The server initiated the conversation with a "Knock! Knock!" so afterwards the server must wait for the client to say "Who's there?" As a result, the while loop iterates on a read from the input stream. The readLine method waits until the client responds by writing something to its output stream (the server's input stream). When the client responds, the server passes the client's response to the KnockKnockProtocol object and asks the KnockKnockProtocol object for a suitable reply. The server immediately sends the reply to the client via the output stream connected to the socket, using a call to println. If the server's response generated from the KnockKnockServer object is "Bye." this indicates that the client doesn't want any more jokes and the loop quits.
The KnockKnockProtocol class implements the protocol that the client and server use to communicate. This class keeps track of where the client and the server are in their conversation and serves up the server's response to the client's statements. The KnockKnockProtocol object contains the text of all the jokes and makes sure that the client gives the proper response to the server's statements. It wouldn't do to have the client say "Dexter who?" when the server says "Knock! Knock!"
7fc3f7cf58