Java client cant connect with NodeJS server on heroku - java

So I have this Javascript server:
const net = require('net');
var port = process.env.PORT || 3001;
net.createServer(function (sock){
console.log('CONNECTED: ' + sock.remoteAddress + ":" + sock.remotePort);
sock.on('data', function(data){
console.log(data.toString());
});
sock.on('close', function(data){
console.log('DISCONNECTED: ' + sock.remoteAddress + ":" + sock.remotePort);
});
sock.on('error', function(error){
console.log(error);
});
}).listen(port, '0.0.0.0');
console.log('Listening on: ' + port);
And I use this to connect my Java client:
public void connect () {
System.out.println("Connecting with: " + host + ":" + port);
try {
this.socket = new Socket(this.host, this.port);
System.out.println("Connected!");
} catch (IOException e) {
e.printStackTrace();
}
}
And it works when I try it local but whenever I upload it to heroku it never logs a CONNECTED message or any received data so I don't think it connects.
I'm using this as ip/port to connect my client: .herokuapp.com:heroku.env.PORT not literally ofcourse, I use the port heroku gives to my application on launch since I do see the Listening on: message.
Am I doing something wrong? Or does heroku not support sockets?

Your client should connect to <appname>.herokuapp.com on port 80 or 443. The PORT var is for internal binding only.

Related

Not able to publish: MQTT client is not connected in java

When i run my code it's working fine but after some hours it stop working and show this error Not able to publish: MQTT client is not connected
public Mqtt5AsyncClient connect(String host, int port) {
Mqtt5AsyncClient client = MqttClient.builder().useMqttVersion5()
.identifier(UUID.randomUUID().toString())
.serverHost(host)
.serverPort(port).buildAsync();
Mqtt5ConnAck connectionAck = null;
try {
connectionAck = client.toBlocking().connect();
Mqtt5ConnAckReasonCode connAckCode = connectionAck.getReasonCode();
logger.debug("Client connected to broker with url: " + host + ":" + port + " ::Connection ack code: " + " keep alive ");
} catch (Exception ex) {
logger.debug("Not able to connect to broker with url: " + host + ":" + port);
ex.printStackTrace();
}
return client;
}

jquery stomp websockets server reconnection reinitialization

I have a websocket app that connects to my Java backend via stomp.js file.
function connect() {
var socket = new SockJS('<?php echo $rootbasename;?>wsconnect');
stompClient = Stomp.over(socket);
stompClient.debug = null;
stompClient.connect({}, function (frame) {
console.log('Connected: ' + frame);
console.log('/queue/messages/' + widgetId + "/" + $.cookie(cookiename));
stompClient.subscribe('/queue/messages/' + widgetId + "/" + $.cookie(cookiename), function (result) {
//code here
}, {userToken: $.cookie(cookiename), widgetId: widgetId});
stompClient.subscribe('/queue/makereadresult/' + widgetId + '/' + $.cookie(cookiename), function (result) {
});
But, what if my java backend server will reboot? I want customers not to notice any change. Is there a way to auto-reconnect on connection lost? Or any way to make it smooth for clients?
When you are connecting you can pass in error callback. In there you could have reconnect logic.
For example stomp has this method
client.connect(login, passcode, connectCallback, errorCallback);
and in errorCallback just call connectCallback.

Java Socket Client Unable to Connect to C# Socket Server

I have a use case, where I have to send data from a .NET application, to a Java application and I'm trying to do this using sockets. I have a server created using C# -
TcpListener tcpListener = null;
IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
try
{
// Set the listener on the local IP address
// and specify the port.
tcpListener = new TcpListener(ipAddress, 13);
tcpListener.Start();
Console.WriteLine("The server is running at port 13...");
Console.WriteLine("The local End point is -" +
tcpListener.LocalEndpoint);
output = "Waiting for a connection...";
Console.WriteLine(output);
Socket s = tcpListener.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
}
catch (Exception e)
{
output = "Error: " + e.ToString();
Console.WriteLine(output);
}
}
On the Java side, I have created a socket which listens to the same host and port -
Socket socket;
try {
socket = new Socket( "localhost", 13);
System.out.println("Connection established");
BufferedReader input =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
String answer = input.readLine();
System.out.println(answer);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
I'm getting the below error - java.net.ConnectException: Connection refused: connect. I have used telnet localhost 13, to check if my server is really running, and it is. So i don't think it could be an issue with server not running or firewalls, since both are running locally. Any ideas on how to resolve this?
I tried your code and I had exactly the same problem. Your C# TCP Server only binds to the IPv6 interface (check e.g. the resource monitor listening addresses). If you change your server to new TcpListener(IPAddress.Any, 13) it should work.

Client Socket Port is different between Server Socket port

I've wrote simple client (using SocketChannel):
User friend = database.getUserByName("jonh");
SocketChannel friendSkt = SocketChannel.open(new InetSocketAddress(InetAddress.getByName(friend.getHost()), friend.getPort()));
System.out.println("Local: " + friendSkt.socket().getLocalSocketAddress()
+ " |Remote: "+ friendSkt.socket().getRemoteSocketAddress());
and server (using simple Socket):
ServerSocket skt = new ServerSocket(0);
Socket server = skt.accept();
InputStream x = server.getInputStream();
System.out.println("Local: " + server.getLocalSocketAddress() + " |Remote: "+ server.getRemoteSocketAddress());
Even if connect returns no exception and client write on socket (simple getOutputStream.write(...)), server not read and returns -1. So I've printed the address of each socket and found this:
CLIENT: Local: /192.168.0.2:58981 |Remote: /192.168.0.2:58968
SERVER: Local: /192.168.0.2:58968 |Remote: /192.168.0.2:58980
It is normal that client local port is server remote port+1 instead to being the same value?

Java UDP Port 161 scan

Port 161 is used by SNMP. I wrote a small peice of code to check if a device is running SNMP on this port.
public boolean isPortOpen(String ip, int port, int timeout) {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), timeout);
socket.close();
logger.info(threadId, "Port is Open : " + ip + ":" + port );
return true;
} catch (IOException ex) {
logger.error(threadId, "isPortOpen : " + ip + ":" + port + "\t"+ ex.getMessage());
return false;
}
}
However to my surprise the code always returns false for port 161 even though the box is running SNMP and other tools such as PortQryV2 return true for port 161. Can someone please help!! TIA

Categories

Resources