- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个基本的聊天应用程序。有 3 个类:服务器、客户端和 clientHandler。问题是服务器只能第一次读取(使用 DataInputStream)然后抛出异常。
Hello. I have a basic chat app. There are 3 classes: server, client and clientHandler. The problem is that server is able to read (using DataInputStream) only first time then it throws exceptions.
客户端.java:
package Client;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class Client extends JFrame{
private JPanel contentPane;
private JTextField tf_input;
private static JTextArea ta_chat;
private JButton b_connect;
private static int portNumber = 2309;
private static String ip = null;
private JScrollPane scrollPane;
private JTextField tf_ip;
private Socket s = null;
private DataOutputStream dos = null;
private DataInputStream dis = null;
private JTextField tf_port;
private boolean connected = false;
private JLabel lblIp;
private JLabel lblPort;
private JLabel lblName;
private JTextField tf_name;
private JButton btnDc;
/**
* Launch the application.
*/
public static void main(String[] args) {
Client frame = new Client();
frame.setVisible(true);
System.out.println("Client is running!");
// TODO Auto-generated method stub
//Listening to server
while(!frame.connected)
{
System.out.println("Waiting...!");
}
while(true)
{
System.out.println("Listening started!");
String line = null;
try {
line = frame.dis.readUTF();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ta_chat.append("reading from server failed\n");
//System.out.println(Thread.currentThread().getName() + " failed listening to server!\n");
}
if(line!=null)
ta_chat.append(line.trim() + "\n");
}
}
//
/**
* Create the frame.
*/
public Client() {
setTitle("Client");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 530, 458);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton b_send = new JButton("Send");
b_send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(!connected)
{
JOptionPane.showMessageDialog(null, "You have to connect first!");
return;
}
String message = tf_input.getText();
try {
ta_chat.append("writing UTF" + '\n');
dos.writeUTF(message);
ta_chat.append(ta_chat.getText().trim() + "writing UTF succeded" + '\n');
} catch (IOException e) {
// TODO Auto-generated catch block
ta_chat.append(ta_chat.getText().trim() + "writing UTF failed" + '\n');
}
tf_input.setText(null);
}
});
b_send.setBounds(375, 204, 89, 23);
contentPane.add(b_send);
tf_input = new JTextField();
tf_input.setBounds(89, 205, 258, 20);
contentPane.add(tf_input);
tf_input.setColumns(10);
scrollPane = new JScrollPane();
scrollPane.setBounds(39, 11, 452, 175);
contentPane.add(scrollPane);
ta_chat = new JTextArea();
scrollPane.setViewportView(ta_chat);
ta_chat.setEditable(false);
b_connect = new JButton("Connect");
b_connect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
connected = true;
String ip = tf_ip.getText();
String name = tf_name.getText();
int portNumber = Integer.parseInt(tf_port.getText());
try {
s = new Socket(ip, portNumber);
ta_chat.append("s=" + s + "\n");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
dis = new DataInputStream(s.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ta_chat.append("dis init failed\n");
}
try {
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("DataOutputStraem object init failed!");
ta_chat.append("dos init failed\n");
}
try
{
dos.writeUTF("/name " + name);
}
catch(Exception e)
{
ta_chat.append("dos.writeUTF() failed \n");
}
}
});
b_connect.setBounds(89, 373, 89, 23);
contentPane.add(b_connect);
tf_ip = new JTextField();
tf_ip.setBounds(83, 269, 123, 20);
contentPane.add(tf_ip);
tf_ip.setColumns(10);
tf_port = new JTextField();
tf_port.setColumns(10);
tf_port.setBounds(83, 300, 123, 20);
contentPane.add(tf_port);
lblIp = new JLabel("IP:");
lblIp.setBounds(39, 272, 46, 14);
contentPane.add(lblIp);
lblPort = new JLabel("Port");
lblPort.setBounds(39, 303, 46, 14);
contentPane.add(lblPort);
lblName = new JLabel("Name:");
lblName.setBounds(39, 335, 46, 14);
contentPane.add(lblName);
tf_name = new JTextField();
tf_name.setColumns(10);
tf_name.setBounds(83, 332, 123, 20);
contentPane.add(tf_name);
btnDc = new JButton("dc");
btnDc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnDc.setBounds(290, 373, 89, 23);
contentPane.add(btnDc);
}
}
服务器.java:
package Server;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Server extends JFrame{
private JPanel contentPane;
private JTextField tf_input;
private static int portNumber = 2309;
private static JTextArea ta_ServerLog;
private JTextField tf_IP;
private JButton btnRefresh;
private JTextField textField;
private JLabel lblPort;
private static ServerSocket ss = null;
/**
* Launch the application.
*/
public static void addTextToServerLog(String text)
{
if(text.equals(null))
return;
else
ta_ServerLog.setText(ta_ServerLog.getText().trim() + "\n" + text.trim());
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Server frame = new Server();
frame.setVisible(true);
} catch (Exception e) {
}
}
});
try {
ss = new ServerSocket(portNumber);
} catch (IOException e1) {
// TODO Auto-generated catch block
}
while(true)
{
Socket s = null;
try {
s = ss.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
}
System.out.println("Creating new ClientListener!");
new ClientHandler(s).start();
}
}
/**
* Create the frame.
*/
public Server() {
setTitle("Server");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 377);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
tf_input = new JTextField();
tf_input.setBounds(125, 209, 109, 20);
contentPane.add(tf_input);
tf_input.setColumns(10);
JButton b_send = new JButton("Send");
b_send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
b_send.setBounds(278, 209, 86, 20);
contentPane.add(b_send);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(49, 26, 294, 136);
contentPane.add(scrollPane);
ta_ServerLog = new JTextArea();
ta_ServerLog.setEditable(false);
scrollPane.setViewportView(ta_ServerLog);
JLabel lblYouAreHosting = new JLabel("Your IP:");
lblYouAreHosting.setBounds(49, 277, 44, 14);
contentPane.add(lblYouAreHosting);
tf_IP = new JTextField();
tf_IP.setEditable(false);
tf_IP.setBounds(96, 274, 103, 20);
contentPane.add(tf_IP);
tf_IP.setColumns(10);
btnRefresh = new JButton("Refresh IP");
btnRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tf_IP.setText(getIP());
}
});
btnRefresh.setBounds(212, 273, 89, 23);
contentPane.add(btnRefresh);
textField = new JTextField();
textField.setText(Integer.toString(portNumber));
textField.setColumns(10);
textField.setBounds(96, 305, 103, 20);
contentPane.add(textField);
lblPort = new JLabel("Port:");
lblPort.setBounds(49, 308, 44, 14);
contentPane.add(lblPort);
JButton btnNewButton = new JButton("dc");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ss = null;
}
});
btnNewButton.setBounds(10, 185, 51, 23);
contentPane.add(btnNewButton);
}
private String getIP()
{
URL site = null;
try {
site = new URL("http://checkip.amazonaws.com");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(site.openStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
return InetAddress.getLocalHost().getHostAddress().toString();
//return reader.readLine();
}
catch(Exception e)
{
}
return null;
}
}
ClientHandler.java:
package Server;
import java.awt.HeadlessException;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class ClientHandler extends Thread{
private Socket clientSocket = null;
private DataInputStream dis = null;
private static ArrayList<Socket> users = null;
private String getIP()
{
URL website = null;
try {
website = new URL("http://checkip.amazonaws.com");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(website.openStream()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
return reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public ClientHandler(Socket clientSocket)
{
this.clientSocket = clientSocket;
if(users==null)
users = new ArrayList<Socket>();
try
{
users.add(clientSocket);
}
catch(Exception ex)
{
System.out.println("Adding client's socket to socket list failed!");
}
try
{
dis = new DataInputStream(clientSocket.getInputStream());
}
catch(Exception e)
{
}
Server.addTextToServerLog("Trying to add a new client!");
/*
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
Server.addTextToServerLog("Client added!");
}
public void tellEveryone(String message, String name)
{
for (Socket socket : users) {
Socket s = socket;
try {
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(name + ":" + message);
dos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public void disconnect()
{
}
// reciving messages from a specific client and send to everyone else
public void run()
{
while(true)
{
String line = null;
try {
System.out.println("Reading client input");
Server.addTextToServerLog("-line-");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
line = dis.readUTF();
Server.addTextToServerLog("Line=" + line);
if(line!=null)
{
if(line.startsWith("/name "))
{
String name = line.replaceFirst("/name", "");
tellEveryone("joined room!", name);
Server.addTextToServerLog(name + "joined room!");
}
else
{
tellEveryone(line, null);
Server.addTextToServerLog(line);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
}
第二次尝试抛出异常:(Client.java - 当按下发送按钮时执行)
b_send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(!connected)
{
JOptionPane.showMessageDialog(null, "You have to connect first!");
return;
}
String message = tf_input.getText();
try {
ta_chat.append("writing UTF" + '\n');
dos.writeUTF(message);
ta_chat.append(ta_chat.getText().trim() + "writing UTF succeded" + '\n');
} catch (IOException e) {
// TODO Auto-generated catch block
ta_chat.append(ta_chat.getText().trim() + "writing UTF failed" + '\n');
}
tf_input.setText(null);
}
});
Server.java:(这部分可能是相关的)
while(true)
{
Socket s = null;
try {
s = ss.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
}
System.out.println("Creating new ClientListener!");
new ClientHandler(s).start();
}
ClientHandler.run():
public void run()
{
while(true)
{
String line = null;
try {
System.out.println("Reading client input");
Server.addTextToServerLog("-line-");
try {
//Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
line = dis.readUTF(); // <----------- this works only 1st time
Server.addTextToServerLog("Line=" + line);
if(line!=null)
{
if(line.startsWith("/name "))
{
String name = line.replaceFirst("/name", "");
tellEveryone("joined room!", name);
Server.addTextToServerLog(name + "joined room!");
}
else
{
tellEveryone(line, null);
Server.addTextToServerLog(line);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
我不得不说这个应用程序不久前就可以运行了,它有并发问题,但它运行得足够好。我没有后盾。
最佳答案
至少部分问题出在您客户端的 tellEveryone(2)
方法中:
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(name + ":" + message);
dos.close();
每次发送消息时,您都会关闭流。由于 dos
是使用客户端 Socket
实例的输出流创建的,因此每次调用 close()
方法时,它都会关闭套接字dos
。您可能应该将 dos
存储为客户端类的成员变量,并且仅在您真正想要断开连接时才将其关闭。
此外,您可能应该在每次发送消息时调用dos.flush()
,以确保将整个消息写入套接字。有时,OutputStream 实现会缓存数据,直到达到一定的缓冲区大小,然后将整个缓冲区刷新到底层流(在这种情况下,底层流是套接字本身)。如果您不调用flush()
,则由于缓冲,某些消息可能不会写入套接字。
确保在整个代码中,除非您确实想要断开连接,否则不会关闭任何打开的套接字,这可能会解决您的问题。
关于Java DataOutputStream 只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40160873/
代码在Java Tutorials显示了使用 DataOutputStream 类和 DataInputStream 类的示例。 代码片段如下所示: //.. out = new DataOutput
我怎样才能做类似ByteBuffer.putBoolean和ByteBuffer.getBoolean的事情? 与DataInputStream一样,有readBoolean,而DataOutputS
这个问题在这里已经有了答案: Java Socket why server can not reply client (4 个回答) 2年前关闭。 我有一个使用套接字与另一台计算机通信的 java 应
我在应用程序中使用此代码来发送一些字符串并抛出套接字。 public class OutgoingData { public static DataOutputStream dos = null; p
当我在没有 close 方法的情况下运行此代码时,服务器无法接收消息! 客户: Socket con = new Socket(InetAddress.getByName("local
DataOutputStream salida; salida = new DataOutputStream(socket.getOutputStream()); Error:(35, 24) ja
在下面的代码中。我正在将文件读入一个小缓冲区(len = CHUNK_SIZE),我只想将此缓冲区写入输出流。但即使我在每个 block 之后刷新,我也会遇到堆溢出异常。好吧,如果我想流式传输小文件,
我尝试使用 DataOutputStream 发送 POST 数据并获取响应数据。 我是这样编码的。 String urlParameters = "table=page&format=xml
所以我尝试将我的代码与 DataInputStream 和 BufferedReader 一起使用将字符串从客户端发送到服务器,并在整个互联网上查找资源来解决此问题。我正在使用一种从客户端读取字符串的
我正在开发服务器/客户端通信程序,但遇到了一个问题。当我尝试从客户端发送消息时,它无法正常工作。初始化服务器后,我连接客户端并且成功。当我尝试从客户端发送消息时,服务器不会收到它们。关闭客户端连接后,
DataOutputStream 类用于将原始数据类型写入二进制格式。我一直在使用DataOutputStream类的方法void writeChars(String s),使用程序将其写入文件中..
我正在尝试使用 Java 创建一个客户端/服务器程序,其中客户端将简单的字符串发送到服务器,并且连接到服务器的每个客户端都会获取此字符串。因此,我为服务器创建了 2 个服务器类,它们为每个客户端创建新
对于简单的 2D 游戏,我需要我的客户端程序始终将坐标发送到服务器。 因此,我创建了一个测试,看看是否可以通过从服务器发送和检索值来使两个玩家具有相同的速度。 package main; import
我在 Android 中的 DataOutputStream 上收到了一个 NullPointerException。 我首先编写了一些代码,使用 DataOutputStream 在 Java 中写
我正在为包装 TCP 套接字的类编写单元测试(使用 Mockito 作为我的模拟库)。问题是任何在 DataOutputStream 上调用 writeBytes 的测试都会抛出空指针异常。 我在下面
我有一个基本的聊天应用程序。有 3 个类:服务器、客户端和 clientHandler。问题是服务器只能第一次读取(使用 DataInputStream)然后抛出异常。 Hello. I have a
我正在编写一个客户端服务器应用程序,但是当它们从服务器端发送时,我在客户端没有收到相同的字节。在服务器端,我使用了 .write(bytes[]) 方法。在客户端,我使用了 .readFully(by
我正在为 HTTP post 请求将单个字节写入 java 中的 DataOutputStream。帖子的结构如下: /* Init Post */ URL PostToLink = new URL(
我正在尝试将一个大字符串写入我的 DataOutputStream,但是我收到了一个 UTFDataFormatException,上面写着: 长度超过 65535 个 UTF 字节的字符串 当我打电
我制作了一个简单的服务器应用程序,用于将图像发送到连接的客户端。我在主线程中有一个循环,它接受传入的连接,并从相应的套接字创建一个 Client 对象。我有一份活跃客户名单。每次有新图片可用时,我都会
我是一名优秀的程序员,十分优秀!