java 为什么服务器不接收来自客户端的消息?

92dk7w1h  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(99)

我试图实现使用套接字对电影进行评级的代码,但服务器和客户端并不执行我的每个请求。这样的执行方式好吗?在一瞬间,一切似乎都被封锁了。当我试着写一部电影时,do- while循环永远不会停止。这是另一个问题。代码在下面。

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map.Entry;

public class Server{
public static int test_port=12345;
private int port;
BufferedReader br;
PrintWriter toUser;
BufferedReader fromUser;
public static HashMap\<String,Double\> listOfMovies=new HashMap\<\>();
public static HashMap\<String,Integer\> counter=new HashMap\<\>();
public static void main(String\[\] args) {
// TODO Auto-generated method stub
Server server=new Server(Server.test_port);
server.execute();

    }

private void execute() {

        // TODO Auto-generated method stub
        try(ServerSocket s=new ServerSocket(port)){
            br=new BufferedReader(new InputStreamReader(new FileInputStream("Movies.txt")));
            
        
            for(Entry<String,Double> m: listOfMovies.entrySet()) {
                System.out.println(m.getKey()+" "+m.getValue());
            }
            while(true) {
                Socket client=s.accept();
                fromUser=new BufferedReader(new InputStreamReader(client.getInputStream()));
                toUser=new PrintWriter(client.getOutputStream(),true);
                System.out.println("Client connected!");
                toUser.println("Welcome!");
                toUser.println("Write movie that you want to rate:");
                String line;
                while((line=br.readLine())!=null) {
                    String movie=line;
                    listOfMovies.put(movie,0.0);
                    counter.put(movie, 0);
                    toUser.println(line);                   
                }
                toUser.println("Pick a movie:");
                
                System.out.println(fromUser.readLine());
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

public Server(int port) {
this.port=port;
}
}
package movies;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Scanner;

public class Client {
private String hostname;
private int port;
BufferedReader fromServer;
PrintWriter toServer;
ArrayList\<String\> list=new ArrayList\<String\>();
public static void main(String\[\] args) {
// TODO Auto-generated method stub
Client client=new Client("localhost",Server.test_port);
client.execute();
}
private void execute() {
// TODO Auto-generated method stub
try(Socket client=new Socket(this.hostname,this.port)){
System.out.println("Client connected to server: "+this.hostname);
fromServer=new BufferedReader(new InputStreamReader(client.getInputStream()));
toServer=new PrintWriter(client.getOutputStream(),true);
while(true) {
String line=fromServer.readLine();
while(line!=null) {
System.out.println(line);
list.add(line);
line=fromServer.readLine(); 
}
//fromServer.close();
//toServer.close();
toServer.println("ll");
System.out.println(list.contains("Kika"));
Scanner sc=new Scanner(System.in);
String movie=" ";
while(!list.contains(movie)) {
movie=sc.nextLine();
}
toServer.println(movie);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Client(String hostname,int port) {
this.hostname=hostname;
this.port=port;
}
}

我不明白为什么这个代码不好。比如这两条线

toServer.println("ll");
System.out.println(list.contains("Kika"));

什么都不要做。

dauxcl2d

dauxcl2d1#

您的应用程序协议设计已损坏。您正在以“线路模式”操作。对于一个新的连接,服务器只写2行,然后期望接收行。

toUser.println("Welcome!");
 toUser.println("Write movie that you want to rate:");
 String line;
 while((line=br.readLine())!=null) ...

同时,您的客户端代码期望永远接收不确定数量的行。

String line=fromServer.readLine();
 while(line!=null) {
     System.out.println(line);
     list.add(line);
     line=fromServer.readLine(); 
 }

只有“文件结束”(连接关闭)才能让你摆脱这个循环。
要修复,您有两个选项:
1.让客户端代码“知道”(即,做出相同的假设)正在发生的事情。换句话说,在需要写入之前,期望正好两次读取。
1.添加一些协议说“轮到你写了”。(简单方法:一行写着“结束”!)

相关问题