gpt4 book ai didi

java - 为什么服务器不写给客户端?

转载 作者:行者123 更新时间:2023-12-02 03:38:45 24 4
gpt4 key购买 nike

这是我的服务器类,它打开一个服务器套接字以从一个客户端获取信息,然后使用用户类将字符串发送给数组中的所有用户:

public class Server  {
static ServerSocket server;
static Socket client;
static DataOutputStream out;
static DataInputStream in;
static int port = 7767;
static Users[]User = new Users[10];

public static void main(String[] args)throws Exception{
try {
System.out.print("starting");
server = new ServerSocket(port);
while((client = server.accept() )!= null ){

for(int i=0; i<10; i++){
// System.out.println("input connection");
out= new DataOutputStream(client.getOutputStream());
in = new DataInputStream(client.getInputStream());
if(User[i] == null)
{
User[i] = new Users(out, in, User);
Thread thread = new Thread(User[i]);
thread.start();

}
}
}

} catch (IOException e) {

e.printStackTrace();


}

这个Users类只保存谁是客户端的信息,然后向所有用户发送服务器发送的字符串,我曾经得到message = in.readUTF();的NullPointException但现在,什么也没有发生。

public class Users implements Runnable {
DataOutputStream out;
DataInputStream in;
Users[] User = new Users[10];
public Users(DataOutputStream out, DataInputStream in, Users[] User)
{
this.in = in;
this.out = out;
this.User = User;

}
public void run(){
while(true){
try{
BufferedReader bri = new BufferedReader(new InputStreamReader(in));

String message;
message = bri.readLine();

for(int i=0; i<10; i++){
if(User[i] != null)
{
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(out));
User[i].br.write(message + "\n");
}

}

}
catch(IOException e){
// catching and doing something about it and stuff
}

}
}`

客户:(减去所有 Swing 的东西以节省问题空间)

public class Client implements WriteGui {
static Socket client;
static DataInputStream in;
static DataOutputStream out;
JTextArea msgout;
private JFrame frame;
private JTextField msgA;
private JTextField nameA;



/**
* Create the application.
*/
public Client() {
initialize();
CStart();
}`

public void actionPerformed(ActionEvent arg0) {
if(nameA.getText() == ""){
nameA.setText(getname());


}
String message;

message = (nameA.getText()+": "+ msgA.getText());

try {
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(out));
br.write(message+"\n");


} catch (IOException e) {
msgout.append("error!");

}


}
public void write(String s) {
msgout.append(s+ "/n" );

}
private String getname(){
return JOptionPane.showInputDialog(frame,"fill out name" , "name", JOptionPane.QUESTION_MESSAGE);



}
public void CStart(){
int port = 7767;
String host = "localhost";
try {

client = new Socket(host, port);
msgout.append("starting");
in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
Input input = new Input(in, this);
Thread thread = new Thread(input);
thread.start();

}

catch(Exception e) {
msgout.append("error");
}

我还有一个简短的输入类,用于将消息输入客户端:

public class Input implements Runnable  {
WriteGui gui;
DataInputStream in;
static BufferedReader br;
public Input(DataInputStream in, WriteGui gui){
this.in = in;
this.gui = gui;

}

public void run() {
String message;
br = new BufferedReader(new InputStreamReader(in));
try {
message = br.readLine();
gui.write(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

就像我说的,我将错误追溯到 Users 类,但 NullPointerException 不能保证每次都会发生。它似乎仍然在服务器/用户代码中,因为它应该使用更多资源。谁能帮我弄清楚为什么这个 Datainputstream 似乎不起作用?

编辑:它停止产生 NullPointerException,现在在多次尝试后按开始而不是消息后,它发送一个盒子形状。所以看来确实是UTF读取的问题。即使将 OutputStreamWriter 更改为 DataInputStream,它也不起作用。

最佳答案

为了使 readUTF() 方法正常工作,数据必须以特定方式格式化,而使用 OutputStreamWriter 编写数据时情况并非如此。

我建议您使用 BufferedReaderBufferedWriter,使用 write(string+"\n") 写入内容,然后 readLine() 来读取。为了使其工作,您必须在每条消息的末尾添加一个新行 \n

这是一个例子:

try(BufferedWriter bw = new BufferedWritter(new OutputStreamWriter(socker.getOutputStream()))){
bw.write(message + "\n");
}catch(IOException e){
e.printStackTrace();
}


try(BufferedReader br = new BufferedReader(new InputStreamReader(socker.getInputStream()))){
String message = br.readLine();
}catch(IOException e){
e.printStackTrace();
}

关于java - 为什么服务器不写给客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37107580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com