插座我。 ServerSockets 服务器/客户 GUI 聊天计划

我正在努力学习套接字和 ServerSockets 工作 Java, 因此,我试图制作聊天服务器,但同时使用流处理来处理每个客户端。 我想我需要一个清新的代码,因为我不知道它为什么不起作用。 程序已启动,但客户端不会连接并未创建。 我很确定在我的班级客户中有问题,但我不确定如何解决它。 任何一般的帮助,甚至只是对一个有用资源的引用,都会非常感激。 谢。

服务器代码


package chatbox.server;

import static java.lang.System.out;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;


public class Server {


public final static int DEFAULT_PORT = 5000;

private ServerSocket socket;
private ArrayList<socket> clients;


public Server/int port/ throws IOException {
System.out.println/"Server is now online"/;
System.out.println/"port: " + port/;
this.socket = new ServerSocket/port/;
System.out.println/"Listening socket established"/;
System.out.println/"Waiting for connections..."/;
this.clients = new ArrayList<socket>//;


while /true/ {
try {
final Socket connection = this.socket.accept//;
this.clients.add/connection/;


Runnable incomingMsg = new Runnable// {
private InputStream inputStream = connection.getInputStream//;
private InputStreamReader reader = new InputStreamReader/
inputStream/;
private Scanner scanner = new Scanner/reader/;

@Override
public void run// {
while /true/ {
if /scanner.hasNextLine/// {
String msg = scanner.nextLine//;
System.out.println/"Handling message: \"" + msg
+ "\""/;
notifyAllConnections/msg/;
}
}
}

};

Thread thread = new Thread/incomingMsg/;


thread.setUncaughtExceptionHandler/new Thread.UncaughtExceptionHandler// {

@Override
public void uncaughtException/Thread thread, Throwable exc/ {
try {
connection.close//;
} catch /IOException e1/ {

e1.printStackTrace//;
} finally {
clients.remove/connection/;
System.out.println/"Removed connection"/;
}

}
}/;
thread.start//;
System.out.println/"Added new connection"/;
} catch /IOException exc/ {

System.out
.println/"Error occurred."/;
}
}
}

protected void notifyAllConnections/String msg/ {
for /Socket sock : this.clents/ {
try {
OutputStream out = sock.getOutputStream//;
PrintStream printer = new PrintStream/out/;
printer.println/msg/;
printer.flush//;
} catch /IOException exc/ {
System.out.println/"Message was not fully broadcast"/;
}

}
}

public static void main/String[] args/ {
try {
Server server = new Server/
Server.DEFAULT_PORT/;
} catch /IOException exc/ {
System.out
.println/"Could not create the server socket."/;
exc.printStackTrace//;
String servername = "localhost";
try {
new Client/servername, 5000/;
} catch /Exception ex/ {
out.println/"Error" + ex.getMessage///;

}
}
}
}


客户端代码


package chatbox.client

import java.awt.event.ActionListener;

import javax.swing.JFrame;
import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static java.lang.System.out;

public class Client extends JFrame {

private PrintWriter pw;
private Scanner scanner;
private JPanel chatAndSend;
private JTextArea chatWindow;
private JScrollPane mainScroll;
private JTextArea chatText;
private JScrollPane miniScroll;
private JButton send;
private Socket client;

public Client/String servername, int port/ throws Exception {

this.client = new Socket/servername, port/;
this.scanner = new Scanner/new InputStreamReader/
this.client.getInputStream////;
this.pw = new PrintWriter/this.client.getOutputStream///;

makeGUI//;
new MessagesThread//.start//;

}

public void makeGUI// {
setDefaultCloseOperation/JFrame.EXIT_ON_CLOSE/;
getContentPane//.setLayout/new BorderLayout///;
this.chatWindow = new JTextArea/10, 20/;
this.chatWindow.setEditable/false/;
this.chatWindow.setLineWrap/true/;
this.mainScroll = new JScrollPane/chatWindow,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER/;
this.add/this.mainScroll, BorderLayout.NORTH/;
this.chatAndSend = new JPanel//;
this.chatAndSend.setLayout/new FlowLayout///;
this.chatText = new JTextArea/1, 1/;
this.chatText.setLineWrap/true/;
this.miniScroll = new JScrollPane/chatText,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER/;
this.chatAndSend.add/this.miniScroll/;
this.send = new JButton//;
this.send.setText/"SEND"/;
this.send.addActionListener/new ActionListener// {
public void actionPerformed/ActionEvent event/ {
pw.println/chatText.getText///;
pw.flush//;
}
}/;
this.chatAndSend.add/this.send/;
this.add/this.chatAndSend, BorderLayout.SOUTH/;
this.setVisible/true/;
this.pack//;
}

class MessagesThread extends Thread {
public void run// {
String line;
try {
while /true/ {
line = scanner.nextLine//;
chatWindow.append/line + "\n"/;
}
} catch /Exception exception/ {
System.out.println/exception.getMessage///;
}
}
}
}


</socket></socket>
已邀请:

冰洋

赞同来自:

在课堂里 Client MessagesThread, 在课堂里 while loop 他肯定是
while /scanner.hasNextLine/// {


在你添加的同一个课程中 JTextArea 从背景流中,你不应该做什么。

在客户类中 MessagesThread, 销售量 Runnable, 不要延长线程 /一般性建议/

甚至更好,使用 SwingWorker, 所以你可以使用 publish/process 更新 JTextArea 在事件中 Swing.

为您的钱,我首先将在除此之外的版本中启动一个程序 GUI, 在试图放入之前 GUI.

您的拼写错误表明代码甚至不必编译 clientss 和 clents,... ??? 是你的

当下

代码? 如果我们无法复制 / 插入您的代码并测试它,我们很难帮助您。


编辑

我环顾四周并推出了你的新代码。 您是否看到您正在尝试在块中创建新客户端 catch, 哪个从未被称为?

最重要的是,当您启动代码时,请使用调试器查看您的代码是否已使用。 还添加运营商 println /他们更多/. 如果你这样做,你会看到客户设计师永远不会被称为,他们会知道要查看你试图称之为理解原因的代码。

风见雨下

赞同来自:

我不确定这是你的问题之一,但我不知道是否安全地制作循环 foreach 在服务器类中,因为您可以从一个流中指定并在另一个流中向列表中添加到列表中,同时它可能是循环的问题 foreach. 如果我错了,请纠正我。 /或者只是从中删除 foreach 会伤害他吗?/

要回复问题请先登录注册