Java客户端-服务器应用程序引发“ java.net.SocketException:连接重置”异常

时间:2019-06-01 10:25:57

标签: java sockets tcp

我试图创建一个客户端-服务器应用程序,其中客户端生成一个随机数并将n个字符串发送到服务器。发送字符串“ BYE”的连接已关闭。之后,服务器必须计算接收到的字符串的总长度并进行打印。

问题是,当我的客户端尝试连接到服务器时,该客户端将引发“ java.net.SocketException:连接重置”异常并崩溃。

这是我的客户代码:


import java.net.*;
import java.io.*;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ClientStringa {

    public static void main(String[] args) {
        try{
            Socket socket = new Socket("localhost", 5555);
            PrintWriter out = new PrintWriter(socket.getOutputStream());
            Random r = new Random();

            int N = r.nextInt(100);

            for(int i=1;i<=N;i++){
                out.println("String" +i);    
            }
            out.flush();
        }
        catch (IOException ex) {
            Logger.getLogger(ClientStringa.class.getName()).log(Level.SEVERE, null, ex);    
        }
    }
}

我的服务器代码:


import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ServerStringa {
    public static void main(String[] args) {
        try {
            ServerSocket socket = new ServerSocket(5555);

            while(true){
                System.out.println("Waiting for client connections...");
                Socket client = socket.accept();    //connessione dei client
                ThreadStringa thread = new ThreadStringa(client);
                thread.start();             
            }

        } catch (IOException ex) {
            Logger.getLogger(ServerStringa.class.getName()).log(Level.SEVERE, null, ex);
        }   
    }  
}

还有我的线程代码(因为服务器必须能够回答更多的客户端):


import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ThreadStringa extends Thread{
    Socket client;

    public ThreadStringa(Socket client) {
        this.client = client;
    }

    public void run(){
        try {
           // DataInputStream input = new DataInputStream(client.getInputStream());
            String message;
            int sum=0;

            InputStream input = client.getInputStream();
            InputStreamReader input2 = new InputStreamReader(input);
            BufferedReader in = new BufferedReader(input2);

            while((message= in.readLine()) != null){
                if(message.equals("BYE")){
                    break;
                }
                sum+=message.length();              
            }

            System.out.println("Total length of received strings: " + sum);   
            input.close();
            client.close();
        } 
        catch (IOException ex) {
            Logger.getLogger(ThreadStringa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

我在服务器控制台上看到的完整错误是:

java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
        at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
        at sun.nio.cs.StreamDecoder.read(Unknown Source)
        at java.io.InputStreamReader.read(Unknown Source)
        at java.io.BufferedReader.fill(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at serverstringa.ThreadStringa.run(ThreadStringa.java:27)

1 个答案:

答案 0 :(得分:0)

Instead of PrintWriter in client program, you should use DataOutputStream to communicate with Server in socket programming. Also remember to perform close() from client side. I have only modified your client program, you can check this.

import java.net.*;
import java.io.*;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ClientStringa {

    public static void main(String[] args) {
        try{
            Socket socket = new Socket("localhost", 5555);
            DataOutputStream out=new DataOutputStream(socket.getOutputStream());
//            PrintWriter out = new PrintWriter(socket.getOutputStream());
            Random r = new Random();
            int N = r.nextInt(100);
            for(int i=1;i<=N;i++){
//                out.println("String" +i);
                out.writeUTF("String" +i);
            }
            out.flush();
            out.close();
        }
        catch (IOException ex) {
            Logger.getLogger(ClientStringa.class.getName()).log(Level.SEVERE, null, ex);    
        }
    }
}