简体   繁体   中英

Java socket program hosted on heroku cannot get InputStream

I tried to upload my chat program to heroku, but client side couldn't get InputStream from server, so it stopped. What's problem in this? It worked well when I just try in localhost. It printed "getting I/O stream" but it seems like it stopped while getting InputStream.

Also is it possible to get output from Server.jar? I cannot check if my program is working well.

Server

server = new ServerSocket();
server.bind(new InetSocketAddress(ip, port));
System.out.println("server opened in " + ip + ":" + port);

try {
    while (server.isClosed() == false) {
        if (server.isClosed() == false) {
            Socket connection = server.accept();
            Client client = new Client(connection);
        }
    }
} catch (Exception error) {
    if (server.isClosed() == false) {
        error.printStackTrace();
        shutdown();
    }
}

Client

InetAddress[] dns = null;
try {
    dns = InetAddress.getAllByName("jjabtu.herokuapp.com");
} catch (UnknownHostException error) {
    System.out.println("No domain found");
}

Socket connection = new Socket(dns[0].getHostAddress(), 80);
System.out.println("getting I/O stream");
ObjectOutputStream output = new ObjectOutputStream(connection.getOutputStream());
ObjectInputStream input = new ObjectInputStream(connection.getInputStream());
System.out.println("got I/O stream");

heroku supports websockets, but pure socket tcp connection? I guess they don't support this. you connect to the server using a "router" server in the middle, so it is diferent.network topology than when you run this locally.

see this https://devcenter.heroku.com/articles/node-websockets https://devcenter.heroku.com/articles/play-java-websockets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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