簡體   English   中英

java-無法使用套接字運行程序(無法連接和監聽失敗)

[英]java - Unable to run programs utilising sockets (Unable to connect and listen failed)

我有兩個類-提供者和請求者:

提供商

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


public class Provider {
    ServerSocket providerSocket;
    Socket connection = null;
    ObjectOutputStream out;
    ObjectInputStream in;
    String message;

    Provider() {
    }

    void run() {
        try {
            // 1. creating a server socket
            providerSocket = new ServerSocket(2004, 10);
            // 2. Wait for connection
            System.out.println("Waiting for connection");
            connection = providerSocket.accept();
            System.out.println(
                    "Connection received from " + connection.getInetAddress().getHostName());
            // 3. get Input and Output streams
            out = new ObjectOutputStream(connection.getOutputStream());
            out.flush();
            in = new ObjectInputStream(connection.getInputStream());
            sendMessage("Connection successful");
            // 4. The two parts communicate via the input and output streams
            do {
                try {
                    sendMessage(
                            "Please enter the phrase you wish to echo or the word FINISHED to exit");

                    message = (String) in.readObject();

                    sendMessage(message);

                } catch (ClassNotFoundException classnot) {
                    System.err.println("Data received in unknown format");
                }
            } while (!message.equals("FINISHED"));
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } finally {
            // 4: Closing connection
            try {
                in.close();
                out.close();
                providerSocket.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    void sendMessage(String msg) {
        try {
            out.writeObject(msg);
            out.flush();
            System.out.println("server>" + msg);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    public static void main(String args[]) {
        Provider server = new Provider();
        while (true) {
            server.run();
        }
    }
}

請求者

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Requester {
    Socket requestSocket;
    ObjectOutputStream out;
    ObjectInputStream in;
    String message;
    Scanner input;

    Requester() {
        input = new Scanner(System.in);
    }

    void run() {
        try {
            // 1. creating a socket to connect to the server
            requestSocket = new Socket("127.0.0.1", 2004);
            System.out.println("Connected to localhost in port 2004");
            // 2. get Input and Output streams
            out = new ObjectOutputStream(requestSocket.getOutputStream());
            out.flush();
            in = new ObjectInputStream(requestSocket.getInputStream());
            // 3: Communicating with the server
            try {
                message = (String) in.readObject();
                System.out.println("server>" + message);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            do {
                try {

                    message = (String) in.readObject();
                    System.out.println(message);
                    message = input.nextLine();
                    sendMessage(message);
                    message = (String) in.readObject();

                } catch (ClassNotFoundException classNot) {
                    System.err.println("data received in unknown format");
                }
            } while (!message.equals("FINISHED"));
        } catch (UnknownHostException unknownHost) {
            System.err.println("You are trying to connect to an unknown host!");
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } finally {
            // 4: Closing connection
            try {
                in.close();
                out.close();
                requestSocket.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    void sendMessage(String msg) {
        try {
            out.writeObject(msg);
            out.flush();
            System.out.println("client>" + msg);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    public static void main(String args[]) {
        Requester client = new Requester();
        client.run();
    }
}

這些程序基本上旨在相互通信。 這個想法是,他們每個人都通過套接字彼此“連接”,並且用戶應該能夠在Provider的控制台窗口中輸入內容,並將其回顯到Requester的控制台上。 但是,出現以下錯誤:

提供商

java.net.SocketException:權限被拒絕:監聽失敗

請求者

java.net.ConnectException:連接被拒絕:connect

(如果可以解決此問題,我可以提供其余的錯誤)。

我嘗試過將類放在相同的項目文件夾,單獨的文件夾和不同的工作區中。 我也嘗試過使用Eclipse EE(Neon)和SE(Oxygen)。 最近,我遇到了端口和套接字的問題(最著名的是Tomcat,並且在Eclipse中遇到了“找不到調試器的可用套接字”)。 那與我無法運行這些程序有關嗎?

檢查您的防火牆設置。 嘗試使用套接字時遇到類似的問題。 確保任何相關資源均未被阻止。

您可能還需要運行以下命令:

netstat -ano | 搜索結果:2004

檢查該端口是否已被錯誤使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM