java-无法使用套接字运行程序(无法连接和监听失败)

时间:2018-10-17 13:40:51

标签: java

我有两个类-提供者和请求者:

提供商

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中遇到了“找不到调试器的可用套接字”)。这与我无法运行这些程序有关吗?

1 个答案:

答案 0 :(得分:0)

检查防火墙设置。尝试使用套接字时遇到类似的问题。确保任何相关资源均未被阻止。

您可能还需要运行命令:

  

netstat -ano | findstr:2004

检查该端口是否已被错误使用。