Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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 简单套接字程序中的错误_Java_Sockets_Java Io - Fatal编程技术网

Java 简单套接字程序中的错误

Java 简单套接字程序中的错误,java,sockets,java-io,Java,Sockets,Java Io,我无法发送第二个实例的数据。服务器只是无限期地等待客户端数据 以下是我的示例服务器代码片段: ServerSocket serv = new ServerSocket(6789); Socket soc = serv.accept(); System.out.println("waiting for client's input"); BufferedReader in = new BufferedReader(new InputStreamReader(s

我无法发送第二个实例的数据。服务器只是无限期地等待客户端数据 以下是我的示例服务器代码片段:

ServerSocket serv = new ServerSocket(6789);
    Socket soc = serv.accept();
    System.out.println("waiting for client's input");       
    BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
    DataOutputStream out = new DataOutputStream(soc.getOutputStream());
    String indata=in.readLine();
    System.out.println("The client says: "+indata+"\n Send them some data: ");
    String datum="demodata";        
    out.writeBytes(datum);
    System.out.println("Data sent");
示例客户端:

ocket soc = new Socket("localhost",6789);

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
    DataOutputStream out = new DataOutputStream(soc.getOutputStream());     
    System.out.println("Connected to: "+soc.getLocalAddress() +"\nEnter data to be sent: ");        
    String outdata = br.readLine(); //take input    
    out.writeBytes(outdata); // send 
    String indata=in.readLine(); //read
    System.out.println("Data Sent! Now reading data from server "+indata)

请告诉我我的问题!提前感谢

我想您忘记刷新输出流了


在服务器端和客户端的out.writeBytes之后添加一行:out.flush。

我想您忘记刷新输出流了


在服务器端和客户端的out.writeBytes之后添加行:out.flush。

您的客户端在写入的输出中没有发送换行符。您的服务器通过在BufferedReader上调用readLine来隐式地期待这个换行符,BufferedReader在通过该方法读取的数据中期待一个终止的换行符。在调出.writeBytes后,在客户端和服务器代码中添加以下内容:


或者,您也可以使用PrintWriter,只使用其println方法。

您的客户机在写入的输出中没有发送换行符。您的服务器通过在BufferedReader上调用readLine来隐式地期待这个换行符,BufferedReader在通过该方法读取的数据中期待一个终止的换行符。在调出.writeBytes后,在客户端和服务器代码中添加以下内容:


或者,您也可以使用PrintWriter,只使用它的println方法。

答案很简单。您希望在服务器和客户端之间交换文本行

正确的部分位于服务器端:

String indata=in.readLine();
错误的部分位于客户端:

out.writeBytes(outdata); // send 
我尚未测试代码,但您似乎只是在发送一些数据,服务器端等待\n换行符转义序列出现

备选案文1: 构造一个PrintWriter并调用相应的println方法

备选案文2: 手动追加换行符\n


之后,服务器端的readLine将识别\n终止的行,并继续操作。

答案非常简单。您希望在服务器和客户端之间交换文本行

正确的部分位于服务器端:

String indata=in.readLine();
错误的部分位于客户端:

out.writeBytes(outdata); // send 
我尚未测试代码,但您似乎只是在发送一些数据,服务器端等待\n换行符转义序列出现

备选案文1: 构造一个PrintWriter并调用相应的println方法

备选案文2: 手动追加换行符\n


之后,服务器端的readLine将识别\n终止的行,并将继续操作。

以下是一些示例代码。看看它是否适合你。您可以使用Ctrl-C停止服务器代码,客户端将等待服务器重新出现

此外,您可以停止服务器,它将等待客户端重新出现。 main方法具有语法

package test;

import java.io.*;
import java.net.*;

public class SocketIPC{
    public PrintWriter out;
    BufferedReader in;
    Socket socket = null;
    ServerSocket serverSocket = null;
    StringBuffer sb;
    int port;

    public SocketIPC(int port) throws Exception{
        this.port = port;
    }

    public void send(String msg){
        sb.append(msg+"\n");
    }

    public void flush(){
        out.write(sb.toString());
        sb = new StringBuffer();
        out.flush();
    }

    public String recv() throws Exception{
            return in.readLine();
    }

    public void setIO() throws Exception{
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        sb = new StringBuffer();
    }

    public void serverLoop() throws Exception{
        serverSocket = new ServerSocket(port);
        while(true){
            socket = serverSocket.accept();
            setIO();

            int count = 0;
            System.out.println("Connected.");
            while(true){
                count += 1;
                for(int j=0; j<100; j++)
                    send("+");
                send(String.format(".%d", count));
                flush();
                Thread.sleep(1000L);
                String msg = recv();
                if (msg!=null)
                    System.out.println(msg);
                else
                    break;// socket connection is broken
            }
            }
    }

    public void clientLoop() throws Exception{
        while(true){
            while(socket==null){
                try{
                    socket = new Socket("localhost", port);
                }catch(Exception e){
                    System.out.println("Waiting for server.");
                    Thread.sleep(1000L);
                }
                }
            setIO();

            System.out.println("Connected.");
            while(true){
                String msg = recv();
                if (msg==null){
                    socket.close();
                    socket = null;
                    break;
                }
                if (msg.charAt(0)=='.'){
                    int idx = Integer.parseInt(msg.substring(1));
                        System.out.println(idx);
                    send(Integer.toString(idx));
                    Thread.sleep(2000);
                    flush();
                }
            }
        }
    }

    public static void main(String[] args){
        if (args.length != 1){
            System.out.println("Server invocation: java test.SocketIPC s");
            System.out.println("Client invocation: java test.SocketIPC c");
        }

        int port = 32000;
        try{
            SocketIPC fip = new SocketIPC(port);
            if (args[0].equals("s")){
                System.out.println("Server started");
                fip.serverLoop();
            }else{
                System.out.println("Client started");
                fip.clientLoop();
            }
            System.out.println("Done");
        }catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
            System.exit(1);
        }
    }
}

下面是一些示例代码。看看它是否适合你。您可以使用Ctrl-C停止服务器代码,客户端将等待服务器重新出现

此外,您可以停止服务器,它将等待客户端重新出现。 main方法具有语法

package test;

import java.io.*;
import java.net.*;

public class SocketIPC{
    public PrintWriter out;
    BufferedReader in;
    Socket socket = null;
    ServerSocket serverSocket = null;
    StringBuffer sb;
    int port;

    public SocketIPC(int port) throws Exception{
        this.port = port;
    }

    public void send(String msg){
        sb.append(msg+"\n");
    }

    public void flush(){
        out.write(sb.toString());
        sb = new StringBuffer();
        out.flush();
    }

    public String recv() throws Exception{
            return in.readLine();
    }

    public void setIO() throws Exception{
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        sb = new StringBuffer();
    }

    public void serverLoop() throws Exception{
        serverSocket = new ServerSocket(port);
        while(true){
            socket = serverSocket.accept();
            setIO();

            int count = 0;
            System.out.println("Connected.");
            while(true){
                count += 1;
                for(int j=0; j<100; j++)
                    send("+");
                send(String.format(".%d", count));
                flush();
                Thread.sleep(1000L);
                String msg = recv();
                if (msg!=null)
                    System.out.println(msg);
                else
                    break;// socket connection is broken
            }
            }
    }

    public void clientLoop() throws Exception{
        while(true){
            while(socket==null){
                try{
                    socket = new Socket("localhost", port);
                }catch(Exception e){
                    System.out.println("Waiting for server.");
                    Thread.sleep(1000L);
                }
                }
            setIO();

            System.out.println("Connected.");
            while(true){
                String msg = recv();
                if (msg==null){
                    socket.close();
                    socket = null;
                    break;
                }
                if (msg.charAt(0)=='.'){
                    int idx = Integer.parseInt(msg.substring(1));
                        System.out.println(idx);
                    send(Integer.toString(idx));
                    Thread.sleep(2000);
                    flush();
                }
            }
        }
    }

    public static void main(String[] args){
        if (args.length != 1){
            System.out.println("Server invocation: java test.SocketIPC s");
            System.out.println("Client invocation: java test.SocketIPC c");
        }

        int port = 32000;
        try{
            SocketIPC fip = new SocketIPC(port);
            if (args[0].equals("s")){
                System.out.println("Server started");
                fip.serverLoop();
            }else{
                System.out.println("Client started");
                fip.clientLoop();
            }
            System.out.println("Done");
        }catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
            System.exit(1);
        }
    }
}