通过Java中的IP通过IP连接到服务器时出现ConnectException

萨拉

我用Java编写了一个简单的Client-Server程序。当我创建一个套接字
Socket socket = new Socket("localhost",7000);

我能够连接到服务器并将数据(任何控制台输入)传输到服务器,但是当我在Socket中传递localhost Ip(127.0.0.1)时

 Socket socket = new Socket("127.0.0.1",7000);

我收到以下错误错误:

java.net.ConnectException: Connection refused: connect

为什么我得到这个。

这是我的服务器端代码

public class SocketServer {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

      new SocketServer();

        // TODO code application logic here
    }

  public   SocketServer(){

       try {
            ServerSocket sSocket = new ServerSocket(7000);
            System.out.println("Server started at: " + new Date());


//Wait for a client to connect
            Socket socket = sSocket.accept();

            //Create the streams
            PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            //Tell the client that he/she has connected
            output.println("You have connected at: " + new Date());

            //Loop that runs server functions
            while(true) {
                //This will wait until a line of text has been sent
                String chatInput = input.readLine();
                System.out.println(chatInput);
            }
        } catch(IOException exception) {
            System.out.println("Error: " + exception);
        }


    }

这是我的客户端代码

public class ClientSocket {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws UnknownHostException {
        // TODO code application logic here

        new ClientSocket();
    }
  public ClientSocket() throws UnknownHostException
    {
    //We set up the scanner to receive user input
        Scanner scanner = new Scanner(System.in);

        try {
           //Socket socket = new Socket("localHost",7000);//works Fine
            Socket socket = new Socket("127.0.0.1",7000);//Gives error
            PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            //This will wait for the server to send the string to the client saying a connection
            //has been made.
            String inputString = input.readLine();
            System.out.println(inputString);
            //Again, here is the code that will run the client, this will continue looking for 
            //input from the user then it will send that info to the server.
            while(true) {
                //Here we look for input from the user
                String userInput = scanner.nextLine();
                //Now we write it to the server
                output.println(userInput);
            }
        } catch (IOException exception) {
            System.out.println("Error: " + exception);
        }
    }

}

这是我的/ etc / hosts文件

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost
127.0.0.1       localhost
127.0.0.1       localhost 
少年比尔·盖茨

我遇到了类似的问题。在您的代码中进行以下更改

在客户端

InetAddress addr = InetAddress.getByName("127.0.0.1");
Socket socket = new Socket(addr,7000);

在服务器端

ServerSocket sSocket = new ServerSocket();
sSocket.bind(new InetSocketAddress("0.0.0.0", 7000));

@Stefan也回答正确,0.0.0.0将其绑定到将允许它侦听所有接口

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在iOS 9中通过IP地址通过IP地址连接到服务器时,kCFStreamErrorDomainSSL,-9802

来自分类Dev

无法通过(IP或域名)连接到MySQL服务器

来自分类Dev

无法通过(IP或域名)连接到MySQL服务器

来自分类Dev

psql无法通过IP和端口5432连接到PostgreSQL服务器(postmaster)?

来自分类Dev

无法通过Macbook Pro上的本地IP连接到rumpus服务器

来自分类Dev

通过具有不同(外部)IP地址的NFS连接到服务器

来自分类Dev

为什么我不能通过实际的公共IP连接到服务器?

来自分类Dev

使用公网 IP 地址通过 Internet 连接到服务器的稳定性?

来自分类Dev

在WSL 2上通过Docker连接到TCP服务器时出现问题

来自分类Dev

通过IP连接到路由器时DNS错误

来自分类Dev

通过LAN IP地址连接WebSocket服务器

来自分类Dev

通过SSH连接时如何给用户服务器名称而不是IP?

来自分类Dev

当我尝试使用外部IP连接到服务器时Java套接字超时

来自分类Dev

使用JAVA通过FTP连接到远程服务器

来自分类Dev

通过 Android Emulator 连接到 Java 服务器

来自分类Dev

运行Rails时无法通过ubuntu中的套接字连接到本地MySQL服务器

来自分类Dev

为什么我不能通过公共静态IP连接到PC上运行的IIS服务器?

来自分类Dev

使用 volley 连接到 android 中的服务器时出现内部服务器错误?

来自分类Dev

通过子域获取客户端的IP时,它将返回服务器IP

来自分类Dev

无法连接到服务器(通过远程连接)

来自分类Dev

更新IP地址或通过以太网连接到宽带时出现“ RPC不可用”

来自分类Dev

通过外部IP连接到我的RaspberryPi时出现ERR_CONNECTION_RESET

来自分类Dev

尝试连接时出现Google CloudSQL(MySQL)错误:错误2003(HY000):无法在“ Google mysql ip here”上连接到MySQL服务器

来自分类Dev

通过android模拟器连接到服务器

来自分类Dev

通过android模拟器连接到服务器

来自分类Dev

无法使用WAN IP地址连接到服务器。

来自分类Dev

如何仅使用其IP连接到服务器?

来自分类Dev

几个特殊的IP使用ssh连接到服务器

来自分类Dev

使用“连接到服务器”通过SFTP连接以访问远程服务器中的文件夹

Related 相关文章

  1. 1

    在iOS 9中通过IP地址通过IP地址连接到服务器时,kCFStreamErrorDomainSSL,-9802

  2. 2

    无法通过(IP或域名)连接到MySQL服务器

  3. 3

    无法通过(IP或域名)连接到MySQL服务器

  4. 4

    psql无法通过IP和端口5432连接到PostgreSQL服务器(postmaster)?

  5. 5

    无法通过Macbook Pro上的本地IP连接到rumpus服务器

  6. 6

    通过具有不同(外部)IP地址的NFS连接到服务器

  7. 7

    为什么我不能通过实际的公共IP连接到服务器?

  8. 8

    使用公网 IP 地址通过 Internet 连接到服务器的稳定性?

  9. 9

    在WSL 2上通过Docker连接到TCP服务器时出现问题

  10. 10

    通过IP连接到路由器时DNS错误

  11. 11

    通过LAN IP地址连接WebSocket服务器

  12. 12

    通过SSH连接时如何给用户服务器名称而不是IP?

  13. 13

    当我尝试使用外部IP连接到服务器时Java套接字超时

  14. 14

    使用JAVA通过FTP连接到远程服务器

  15. 15

    通过 Android Emulator 连接到 Java 服务器

  16. 16

    运行Rails时无法通过ubuntu中的套接字连接到本地MySQL服务器

  17. 17

    为什么我不能通过公共静态IP连接到PC上运行的IIS服务器?

  18. 18

    使用 volley 连接到 android 中的服务器时出现内部服务器错误?

  19. 19

    通过子域获取客户端的IP时,它将返回服务器IP

  20. 20

    无法连接到服务器(通过远程连接)

  21. 21

    更新IP地址或通过以太网连接到宽带时出现“ RPC不可用”

  22. 22

    通过外部IP连接到我的RaspberryPi时出现ERR_CONNECTION_RESET

  23. 23

    尝试连接时出现Google CloudSQL(MySQL)错误:错误2003(HY000):无法在“ Google mysql ip here”上连接到MySQL服务器

  24. 24

    通过android模拟器连接到服务器

  25. 25

    通过android模拟器连接到服务器

  26. 26

    无法使用WAN IP地址连接到服务器。

  27. 27

    如何仅使用其IP连接到服务器?

  28. 28

    几个特殊的IP使用ssh连接到服务器

  29. 29

    使用“连接到服务器”通过SFTP连接以访问远程服务器中的文件夹

热门标签

归档