客户端想要连接到服务器时出错

时间:2012-03-07 07:57:02

标签: java connection client client-server

假设我们使用以下类来说明客户端和服务器:

客户端

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

public class Client {
    private PrintWriter toServer;
    private BufferedReader fromServer;
    private Socket socket;

    public Client() throws IOException {
        socket = new Socket("127.0.0.1", 3000);
    }

    public void openStreams() throws IOException {
        toServer = new PrintWriter(socket.getOutputStream(), true);
        fromServer = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));
    }

    public void closeStreams() throws IOException {
        fromServer.close();
        toServer.close();
        socket.close();
    }

    public void run() throws IOException {
        openStreams();
        String msg = "";
        Scanner scanner = new Scanner(System.in);
        toServer.println("Hello from Client.");
        while (!"exit".equals(msg)) {
            System.out.println(">");

            toServer.println("msg");
            String tmp = fromServer.readLine();
            System.out.println("Server said: " + tmp);
        }

        closeStreams();
    }
}

服务器

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class myServer {
    private static ServerSocket serverSocket;
    private static Socket socket;
    private static PrintWriter toClient;
    private static BufferedReader fromClient;

    public static void run() throws IOException {
        System.out.println("Server is waiting for connections...");

        while (true) {
            openStreams();
            processClient();
            closeStreams();
        }
    }

    public static void openStreams() throws IOException {
        serverSocket = new ServerSocket(3000);
        socket = serverSocket.accept();
        toClient = new PrintWriter(socket.getOutputStream(), true);
        fromClient = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));
    }

    public static void closeStreams() throws IOException {
        fromClient.close();
        toClient.close();
        socket.close();
        serverSocket.close();
    }

    public static void processClient() throws IOException {
        System.out.println("Connection established.");
        String msg = fromClient.readLine();
        toClient.println("Client said " + msg);
    }
}

如果我们首先运行服务器,然后在服务器准备就绪后运行客户端,我们会收到以下错误:

Error in Client Software caused connection abort: recv failed

问题是什么?

2 个答案:

答案 0 :(得分:0)

小心你的代码

while (true){
    openStreams(); 
    processClient();  
    closeStreams();
}

在这种情况下,套接字连接始终已重置。因此,客户端无法成功创建套接字连接。 你应该让代码像:

openStreams();
while(someCondition){
    processClient();
}
closeStream();

答案 1 :(得分:0)

也许你可以尝试使用旧版本的java,类似的事情发生在我身上,当我使用sdk 1.6代替1.7一切都修复了