Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 来自服务器的BufferedReader不工作_Java_Bufferedreader - Fatal编程技术网

Java 来自服务器的BufferedReader不工作

Java 来自服务器的BufferedReader不工作,java,bufferedreader,Java,Bufferedreader,在这段代码中,我可以使用在客户端套接字上创建的BufferedReaderinClient正确地接收请求。 然后我将请求发送到服务器,我看到服务器收到了请求。 但是,当我尝试从服务器读取回复时(使用服务器套接字上的BufferedReaderinServer),它总是以IOException:无法从服务器读取结束 我指的是街区################ 你知道可能的原因吗 import java.io.*; import java.net.Socket; import java.net.S

在这段代码中,我可以使用在客户端套接字上创建的
BufferedReader
inClient
正确地接收请求。 然后我将请求发送到服务器,我看到服务器收到了请求。 但是,当我尝试从服务器读取回复时(使用服务器套接字上的
BufferedReader
inServer
),它总是以
IOException:无法从服务器读取
结束

我指的是街区################ 你知道可能的原因吗

import java.io.*;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ProxyMain {

public static void main(String argv[]) {

    int proxyPort = 55554;
    String proxyAddr = "127.0.0.1";
    ServerSocket proxySocket = null;

    try {
        proxySocket = new ServerSocket(proxyPort, 50, InetAddress.getByName("127.0.0.1"));
    }

    catch (Exception e) {
        System.err.println("Impossible to create socket server!");
        System.out.flush();
        System.exit(1);
    }

    System.out.printf("Proxy active on port: %d and on address %s\n", proxyPort, proxySocket.getInetAddress());
    System.out.println();

    while (true) {
        Socket client = null;
        Socket sockServ = null;
        BufferedReader inClient = null;
        PrintWriter outClient = null;
        BufferedReader inServer = null;
        PrintWriter outServer = null;
        String request = new String();
        String tmp = new String();
        String reply = new String();
        String tmpReply = new String();


        try {
            client = proxySocket.accept();
            System.out.println("Connected to: ");
            System.out.println(client.getInetAddress().toString());
            System.out.printf("On port %d\n", client.getPort());
            System.out.println();
            inClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
            outClient = new PrintWriter(client.getOutputStream(), true);

        }

        /*catch (IOException e) {
            System.err.println("Couldn't get I/O for connection accepted");
            System.exit(1);
        }*/

        catch (Exception e) {
            System.out.println("Error occurred!");
            System.exit(1);
        }

        System.out.println("Received request:");

        try{
            for (int i = 0; i<2; i++) {
                tmp = inClient.readLine();
                request = request + tmp;
            }

            inClient.close();
        }
        catch (IOException ioe) {
            System.err.println("Impossible to read mhttp request!");
            System.exit(1);
        }

        System.out.println(request);
        System.out.println();


        try {

            sockServ = new Socket("127.0.0.1", 55555);
            outServer = new PrintWriter(sockServ.getOutputStream(), true);
            inServer = new BufferedReader(new InputStreamReader(sockServ.getInputStream()));

        }

        catch (UnknownHostException e) {
            System.err.println("Don't know about host: 127.0.0.1:55555");
            System.exit(1);
        }

        catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: 127.0.0.1:55555");
            System.exit(1);
        }

        outServer.println(request);
        outServer.close();


        try {
            #################################################
            while ((tmpReply = inServer.readLine()) != null) {
                System.out.println(tmpReply);
                reply = reply + tmpReply;
            }

            inServer.close();
            sockServ.close();

        }

        catch (IOException ioe) {
            System.err.println("Impossible to read from server!");
            System.exit(1);
        }


        outClient.println(reply);
        outClient.close();

        try {
            client.close();
        }

        catch (IOException ioe) {
            System.err.printf("Impossible to close connection with %s:%d\n", client.getInetAddress().toString(), client.getPort());
        }


    }

}

}
import java.io.*;
导入java.net.Socket;
导入java.net.ServerSocket;
导入java.net.InetAddress;
导入java.net.UnknownHostException;
公共类代理{
公共静态void main(字符串argv[]){
int proxyPort=55554;
字符串proxyAddr=“127.0.0.1”;
ServerSocket proxySocket=null;
试一试{
proxySocket=新的ServerSocket(proxyPort,50,InetAddress.getByName(“127.0.0.1”);
}
捕获(例外e){
System.err.println(“无法创建套接字服务器!”);
System.out.flush();
系统出口(1);
}
System.out.printf(“代理在端口:%d和地址%s上处于活动状态\n”,proxyPort,proxySocket.getInetAddress());
System.out.println();
while(true){
socketclient=null;
套接字sockServ=null;
BufferedReader inClient=null;
PrintWriter outClient=null;
BufferedReader inServer=null;
PrintWriter outServer=null;
字符串请求=新字符串();
字符串tmp=新字符串();
字符串回复=新字符串();
字符串tmpReply=新字符串();
试一试{
client=proxySocket.accept();
System.out.println(“连接到:”);
System.out.println(client.getInetAddress().toString());
System.out.printf(“在端口%d\n上,client.getPort());
System.out.println();
inClient=new BufferedReader(新的InputStreamReader(client.getInputStream());
outClient=新的PrintWriter(client.getOutputStream(),true);
}
/*捕获(IOE异常){
System.err.println(“无法获取接受连接的I/O”);
系统出口(1);
}*/
捕获(例外e){
System.out.println(“发生错误!”);
系统出口(1);
}
System.out.println(“收到的请求:”);
试一试{

对于(int i=0;i当您关闭
outServer
时,您将关闭底层套接字。如果您只想关闭输出并保持输入打开,则需要使用
socket.shutdownOutput()
。注意,当您关闭
inClient
时,您也会遇到同样的问题。这是可行的,也许您可以从中获得一些想法

ChatServer-向所有连接的客户端广播

在一个命令提示符中:
JavaChartServer

在另一种情况下:
java聊天客户端localhost
(或服务器运行的ip地址)
另一个:
java聊天客户端localhost
(或服务器运行的ip地址)

在客户端窗口中开始聊天

像这样的服务器

// xagyg wrote this, but you can copy it
import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer {

    public static List list = new ArrayList();

    public static void main(String[] args) throws Exception {

        ServerSocket svr = new ServerSocket(4444);

        System.out.println("Chat Server started!");

        while (true) {
            try {
                Socket s = svr.accept();
                synchronized(list) {
                   list.add(s);              
                }                                  
                new Handler(s, list).start();
            }
            catch (IOException e) {
                // print out the error, but continue!
                System.err.println(e);
            }
        }
    }
}

class Handler extends Thread {

    private Socket s;
    private String ipaddress;
    private List list;

    Handler (Socket s, List list) throws Exception {
      this.s = s;
      ipaddress = s.getInetAddress().toString();
      this.list = list;
    }

    public void run () {

      try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String message;
        //MyDialog x = (MyDialog)map.get(ipaddress.substring(1));
        while ((message = reader.readLine()) != null) {
            if (message.equals("quit")) {
                synchronized(list) {
                    list.remove(s);
                }
                break;
            }
            synchronized(list) {
                for (Object object: list) {
                    Socket socket = (Socket)object;
                    if (socket==s) continue;
                    PrintWriter writer = new PrintWriter(socket.getOutputStream());
                    writer.println(ipaddress + ": " + message);
                    writer.flush();
                }
            }
        }
        try { reader.close(); } catch (Exception e) {}
      }
      catch (Exception e) {
        System.err.println(e);
      }
    }
}
像这样的客户

// xagyg wrote this, but you can copy it
import java.util.*;
import java.io.*;
import java.net.*;

public class ChatClient {


    public static void main(String[] args) throws Exception {

        Socket s = new Socket(args[0], 4444);
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintWriter out = new PrintWriter(s.getOutputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String message;
        new SocketReader(in).start();   
        while ((message = reader.readLine())!=null) {
            out.println(message);
            out.flush();
            if (message.equals("quit")) break;
        }
        in.close();
        out.close();
    }        
}

class SocketReader extends Thread {

    BufferedReader in;

    public SocketReader(BufferedReader in) {
        this.in = in;
    }

    public void run() {   
        String message;
        try {
            while ((message = in.readLine())!=null) {
                System.out.println(message);
            }
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }        
    }
}