[英]How to do multiple clients with 1 server?
這是我的服務器。 我正在使用JFrame。 實際上,當我在pc 1中運行服務器時,我有很多pc,然后在pc 2和pc 3上運行客戶端。pc 3客戶端連接但服務器無法接收到消息。 當PC 2客戶端已連接時。
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(){
用於連接
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();
}
客戶
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();
}
您必須為每個套接字(客戶端)創建一個線程,並對其進行處理。
到目前為止,我只看到一個連接,因為您只初始化了一次connection
。
您可以列出所有套接字,並對其輸入/輸出流進行讀寫操作。
您需要一個專用線程來連續監聽連接。
接受連接后,為每個接受的連接創建兩個線程,一個用於讀取傳入的數據,另一個用於將數據寫入套接字。
盡管可以用更好的方式編寫代碼,但這是對代碼的簡單修改。
使您的JFrame類實現Runnable。
public class FrmServer extends javax.swing.JFrame implements Runnable
在您的JFrame類中實現run方法
@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); } } }
創建一個線程以寫入套接字
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); } } }
創建一個線程以從套接字讀取。
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); } } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.