我正在尝试做多客户端服务器。我已经使用多线程设置服务器。我的问题是我如何一次加入(运行)20个客户端?我还需要使用线程吗?我正在考虑使用我的客户端创建构造函数来设置连接并获取随机名称,然后创建循环,通过其中的20个并设置它们? 这是代码: //服务器
package com.multi;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static final int PORT = 4445;
public void startServer(){
try {
ServerSocket server = new ServerSocket(PORT);
System.out.println("Server is waiting for connetions...");
while (true) {
Socket socket = server.accept();
new ServerThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
new Server().startServer();
}
}
//服务器多线程
package com.multi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class ServerThread extends Thread {
Socket socket;
String message = null;
ServerThread(Socket socket) {
this.socket = socket;
}
public void run(){
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((message = bufferedReader.readLine()) != null){
System.out.println("Client: " + message);
}
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//客户端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Client extends JFrame{
private String[] names = { "Sara", "Jessica", "Tom", "Emanuel", "Joshua",
"Kristin", "Kirito", "Goku", "Bulma" };
private JTextField chatWindow;
public void clientJoin() {
for (String name : names) {
try {
chatWindow = new JTextField();
add(new JScrollPane(chatWindow));
setSize(300, 150);
setVisible(true);
Socket socket = new Socket("localhost", 4445);
PrintWriter printWriter = new PrintWriter(
socket.getOutputStream(), true);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(System.in));
System.out.println(name);
while (true) {
String readerInput = bufferedReader.readLine();
printWriter.println(name + ": " + readerInput);
chatWindow.setText(name);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
Runnable r = new Runnable() {
@Override
public void run() {
new Client().clientJoin();
}
};
new Thread(r).start();
}
}
}
答案 0 :(得分:0)
如果要测试并发访问,那么是的,您需要启动20个客户端线程。类似的东西:
for (int i = 0; i < 20; i++) {
Runnable r = new Runnable() {
@Override
public void run() {
startAClient();
}
};
new Thread(r).start();
}
答案 1 :(得分:0)
这对我有用:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Client extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private static String[] names = new String[]{ "Sara", "Jessica", "Tom", "Emanuel", "Joshua",
"Kristin", "Kirito", "Goku", "Bulma" };
private JTextField chatWindow;
private static int c = 0;
public void clientJoin(String name) {
try {
chatWindow = new JTextField();
add(new JScrollPane(chatWindow));
setSize(300, 150);
setVisible(true);
Socket socket = new Socket("localhost", 4444);
PrintWriter printWriter = new PrintWriter(
socket.getOutputStream(), true);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(System.in));
System.out.println(name);
System.out.println(name);
while (true) {
String readerInput = bufferedReader.readLine();
printWriter.println(name + ": " + readerInput);
chatWindow.setText(name);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
while (c < names.length-1) {
Runnable r = new Runnable() {
@Override
public void run() {
new Client().clientJoin(names[c]);
}
};
new Thread(r).start();
System.out.println("ges here");
c++;
}
}
}