- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿,我正在创建一个聊天程序,可以处理一台服务器的多个客户端。一切都按照聊天端的方式进行。我可以让 3 个客户打开并互相交谈,一切都很好。但是,当有人在浏览器中使用该 URL:PORT 时,我还想放置一个小网页,让他们知道这是一个私有(private)服务器,请继续进行此类操作。
对于我的客户端代码,我有这个:
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
public class MainClient {
private Socket clientSocket;
public PrintWriter out;
public BufferedReader userInput;
private ClientReceiver clientReceiver;
private static final int SERVER_PORT = 6427;
private String line;
public static void main(String[] args) {
MainClient uiSender = new MainClient();
uiSender.go();
}
private void go() {
try {
clientSocket = new Socket("localhost", SERVER_PORT);
out = new PrintWriter(clientSocket.getOutputStream(), true);
clientReceiver = new ClientReceiver(this, clientSocket);
userInput = new BufferedReader(new InputStreamReader(System.in));
clientReceiver.start();
while (true) {
line = userInput.readLine();
if (!clientSocket.isClosed()) {
if (line != null) {
out.println(line);
if (line.equals("END")) {
clientReceiver.interrupt();
break;
}
} else {
break;
}
} else {
break;
}
}
} catch (SocketException e) {
System.out.println("Main thread socketexception");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
if (clientSocket != null) {
clientSocket.close();
}
} catch (IOException e) {
System.err.println("Failed to close.");
}
}
System.out.println("Main thread ended");
}
public class ClientReceiver extends Thread {
private Socket clientSocket;
private MainClient mainHandle;
private BufferedReader socketIn;
private String line;
public ClientReceiver(MainClient mainHandle, Socket clientSocket) {
this.mainHandle = mainHandle;
this.clientSocket = clientSocket;
}
@Override
public void run() {
try {
socketIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while (true) {
line = socketIn.readLine();
if (line != null) {
if (line.equals("END")) {
System.out.println("Server sent END message.");
clientSocket.shutdownOutput();
System.out.println("Shutted down socket output");
} else if (line.equals("CLOSE_SOCKET_INPUT")) {
clientSocket.shutdownInput();
System.out.println("Shutted down socket input");
break;
} else
System.out.println(line);
} else {
System.out.println("Server sent null. Exiting...");
break;
}
}
} catch (SocketException e) {
System.out.println("Caught SocketException");
} catch (IOException e) {
System.out.println("Caught IOException");
e.printStackTrace();
} finally {
try {
clientSocket.close();
System.out.println("Closed client socket");
} catch (IOException e) {
System.err.println("BufferedReader or socket failed to close.");
}
System.out.println("ClientReceiver thread ended.");
System.out.println("Server has shut down. Press any key to quit.");
}
}
}
}
服务器代码是:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class MainServer {
private MessageQueue messageQueue;
private ConnectionManager connectionManager;
private BufferedReader inputStream;
private MessageRetriever messageRetriever;
private int _PORT = 6427;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private ServerSocket serverSocket;
public static void main(String[] args) {
MainServer uiThread = new MainServer();
uiThread.go();
System.out.println("Main thread ended");
}
private void go() {
System.out.println("multichat server");
messageQueue = new MessageQueue();
connectionManager = new ConnectionManager(messageQueue);
messageRetriever = new MessageRetriever(messageQueue);
connectionManager.start();
messageRetriever.start();
inputStream = new BufferedReader(new InputStreamReader(System.in));
String line;
do {
try {
line = inputStream.readLine();
messageQueue.addToSendMessage(line);
if (line.equals("END")) {
connectionManager.stopListening();
messageRetriever.interrupt();
break;
}
} catch (IOException e) {
return;
}
} while (line != null);
}
public class MessageQueue {
private ArrayList<String> toSendList = new ArrayList<String>();
private LinkedList<String> receivedMessagesList = new LinkedList<String>();
private LinkedList<ClientInfo> clientList;
private boolean shutdownTriggered;
public MessageQueue() {
clientList = new LinkedList<ClientInfo>();
}
synchronized public void addToSendMessage(String message) {
toSendList.add(message);
notifyAll();
}
synchronized public List<String> retrieveToSendMessages(int index) throws InterruptedException {
while (toSendList.size() == index) {
if (!shutdownTriggered)
wait();
else
throw new InterruptedException();
}
if (index < toSendList.size()) {
return toSendList.subList(index, toSendList.size());
} else {
System.err.println("Index is " + index + ". Send List size is " + toSendList.size());
return null;
}
}
synchronized public String pollReceivedMessage() throws InterruptedException {
while (receivedMessagesList.isEmpty()) {
if (!shutdownTriggered)
wait();
else
throw new InterruptedException();
}
return receivedMessagesList.poll();
}
synchronized public void addReceivedMessage(String message) {
receivedMessagesList.push(message);
notifyAll();
}
public void addClient(ClientInfo info) {
clientList.add(info);
}
synchronized public LinkedList<ClientInfo> getClientList() {
return clientList;
}
synchronized public void changeClientInfoStatus(int port, String newStatus) {
for (ClientInfo info : clientList) {
if (info.getPort() == port) {
info.setStatus(newStatus);
break;
}
}
notifyAll();
}
public void closeClientCommunicatorInputStreams() {
for (ClientInfo info : clientList) {
info.getCommunicator().closeInputStream();
}
}
private boolean allClientInputStreamClosed() {
for (ClientInfo info : clientList) {
if (!info.getStatus().equals(ClientInfo.STATE_CLOSED_INPUTSTREAM))
return false;
}
return true;
}
private boolean allClientOutputStreamClosed() {
for (ClientInfo info : clientList) {
if (!info.getStatus().equals(ClientInfo.STATE_CLOSED_OUTPUTSTREAM))
return false;
}
return true;
}
synchronized public void waitForAllClientInputStreamClose() {
while (!allClientInputStreamClosed()) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
notifyAll();
}
synchronized public void waitForAllClientOutputStreamClose() {
while (!allClientOutputStreamClosed()) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
notifyAll();
}
synchronized public void shutdown() {
shutdownTriggered = true;
notifyAll();
}
}
public class ClientInfo {
private ClientCommunicator communicator;
private String status;
public static final String STATE_CLOSED_INPUTSTREAM = "state_closed_inputstream";
public static final String STATE_CLOSED_OUTPUTSTREAM = "state_closed_outputstream";
public ClientInfo(ClientCommunicator communicator) {
this.communicator = communicator;
_PORT = communicator.getSocket().getPort();
}
synchronized public void setStatus(String status) {
this.status = status;
notifyAll();
}
public int getPort() {
return _PORT;
}
synchronized public String getStatus() {
return status;
}
public ClientCommunicator getCommunicator() {
return communicator;
}
}
public class ClientCommunicator extends Thread {
private MessageQueue messageQueue;
private Sender sender;
private Socket socket;
public ClientCommunicator(Socket socket, MessageQueue messageQueue) {
System.out.println("Connected to new client at port " + socket.getPort() + ". Local port is " + socket.getLocalPort());
this.messageQueue = messageQueue;
this.socket = socket;
try {
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
sender = new Sender();
sender.start();
try {
while (true) {
String receivedMessage = in.readLine(); //GET / HTTP/1.1
if (receivedMessage != null)
messageQueue.addReceivedMessage(receivedMessage);
else
break;
}
} catch (SocketException e) {
// Typically this means that the socket has been closed
} catch (IOException e) {
System.err.println(e.getMessage());
}
System.out.println("ClientCommunicator thread ended.");
}
public void closeInputStream() {
if (!socket.isInputShutdown()) {
try {
socket.shutdownInput();
messageQueue.changeClientInfoStatus(socket.getPort(), ClientInfo.STATE_CLOSED_INPUTSTREAM);
} catch (IOException e) {
System.err.println("Failed to close socket input stream.");
e.printStackTrace();
}
}
}
public void closeOutputStream() {
if (!socket.isOutputShutdown()) {
try {
socket.shutdownOutput();
} catch (IOException e) {
System.err.println("Failed to close socket output stream.");
e.printStackTrace();
}
}
}
public Socket getSocket() {
return socket;
}
private class Sender extends Thread {
private int toSendMessageIndex;
@Override
public void run() {
while (true) {
try {
List<String> messages = messageQueue.retrieveToSendMessages(toSendMessageIndex);
if (messages != null) {
boolean exit = false;
for (String message : messages) {
out.println(message); //GET / HTTP/1.1
toSendMessageIndex++;
if (message.equals("CLOSE_SOCKET_INPUT")) {
closeOutputStream();
messageQueue.changeClientInfoStatus(socket.getPort(), ClientInfo.STATE_CLOSED_OUTPUTSTREAM);
exit = true;
break;
} else if (message.equals("GET / HTTP/1.1")) {
System.out.println("GET");
loadWP();
//loadWEBPage();
}
}
if (exit)
break;
} else {
System.err.println("Null messages!");
break;
}
} catch (InterruptedException e) {
break;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("ClientCommunicator.Sender thread stopped.");
}
}
}
public class ConnectionManager extends Thread {
private final int PORT = _PORT;
private MessageQueue messageQueue;
public ConnectionManager(MessageQueue messageQueue) {
this.messageQueue = messageQueue;
}
@Override
public void run() {
try {
serverSocket = new ServerSocket(PORT, 0, InetAddress.getByName("localhost"));
System.out.println("Server listening on port " + PORT);
while (true) {
socket = serverSocket.accept();
ClientCommunicator communicator = new ClientCommunicator(socket, messageQueue);
ClientInfo clientInfo = new ClientInfo(communicator);
communicator.start();
messageQueue.addClient(clientInfo);
}
} catch (SocketException e) {
// SocketException is thrown when serverSocket is closed. This
// is normal when we shut down the server.
} catch (IOException e) {
System.err.println(e.getMessage());
} finally {
System.out.println("ConnectionManager thread ended.");
}
}
public void stopListening() {
try {
messageQueue.closeClientCommunicatorInputStreams();
messageQueue.waitForAllClientInputStreamClose();
messageQueue.addToSendMessage("CLOSE_SOCKET_INPUT");
messageQueue.waitForAllClientOutputStreamClose();
System.out.println("Closing client sockets.");
for (ClientInfo info : messageQueue.getClientList()) {
Socket socket = info.getCommunicator().getSocket();
if (!socket.isClosed()) {
try {
socket.close();
} catch (IOException e) {
System.err.println("Failed to close client socket.");
}
} else {
System.err.println("Client socket is already closed.");
}
}
if (serverSocket != null) {
serverSocket.close();
System.out.println("Closed server socket.");
}
messageQueue.shutdown();
} catch (IOException e) {
System.err.println("Failed to close socket");
}
}
}
public class MessageRetriever extends Thread {
private MessageQueue messageQueue;
public MessageRetriever(MessageQueue messageQueue) {
this.messageQueue = messageQueue;
}
@Override
public void run() {
while (true) {
try {
String receivedMessage = messageQueue.pollReceivedMessage();
if (receivedMessage == null) {
System.err.println("Received null message. Terminating.");
break;
} else {
System.out.println(receivedMessage);
messageQueue.addToSendMessage(receivedMessage);
}
} catch (InterruptedException e) {
break;
}
}
System.out.println("MessageRetriever ended.");
}
}
public void loadWEBPage() {
final String newLine = "\r\n";
try {
while (true) {
try {
String request = in.readLine();
if (request == null)
continue;
while (true) {
String ignore = in.readLine();
if (ignore == null || ignore.length() == 0)
break;
}
if (!request.startsWith("GET ")
|| !(request.endsWith(" HTTP/1.0") || request.endsWith(" HTTP/1.1"))) {
out.print("HTTP/1.0 400 Bad Request" + newLine + newLine);
} else {
String response = "Hello, World!";
out.print("HTTP/1.0 200 OK" + newLine + "Content-Type: text/plain" + newLine + "Date: "
+ new Date() + newLine + "Content-length: " + response.length() + newLine + newLine
+ response);
}
out.close();
} catch (Throwable tri) {
System.err.println("Error handling request: " + tri);
}
}
} catch (Throwable tr) {
System.err.println("Could not start server: " + tr);
}
}
}
一旦我启动套接字服务器,我就会测试聊天,并且它再次运行得很好。然后我测试了网站 http:\localhost:6427,这是我执行此操作后从 Eclipse 控制台获得的输出:
multichat server
Server listening on port 6427
Connected to new client at port 58833. Local port is 6427
Upgrade-Insecure-Requests: 1
Accept-Language: en-US,en;q=0.9
Accept-Encoding: gzip, deflate, br
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/a png,/;q=0.8,application/signed-exchange;v=b3
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36 Connection: keep-alive Host: localhost:6427 GET / HTTP/1.1 Exception in thread "Thread-8" java.util.ConcurrentModificationException at java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1250) at java.util.ArrayList$SubList.listIterator(ArrayList.java:1110) at java.util.AbstractList.listIterator(AbstractList.java:310) at java.util.ArrayList$SubList.iterator(ArrayList.java:1106) at MainServer$ClientCommunicator$Sender.run(MainServer.java:292)
Connected to new client at port 58834. Local port is 6427
GET
ClientCommunicator thread ended.
我曾经让它工作过,然后我尝试了其他东西,一旦不起作用,我就试图记住我对其他脚本做了什么,可惜我忘记了......
有人愿意指出我缺少什么才能使其再次正常工作吗?
最佳答案
在代码中,您迭代消息列表,但当发生这种情况时,其他元素会向列表中添加另一个元素或删除一个元素,因此您会收到 ConcurrentModificationException。
例如:这是行不通的:
public static void main(String[] args)
{
List<Object> objectList = new ArrayList(Arrays.asList(new Object[] { "1", "2", "3"}));
int index = 0;
for (Object object : objectList)
{
System.out.println(object.toString());
objectList.remove(index);
index++;
}
}
但是如果你使用迭代器,这可以工作:
public static void main(String[] args)
{
List<Object> objectList = new ArrayList(Arrays.asList(new Object[] { "1", "2", "3"}));
Iterator<Object> iterator = objectList.iterator();
while (iterator.hasNext())
{
Object object = iterator.next();
System.out.println(object.toString());
iterator.remove();
}
}
关于Java ServerSocket/socket 与默认网页进行多聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57712084/
在服务器端,我使用这段代码: ServerSocket server = new ServerSocket(1234); Socket server_socket = server.accept();
假设A是服务器,B是客户端。 B 创建一个到服务器 A 的套接字 b 并 ServerSocket.accept() 创建一个相应的套接字 a 对于客户端 B 现在,如果服务器 A 关闭了 a 但客户
我想在单独的线程中运行Python serversocket 类。为此,我修改了此代码以支持 python 线程: 原始文件 import SocketServer class MyTCPHandle
我正在使用一些 java 创建一个小客户端来监控网络。对于网络上的每个节点,我运行一个客户端,但也运行一个 ServerSocket 来监听传入的连接。我写的代码似乎卡在了 ServerSocket.
虽然 ServerSocket 类被设计为一般监听传入连接,但我可以只使用 Socket 对象来代替并在 java 中检索传入数据吗?如果是,那么我是否需要采取任何额外的预防措施? 编辑:感谢您的回答
我在Java项目的类之一中有一个ServerSocket。该套接字接收一些导致新操作的命令。例如: deleteAllFromDB ->删除数据库上的所有条目。 sendMsgToX ->创建发送到X
我用java写了一个服务器,这里是代码: public mainClass() { try { ss = new ServerSocket(8080);
我目前正在参与学校的一个项目,我们正在构建一个在 Android 手机上使用的通信系统。为此,我们将使用一个向所有客户端打开套接字的服务器,使它们进行通信。 我以前做过几个聊天应用程序,没有任何套接字
我有这两个类 Client.java 和 Server.java,每个类都包含一个 main 方法。想法非常简单,其中的代码也非常相似。我试图实现的目标是打开一个使用 ServerSocket 监听特
这可能是一个愚蠢的问题,但就是这样。我正在编写这个聊天程序,其中有一个服务器和可以连接到它的客户端。我想在程序中实现私有(private)消息传递,但我不知道如何让客户端直接相互连接。对于服务器,我使
ServerSocket 与多个客户端一起使用,我们可能为每个客户端附加单独的线程来工作,但问题是连接工作正常并接受所有客户端,但仅服务最后一个连接。所以这不是问题,或者这是正常现象。 服务器代码:
我是套接字编程的新手,我只是尝试一些东西。我想做的是让客户端读取文本文件,将该文件中的行保存在 ArrayList 中,然后将这些行发送到服务器。这是我的代码。连接成功建立,但是当服务器尝试从他的Bu
我有以下代码片段: if (servsock == null) { System.out.println("HELLO1!"); servsock = new ServerSocket
我正在查看serversocket API,它说允许的最大连接数是50。我尝试测试它.. public class ClientSocketTest { public static void
我试图制作一个小聊天程序,但它真的不想工作。我的服务器和客户端基本相同:它们创建一个新的套接字和一个新的聊天(GUI)。有人可以帮我找出我犯的错误吗? 服务器代码: import java.io.In
我尝试使用蓝牙将图像从一个设备发送到其他设备。为此,我采用 Android 蓝牙聊天应用程序源代码,当我发送字符串时它工作正常。但是如果我将图像作为字节数组发送,则 while 循环不会中断或从输入流
我目前正在为我的大学开发一款 TicTacToe 多线程网络游戏,但遇到了一个恼人的问题。 我希望我的服务器每次接受 2 个连接,然后等待其他 2 个连接等。 问题是,在我的代码中,如果客户端连接到服
我正在构建一个简单的服务器应用程序,我可以在本地网络中连接到它。但我无法通过互联网连接。 这是我的服务器代码: ServerSocket server; try { server = new
我有以下构造: try { serverSocket = new ServerSocket(port); } catch (Exception e) { throw new Runti
我一直在尝试在我创建的应用程序中开发一个嵌入式网络服务器。本质上,我们有创建一个简单网络服务器(使用 ServerSocket)的主进程,然后主进程(理论上)将处理它的业务。 Main(){
我是一名优秀的程序员,十分优秀!