简单java聊天室
非常简单,我想创建一个聊天室,它可以接受多个客户端,所有客户端都可以分配自己的ID。只要他们输入任何内容,它就会发送给所有用户。目前,我有一个echo客户端服务器,客户端在其中输入一些内容,然后将其回显。我的第一个问题是如何允许用户给自己一个用户名?很明显,我需要一个变量来接受这个名称,你建议我把它放在哪个类中?我怎么才能得到这个名字呢简单java聊天室,java,client,chatroom,java-server,Java,Client,Chatroom,Java Server,非常简单,我想创建一个聊天室,它可以接受多个客户端,所有客户端都可以分配自己的ID。只要他们输入任何内容,它就会发送给所有用户。目前,我有一个echo客户端服务器,客户端在其中输入一些内容,然后将其回显。我的第一个问题是如何允许用户给自己一个用户名?很明显,我需要一个变量来接受这个名称,你建议我把它放在哪个类中?我怎么才能得到这个名字呢 if(输入相等信号(“用户名”):“”) 然后我需要做的就是回应客户对所有客户说的话。我不知道该怎么做,所以任何建议都将不胜感激。虽然我在网上找到了一些教程和示
if(输入相等信号(“用户名”):“
”)
然后我需要做的就是回应客户对所有客户说的话。我不知道该怎么做,所以任何建议都将不胜感激。虽然我在网上找到了一些教程和示例代码,但我不理解它,如果我不理解它,即使它确实有效,我也不会感到舒服。谢谢
这是我的密码:
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?)
}
}
Scanner in = new Scanner(mySocket.getInputStream());
while (true)
{
String inputLine = in.nextLine();
if (inputLine.equalsIgnoreCase("Bye"))
{
out.println("Closing connection");
System.exit(0);
}
else
{
out.println(inputLine);
}
}
}最简单的方法可能是拥有一个公共类,所有客户机(在创建时)都使用该类连接到该类。该链接甚至提供了一些java代码 要让客户端拥有用户名,最简单的方法就是让服务器发送的第一条消息是“输入您想要的用户名:”,然后按原样获取返回值。否则,您可以使用
inputLine.substring(int)
从username:{username}
获取用户名。您可以将用户名存储在EchoThread中。避免重复用户名需要在EchoServer中存储一组用户名。然后可以将用户名与消息一起传递给可观察类
当前,您的程序使用顺序消息传递,就像客户端和服务器交替发送消息一样(更具体地说,客户端发送消息,然后服务器发送相同的消息)。您需要对此进行更改,以便客户端可以随时接收消息。您可以通过在客户端创建一个线程来实现这一点,该线程要么只是发送,要么只是接收(并在客户端本身中执行另一个)。在客户端:
...
System.out.println("Connected OK");
PrintWriter socketOut = new PrintWriter(echoSocket.getOutputStream(), true); // set up output to socket
new ReceiverThread(echoSocket).start();
while (true)
{
String userInput = kbdIn.nextLine(); // get input from the user
socketOut.println(userInput); // send user input to the socket
}
...
ReceiverThread运行方法内部尝试。。。catch
:(其余部分看起来与EchoThread相同)
System.exit()可能不是最好的主意,但这只是让你知道该怎么做。Hi-1)我不确定这是否只是一个“有趣的实验”,一个家庭作业。。。或者如果你真的想创建一个人们可以使用的聊天室。如果是后者,我强烈鼓励您考虑编写一个Web应用程序:例如,使用Tomcat和.jsp。2) 问:“回应客户对所有客户说的话?”答:为每个客户创建某种“列表”。至少,您需要a)用户名和b)IP地址或套接字。