So to put everything simple I have a project to do which is 'Chat application' and I wanted to do it with java Sockets cause then I will be able to create different threads for every user as it's a college project there won't be many users. So as usual I found website named 'HEROKU' which will host my application server.
Now the problem starts as I actually created programs with notepad and command prompt, and suddenly I came to know that you can't just publish it on the heroku like that so, I created a Maven project and made a simple prime checker code.
But when I push the code to heroku it accepts anonymous socket connection that aren't supposed to be connecting to my socket. Here is my code -
import java.net.*;
import java.io.*;
public class DemoPrimeServer
{
static int getEnvPort() // if code running on server bind to heroku provided port, otherwise 80
{
//if(System.getenv("PORT") == null)
return Integer.parseInt(System.getenv("PORT"));
//else
// return 80;
}
public static void main(String[] args)throws IOException
{
ServerSocket ss = new ServerSocket(getEnvPort());
System.out.println("Waiting for Clients... on port : "+ss.getLocalPort());
try
{
while(true)
{
Runnable r = new HandleClient(ss.accept());
new Thread(r).start(); //for handling multiple clients
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
ss.close();
}
}
}
class HandleClient implements Runnable // basically handles the client
{
DataInputStream d1;
DataOutputStream d2;
String msg,cl_nm;
Socket s;
HandleClient(Socket s)
{
this.s =s;
msg ="";
}
public void run()
{
try
{
InetSocketAddress is = (InetSocketAddress) s.getRemoteSocketAddress(); // trying to get who is connected
System.out.println("remote host name : " + is.getHostName());
System.out.println("reomtr host port : "+ is.getPort());
getStreams();
getClientName(); // Line number 61
while(true)
{
//getting input and passing out output.. Basically given no. is prime or not
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
d1.close();
d2.close();
s.close();
}
catch (IOException e)
{
System.out.println("cant close socket of client : "+cl_nm);
e.printStackTrace();
}
}
}
void getClientName()throws IOException
{
cl_nm = d1.readUTF(); // Line number 102
System.out.println("Client Connected : "+cl_nm);
}
void getStreams() throws IOException
{
d1 = new DataInputStream(s.getInputStream());
d2 = new DataOutputStream(s.getOutputStream());
}
boolean checkPrime(int ch)
{
for(int i = 2;i<=ch/2;i++)
if(ch%i == 0)
return true;
return false;
}
}
The heroku logs provides the info like this - (Don't consider the line no's as the code is actually slightly deleted for improved understanding, the error line numbers are as comment in code snippet)
2020-12-31T08:18:48.673564+00:00 app[web.1]: Waiting for Clients... on port : 53635
2020-12-31T08:18:50.281862+00:00 app[web.1]: remote host name : 93657a43-e55a-4d7a-95d4-8073c080b09b.int.dyno.rt.heroku.com
2020-12-31T08:18:50.281981+00:00 app[web.1]: reomtr host port : 244682020-12-31T08:18:50.287804+00:00 app[web.1]: java.io.EOFException
2020-12-31T08:18:50.288109+00:00 app[web.1]: at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
2020-12-31T08:18:50.288235+00:00 app[web.1]: at java.io.DataInputStream.readUTF(DataInputStream.java:589)
2020-12-31T08:18:50.288350+00:00 app[web.1]: at java.io.DataInputStream.readUTF(DataInputStream.java:564)
2020-12-31T08:18:50.288465+00:00 app[web.1]: at com.testserver.PrimeServer.HandleClient.getClientName(DemoPrimeServer.java:102)
2020-12-31T08:18:50.288587+00:00 app[web.1]: at com.testserver.PrimeServer.HandleClient.run(DemoPrimeServer.java:61)
2020-12-31T08:18:50.288700+00:00 app[web.1]: at java.lang.Thread.run(Thread.java:748)
Also I can't actually connect to the server application myself via a client code cause its keep me giving UnknownHostException Exception
client code -
Socket cs = new Socket("https://prime-number-6969.herokuapp.com/",11961);
Also in real scenario how I am going to know port number of the heroku socket, it changes everytime.
I found out an similar question here -
hello world with sockets on heroku
As it didn't actually explained what 'Make an HTTP version meant'
So what I really need right now is a way to send and receive data between heroku server and client in java while also managing multiple users which sockets can do but it isn't doing.
Any quick help will be very nice as I need to start the project ASAP.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…