我应该打开 - 每次我的客户端GUI发送消息或打开连接一次并保持打开时关闭连接?
我使用BufferedWriter发送数据但这个methid似乎只工作了一次,在执行write和flush方法之后我只能向我的服务器发送1条消息,然后它只是没有得到数据即时发送..
客户端连接 - 将数据发送到服务器(第1次) - 工作 客户端再次发送数据 - 没有数据在服务器控制台上回显。
每次我想发送数据或者一直打开数据并且从不关闭它时,我应该打开一个缓冲区吗?
我的实际代码 - 客户端
主要
public class Main {
public static void main(String args[]) {
Client chat = new Client("localhost", 6111);
}
}
客户端
public class Client {
public Client(String host, int port) {
SwingUtilities.invokeLater(new Runnable() {
private Socket chat;
public void run() {
try {
this.chat = new Socket("localhost", 6113);
} catch ( IOException e) {
e.printStackTrace();
}
Gui gui = new Gui("Chat", this.chat);
}
});
}
}
GUI
public class Gui {
private JFrame frame;
private JTextArea area;
private JTextField field;
private JMenuBar menu;
private JButton button;
private Socket socket;
private BufferedWriter write;
public Gui(String title, Socket socket) {
this.socket = socket;
try {
this.write = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
}
this.frame = new JFrame(title);
this.frame.setSize(new Dimension(400, 400));
this.frame.setLayout(new BorderLayout());
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setResizable(false);
Elements interfce = new Elements(this.frame);
this.field = interfce.addField("Texto to send...");
this.menu = interfce.addBar();
this.area = interfce.addArea();
this.button = interfce.addButton("Send");
this.button.addActionListener(new Listener(this.field, this.socket, this.write));
this.menu.add(this.field);
this.menu.add(this.button);
this.frame.setVisible(true);
}
}
监听器(发送数据)
public class Listener implements ActionListener {
private JTextField text;
private Socket chat;
private BufferedWriter writer;
public Listener(JTextField field, Socket chat, BufferedWriter write) {
this.text = field;
this.chat = chat;
this.writer = write;
}
public void actionPerformed(ActionEvent e) {
System.out.println(this.text.getText());
try {
writer.write("Hello\n");
writer.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
服务器
public class Main {
public static void main(String args[]) {
Server chat = new Server(6113);
}
}
public class Server {
private int port;
private ServerSocket server;
public Server(int port) {
this.port = port;
startServer();
}
public void startServer() {
try {
this.server = new ServerSocket(this.port);
System.out.println("Starting chat server at port " + this.port + "...");
while (true) {
Socket s = this.server.accept();
System.out.println("Client connected - " + s.getLocalAddress().getHostName());
Thread client = new Thread(new Client(s));
client.start();
}
} catch (IOException e) {
System.out.println("Error found!");
e.printStackTrace();
}
}
}
public class Client implements Runnable {
private Socket client;
public Client(Socket client) {
this.client = client;
}
public void run() {
try {
BufferedReader read = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
System.out.println("Client says - " + read.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
我应该打开 - 每次我的客户端GUI发送消息或打开连接一次并保持打开时关闭连接?
客户端和服务器之间的连接通常对整个消息传递会话开放,而不是为了发送每条消息而重新创建。所以保持打开直到客户端或服务器必须离开。
在接受连接时,查看服务器中Client
个start
个帖子。
public void run() {
try {
BufferedReader read = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
System.out.println("Client says - " + read.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
这会给你一个提示吗?好吧 - 你必须在run()
内安排某种循环 - 听取来自该客户的更多意见。
答案 1 :(得分:0)
只是做一些改变
服务器端:
public void run() {
try {
BufferedReader read = new BufferedReader(new InputStreamReader(
this.client.getInputStream()));
while (true) {
System.out.println("Client says - " + read.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
客户方:
使用提供打印新行功能和自动刷新属性的PrintWriter
代替BufferedWriter
。
this.write = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream()),
true);
....
public void actionPerformed(ActionEvent e) {
System.out.println(this.text.getText());
try {
writer.write(this.text.getText());
writer.println();
} catch (Exception e1) {
e1.printStackTrace();
}
}