有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

amazon web服务AWS EC2 Linux客户端代码未与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); 
        } 
    } 

}

这是上面的客户端代码^

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


共 (1) 个答案

  1. # 1 楼答案

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

    如果我弄清楚了,你把端口4000转发到了4005

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

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

    最后但并非最不重要的一点是,您是否考虑过您的Windows防火墙可能会阻止您的外国请求

    亲切的问候