【问题标题】:AWS EC2 Linux Client code not connecting with Server on Windows JavaAWS EC2 Linux 客户端代码未与 Windows Java 上的服务器连接
【发布时间】:2021-03-25 01:26:30
【问题描述】:
public class Client {
    
    private Socket socket;
    private DataOutputStream out     = null; 
    private Writer writer;


    public Client(Writer writer) { // get all stats in the client class 
        this.writer = writer;
    }


    public void connectToServer() 
    { 
    
        // establish a connection 
    
        try
        { 
            System.out.println("Connecting...");
            socket = new Socket("39.32.42.65", 4003); 
        
        
            System.out.println("Connected"); 
  
  
            // sends output to the socket 
            out    = new DataOutputStream(socket.getOutputStream()); 
        }
        catch(UnknownHostException u) 
        { 
            System.out.println(u); 
        } 
        catch(IOException i) 
        { 
            System.out.println(i); 
        } 


            try
            { 
                this.writer.transferStats(out); // send all stats to back end server
                out.writeUTF("Over"); // finish sending 
            } 
            catch(IOException i) 
            { 
                System.out.println(i); 
            } 

        try
        { 
            out.close(); 
            socket.close(); 
        } 
        catch(IOException i) 
        { 
            System.out.println(i); 
        } 
    } 

}

这是上面的客户端代码 ^

package com.clxpr.demo;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import org.springframework.context.ConfigurableApplicationContext;
import com.clxpr.demo.service.CpuDataService;
import com.clxpr.demo.service.DiskDataService;
import com.clxpr.demo.service.IoDataService;
import com.clxpr.demo.service.MemDataService;
import com.clxpr.demo.service.PidDataService;
import com.clxpr.demo.service.PidSchedDataService;
import com.clxpr.demo.service.ResourceDataService;
import com.clxpr.demo.service.ResourceService;

public class LinuxListener implements Runnable {

    private ConfigurableApplicationContext springContext;
    private ServerSocket server;
    private ResourceService resourceServ;
    private ResourceDataService resourceDataServ;
    private CpuDataService cpuDataServ;
    private DiskDataService diskDataServ;
    private IoDataService ioDataServ;
    private MemDataService memDataServ;
    private PidDataService pidDataServ;
    private PidSchedDataService pidSchedDataServ;

    // Constructor to get objects of services managed by java spring 
    public LinuxListener(ConfigurableApplicationContext springContext) throws IOException {
        // TODO Auto-generated constructor stub
        this.springContext=springContext;
        this.resourceServ=springContext.getBean("ResourceService",ResourceService.class);
        this.resourceDataServ=springContext.getBean("ResourceDataService",ResourceDataService.class);
        this.cpuDataServ = springContext.getBean("CpuDataService",CpuDataService.class);
        this.diskDataServ = springContext.getBean("DiskDataService",DiskDataService.class);
        this.ioDataServ = springContext.getBean("IoDataService",IoDataService.class);
        this.pidDataServ = springContext.getBean("PidDataService",PidDataService.class);
        this.pidSchedDataServ = springContext.getBean("PidSchedDataService",PidSchedDataService.class);
        this.memDataServ = springContext.getBean("MemDataService",MemDataService.class);
        server = new ServerSocket(4003);    // create server socket for client to connect to
        
    }
    
    public ConfigurableApplicationContext getContext() {
        return this.springContext;
    }
    
    

    @Override
    public void run() {
        int usrId = 1;
        while(true) {
            try { 
                    
                    System.out.println("Server started"); 
          
                    System.out.println("Waiting for a client ..."); 
          
                    Socket socket = server.accept(); 
                    System.out.println("Client accepted"); 
          
                    // takes input from the client socket 
                    DataInputStream in = new DataInputStream( 
                        new BufferedInputStream(socket.getInputStream())); 
                    Thread t = new ClientHandler(socket,in,this.resourceServ,this.resourceDataServ,this.cpuDataServ,this.diskDataServ,
                            this.ioDataServ,this.memDataServ,this.pidDataServ,this.pidSchedDataServ, usrId);
                    t.start();
                   
            } 
            catch(IOException i) 
            { 
                System.out.println(i); 
            }
            usrId++;
        }
    }
    

}

这是上面的服务器代码 ^

给出的 ip 是我拥有的公共 ip。客户端代码在 AWS ec2 实例 Linux env 上 它没有与服务器连接。当我在同一网络上运行它们时,它会连接。 我更改了路由器上的设置以在 NAT 下启用端口 4000 到 4005。

【问题讨论】:

  • 检查 EC2 实例安全组。 4000-4005默认是不允许的
  • 我认为这也是问题,但是当我获得静态 IP 然后尝试我的公共 IP 时它起作用了。

标签: java amazon-web-services server client public


【解决方案1】:

您可能需要检查您的 VPC 网络 ACL,尤其是出站部分。

如果我明白了,您将端口 4000 转发到 4005。

您确定将您的服务器绑定到您的路由器 IP 吗?

您的路由器上是否有某种防火墙可能会阻止外部 ip?

最后但同样重要的是,您是否考虑过您的 Windows 防火墙可能会阻止您接收外部请求?

亲切的问候

【讨论】:

  • 当我打电话给我的服务提供商为我分配静态 IP 时它起作用了。
猜你喜欢
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-27
相关资源
最近更新 更多