客户端套接字不从cmd获取任何输入

时间:2014-02-06 00:17:06

标签: java sockets io

我创建了一个程序,服务器将一个文件列表发送给客户端,然后客户端可以请求检查内容。它正确发送文件列表,但客户端不从控制台获取任何输入。 这是服务器程序

import java.util.*;
import java.io.*;
import java.net.*;
class TCPServer{
    public static void main(String args[]) throws Exception{
    ServerSocket server = new ServerSocket(4888);
    while(true){
    Socket client = server.accept();
    System.out.println(client);
    DataOutputStream out = new DataOutputStream(client.getOutputStream());
    File path = new File("C://testjava");
    String[] files = path.list();
    String send = "";
    for(String file:files){
        send = send + file + "\n";
    }
    out.writeBytes(send);
    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream    ()));
    String search_file = in.readLine();
    String searching = "";
    for(String file:files){
        if (file.equals(search_file)){
        searching = search_file;
        }
    }
if(searching.equals("")){
        out.writeBytes("Requested file does not exist");
        client.close();
    }
    Scanner file = new Scanner(new FileReader(searching));
    while(file.hasNextLine()){
        out.writeBytes(file.nextLine());
    }
    client.close();
    }
    }
}

这是客户端程序

import java.util.*;
import java.io.*;
import java.net.*;
class TCPClient{
    public static void main(String args[]) throws Exception{
        Socket client = new Socket("localhost",4888);
        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String  display = ""; 
        while ((display = in.readLine()) != null) {
            System.out.println(display);
        }
        System.out.println("\nChoose a file");
        Scanner src = new Scanner(System.in);
        String ask_file = src.nextLine();
        DataOutputStream out = new DataOutputStream(client.getOutputStream());
        out.writeBytes(ask_file);
        display = ""; 
        while ((display = in.readLine()) != null) {
            System.out.println(display);
        }
    }
}

有人可以解释为什么客户不接受任何输入吗? 感谢名单

2 个答案:

答案 0 :(得分:1)

在客户端中,in.readLine()会阻塞,直到Socket关闭。

由于您显然不想关闭套接字,您可以让服务器发送特殊消息以匹配循环。匹配时,跳出循环。

另外,readLine / nextLine之类的方法会吞噬新行,所以你需要添加一些像@EJP所说的。我在下面编辑了你的编码。我测试了它,现在似乎正在工作。

TCPSERVER

import java.util.*;
import java.io.*;
import java.net.*;
class TCPServer{
    public static void main(String args[]) throws Exception{
    ServerSocket server = new ServerSocket(4888);
    while(true){
    Socket client = server.accept();
    System.out.println(client);
    DataOutputStream out = new DataOutputStream(client.getOutputStream());
    File path = new File("C://Users/Brian/Desktop");
    String[] files = path.list();
    String send = "";
    for(String file:files){
        send = send + file + "\n";
    }
    send = send + "END\n"; // ADD SOMETHING LIKE THIS ------------------------------>
    out.writeBytes(send);
    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream    ()));
    String search_file = in.readLine();
    String searching = "";
    for(String file:files){
        if (file.equals(search_file)){
        searching = search_file;
        }
    }
if(searching.equals("")){
        out.writeBytes("Requested file does not exist");
        client.close();
    }
    Scanner file = new Scanner(new FileReader(searching));
    while(file.hasNextLine()){
        out.writeBytes(file.nextLine() + "\n"); // ADD A NEWLINE HERE ------------------>
    }
    client.close();
    }
    }
}

的TcpClient

import java.util.*;
import java.io.*;
import java.net.*;
class TCPClient{
    public static void main(String args[]) throws Exception{
        Socket client = new Socket("localhost",4888);
        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String  display = ""; 
        // ADD A TEST FOR "END" HERE --------------------------------------------->
        while ((display = in.readLine()) != null && !display.equals("END")) {
            System.out.println(display);
        }
        System.out.println("\nChoose a file");
        Scanner src = new Scanner(System.in);
        String ask_file = src.nextLine() + "\n"; // ADD A NEWLINE HERE ----------->
        DataOutputStream out = new DataOutputStream(client.getOutputStream());
        out.writeBytes(ask_file);
        display = ""; 
        while ((display = in.readLine()) != null) {
            System.out.println(display);
        }
    }
}

答案 1 :(得分:0)

你正在读行,但你不是在写行。您需要使用\n向正在编写的字符串添加writeBytes()。否则readLine()会永远阻塞等待永不到达的行终止符。