-
making a server handle multiple clients at once?
Hello,Im making a chat program and so far ive programmed the client and the server.Unfortunately my server can only recive messages and send it too only 1 client at a time,so for example:
1.Client1 connects to the server.
2.Client1 types a message and clicks send
3.The server recives the message and sends it back to the chat text area to Client1 with Client1's name.
4.Client2 Connects types a message and clicks send.
5.Client2 Never gets a reply from the server(even though it is connected to the server).
6.When Client1 dissconectes from the server and client2 is the only one connected it gets a reply.
Can someone solve this problem for me so everyone can talk to the server and the server outputs all the mesages to all the client at once.
My current code for the server is:By the way run() is what handles all the messages at sends them.
Code:
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
public class server implements Runnable {
ServerSocket server;
Socket income;
DataInputStream input;
PrintStream output;
Thread t;
int numbers = 1; //number of clients connected
public void init()
{
t=new Thread(this);
t.start();
}
public server() {
try {
server = new ServerSocket(4001);
}catch(IOException ff){
}
try {
while(true){
income = server.accept();
run();
numbers = 1;
}
}catch(IOException ee){
}
//Counter.start();
while(true) {
}
}
public void run()
{
try {//starts try 1
while(numbers > 0){
input = new DataInputStream(income.getInputStream());
output = new PrintStream(income.getOutputStream());
output.flush();
String bye = "bye1";
String read = input.readLine();
if (read == null){
numbers--;
System.out.println("DISCONECTED");
}
System.out.println(read);
output.println(read);
t.sleep(10);
//ends try
}
}
catch(IOException e){
System.out.println(e);
}catch(InterruptedException gg){
}
}
public static void main(String[] args){
server app = new server();
}
}
Thanks.So you don't get confused the thread t does not really do nothing.
-
Your code calls run method when a Socket is recieved through the accept() method of ServerSocket. Since the run method is not executing as a different thread, ServerSocket will not accept any other client untill run method finish its execution.
I'm posting a sample code which will make you to understand how to handle multiple client concurrently
import java.net.*;
import java.io.*;
public interface SocketListener {
public void socketRecieved(Socket socket);
}
public class Server implements Runnable{
private int port = 0;
private SocketListener socketListener = null;
public Server(int port,SocketListener socketListener) {
this.port = port;
this.socketListener=socketListener;
}
public void run() {
Socket socket = null;
try
{
ServerSocket s = new ServerSocket(port);
while(true) {
socket = s.accept();
new SocketHandlerThread(socket).start();
}
}catch(IOException ioexception){
iOException.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
private class SocketHandlerThread extends Thread{
private Socket socket = null;
public SocketHandlerThread(Socket socket){
this.socket = socket;
}
public void run() {
socketListener.socketRecieved(socket);
}
}
}
class SocketHandler implements SocketListener{
public static void main(String args[]) {
Runnable server = new server(2080,new SocketHandler());
new Thread(server).start();
}
public void socketRecieved(Socket socket){
//write to read the data from socket
}
}
Similar Threads
-
By rperez in forum Database
Replies: 5
Last Post: 01-02-2009, 04:14 PM
-
By Mike G in forum Database
Replies: 0
Last Post: 06-29-2001, 11:10 AM
-
By sparkytheowl in forum Architecture and Design
Replies: 0
Last Post: 05-01-2001, 03:31 PM
-
By FELIX in forum VB Classic
Replies: 10
Last Post: 04-24-2001, 08:03 AM
-
By Jason Langston in forum Enterprise
Replies: 1
Last Post: 07-12-2000, 02:53 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks