简体   繁体   中英

How to do multiple clients with 1 server?

This is my server. I'm using JFrame by the way. Actually I have many pc's when I run the server in pc 1 then the client that is on the pc 2 and pc 3. The pc 3 client connect but the server cannot receive the message. While the pc 2 client has connected.

 package server;
 import java.net.*;
 import java.io.*;

public class FrmServer extends javax.swing.JFrame {

ServerSocket providerSocket;
Socket connection=null;
ObjectOutputStream out;
ObjectInputStream in;
String message;

//To run the connection
public void run(){

for the connection

    try{
        providerSocket = new ServerSocket(9090);
        msgArea.append("Waiting for connection....");
        connection = providerSocket.accept();

        out = new ObjectOutputStream(connection.getOutputStream());
        out.flush();

        in = new ObjectInputStream(connection.getInputStream());
        sendmessage("Connection is successful...");

        while(true){
           message = (String)in.readObject();

           if(!message.isEmpty())
               msgArea.append("\nClient: "+message);
        }

    }
    catch(Exception e){

    }

}

public void sendmessage(String msg){

    try{
        out.writeObject(msg);
        out.flush();
        msgArea.append("\nServer: "+msg);
    }catch(Exception e){

    }
}

/**
 * Creates new form FrmServer
 */
public FrmServer() {
    initComponents();
}

private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
    sendmessage(txt.getText());
} 
public static void main(String args[]) {
FrmServer s = new FrmServer();
    s.setVisible(true);
    s.run();

}

Client

package client;

import java.net.*;
import java.io.*;

public class FrmClient extends javax.swing.JFrame {

Socket requestSocket;
ObjectInputStream in;
ObjectOutputStream out;
String message;
/**
 * Creates new form FrmClient
 */
public FrmClient() {
    initComponents();
}

public void run(){

    try{
        requestSocket = new Socket("10.99.225.12",9090);
        msgArea.append("Connected to the server...");

        out = new ObjectOutputStream(requestSocket.getOutputStream());
        out.flush();

        in = new ObjectInputStream(requestSocket.getInputStream());

        while(true){
           message = (String)in.readObject(); 

           if(!message.isEmpty());
               msgArea.append("\nServer: "+message);
        }
    }
    catch(Exception e){

    }

}

public void sendmessage(String msg){

    try{
        out.writeObject(msg);
        out.flush();

        msgArea.append("\nClient: "+msg);
    }
    catch(Exception e){

    }

}

private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
    sendmessage(txt.getText());
}  
private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
    // TODO add your handling code here:

    try{
        sendmessage("Got to go.. Goodbye!");
        in.close();
        out.close();
        requestSocket.close();
    }
    catch(Exception e){

    }

} 
public static void main(String args[]) {
FrmClient c = new FrmClient();
    c.setVisible(true);
    c.run();
}

You'll have to make for each socket (client) a thread, and handle them.

For so far I see, there's only one connection made, as you initialise connection only once.

You could make a list, with all sockets and write/read from/to their input/output stream.

You need a dedicated thread to listen for a connection continuously.

Once you accept a connection, create two threads for each accepted connection, one for reading incoming data, another one to write data to the socket.

Here is a simple modification of your code, although it can be written in a better way.

  1. Make your JFrame class implements Runnable.

     public class FrmServer extends javax.swing.JFrame implements Runnable 
  2. implements run method in your JFrame class

     @Override public void run() { while (true) { try { msgArea.append("Waiting for connection...."); connection = providerSocket.accept(); new ConnectionWriter(connection, msgArea).start(); new ConnectionReader(connection, msgArea).start(); } catch (IOException ex) { System.err.out(ex); } } } 
  3. Create a thread to write to a socket

     public class ConnectionWriter extends Thread { ObjectOutputStream out; Socket connection; JTextArea msgArea; public ConnectionWriter(Socket connection, JTextArea msgArea) { this.connection = connection; this.msgArea = msgArea; } @Override public void run() { try { out = new ObjectOutputStream(connection.getOutputStream()); out.flush(); sendmessage("Connection is successful..."); } catch (IOException e) { System.out.println(e); } } public void sendmessage(String msg) { try { out.writeObject(msg); out.flush(); msgArea.append("\\nServer: " + msg); } catch (IOException e) { System.err.println(e); } } } 
  4. Create a thread to read from a socket.

     public class ConnectionReader extends Thread { ObjectInputStream in; Socket connection; JTextArea msgArea; public ConnectionReader(Socket connection, JTextArea msgArea) { this.connection = connection; this.msgArea = msgArea; } @Override public void run() { try { in = new ObjectInputStream(connection.getInputStream()); while (true) { String message = (String) in.readObject(); if (!message.isEmpty()) { msgArea.append("\\nClient: " + message); } } } catch (IOException ex) { System.err.out(e); } } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM