aws ec2 linux客户端代码未与windows java上的服务器连接

o2rvlv0m  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(337)
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。

mzaanser

mzaanser1#

您可能需要检查vpc网络ACL,尤其是出站部分。
如果我搞清楚了,你就把4000号端口转发到4005号端口。
您确定已将服务器绑定到路由器ip吗?
你的路由器上是否有某种防火墙可以阻止外部IP?
最后但并非最不重要的一点,您是否考虑过您的windows防火墙可能阻止了您的外部请求?
谨致问候

相关问题