很简单,我想创建一个接受多个客户端的聊天室,所有这些客户端都可以分配自己的 ID。每当他们输入任何内容时,它都会发送给所有用户。目前我有一个回显客户端服务器,客户端输入一些内容并回显。我的第一个问题是如何允许用户给自己一个用户名?显然我需要一个变量来接受名称你建议我把它放在什么类中?以及我将如何获得名称本身类似于
if (theInput.equalsIgnoreCase("username" : "
“))
然后我需要做的就是回应客户对所有客户所说的话。我不知道如何做到这一点,所以任何建议都将不胜感激。虽然我在网上找到了一些教程和示例代码,但我不理解它,如果我不理解它,即使它确实有效,我也会感到不舒服。谢谢
这是我的代码:EchoClient
'// echo client
import java.util.Scanner;
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
//PrintWriter socketOut = null;
try {
echoSocket = new Socket("127.0.0.1", 4444); // connect to self at port 4444
System.out.println("Connected OK");
Scanner socketIn = new Scanner(echoSocket.getInputStream()); // set up input from socket
PrintWriter socketOut = new PrintWriter(echoSocket.getOutputStream(), true); // set up output to socket
Scanner kbdIn = new Scanner(System.in); // Scanner to pick up keyboard input
String serverResp = "";
String userInput = kbdIn.nextLine(); // get input from the user
while (true) {
socketOut.println(userInput); // send user input to the socket
serverResp = socketIn.nextLine(); // get the response from the socket
System.out.println("echoed back: " + serverResp); // print it out
if (serverResp.equals("Closing connection")) { break; } //break if we're done
userInput = kbdIn.nextLine(); // get next user input
}
socketOut.close();
kbdIn.close();
socketIn.close();
}
catch (ConnectException e) {
System.err.println("Could not connect to host");
System.exit(1);
}
catch (IOException e) {
System.err.println("Couldn't get I/O for connection");
System.exit(1);
}
echoSocket.close();
}
}'
回声服务器
// multiple (threaded) server
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServerMult {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(4444); // create server socket on this machine
System.err.println("Started server listening on port 4444");
while (true) { // as each connection is made, pass it to a thread
//new EchoThread(serverSocket.accept()).start(); // this is same as next 3 lines
Socket x = serverSocket.accept(); // block until next connection
EchoThread et = new EchoThread(x);
et.start();
System.err.println("Accepted connection from client");
}
}
}
回声线
// thread to handle one echo connection
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class EchoThread extends Thread {
private Socket mySocket = null;
public EchoThread(Socket socket) { // constructor method
mySocket = socket;
}
public void run() {
try {
PrintWriter out = new PrintWriter(mySocket.getOutputStream(), true);
Scanner in = new Scanner(mySocket.getInputStream());
String inputLine;
while (true) {
inputLine = in.nextLine();
if (inputLine.equalsIgnoreCase("Bye")) {
out.println("Closing connection");
break;
} else {
out.println(inputLine);
}
}
out.close();
in.close();
mySocket.close();
} catch (Exception e) {
System.err.println("Connection reset"); // bad error (client died?)
}
}
}