简体   繁体   English

AWS EC2 Linux 客户端代码未与 Windows Java 上的服务器连接

[英]AWS EC2 Linux Client code not connecting with Server on Windows Java

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); 
        } 
    } 

}

This is client code above ^这是上面的客户端代码^

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++;
        }
    }
    

}

this is server code above ^这是上面的服务器代码^

The ip given is the public ip i have.给出的 ip 是我拥有的公共 ip。 The client code is on AWS ec2 instance Linux env It is not connecting with the server.客户端代码在 AWS ec2 实例 Linux env 上它没有与服务器连接。 It connects when i run both of them on same network.当我在同一网络上运行它们时,它会连接。 I changed the settings on my router to enable ports 4000 to 4005 under NAT.我更改了路由器上的设置以在 NAT 下启用端口 4000 到 4005。

You might want to check your VPC network ACLs, especially the outbound part.您可能想要检查您的 VPC 网络 ACL,尤其是出站部分。

If I get that clearly, you forwarded ports 4000 to 4005.如果我明白了,您将端口 4000 转发到 4005。

Are you sure you bound your server to your router IP?您确定将服务器绑定到路由器 IP 吗?

Is there some kind of firewall on your router that might block external ips?您的路由器上是否有某种防火墙可能会阻止外部 ip?

Last but not least, have you considered that maybe your Windows Firewall is preventing you from foreign requests?最后但并非最不重要的一点是,您是否考虑过您的 Windows 防火墙可能会阻止您接收外部请求?

Kind regards亲切的问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM