Socket connection refused in Client Code - java

The below program causes this issue
EDITED:
import java.io.*;
import java.net.*;
public class smtpClient {
public static void main(String[] args) {
// declaration section:
// smtpClient: our client socket
// os: output stream
// is: input stream
Socket smtpSocket = null;
DataOutputStream os = null;
DataInputStream is = null;
// Initialization section:
// Try to open a socket on port 25 : step 1
// Try to open input and output streams: step 2
try {
smtpSocket = new Socket("192.168.1.2", 1024);
os = new DataOutputStream(smtpSocket.getOutputStream());
is = new DataInputStream(smtpSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host: hostname");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: hostname");
}
// If everything has been initialized then we want to write some data
// to the socket we have opened a connection to on port 25
if (smtpSocket != null && os != null && is != null) {
try {
// The capital string before each colon has a special meaning to SMTP
// you may want to read the SMTP specification, RFC1822/3
os.writeBytes("HELO\n");
os.writeBytes("MAIL From: k3is#fundy.csd.unbsj.ca\n");
os.writeBytes("RCPT To: k3is#fundy.csd.unbsj.ca\n");
os.writeBytes("DATA\n");
os.writeBytes("From: k3is#fundy.csd.unbsj.ca\n");
os.writeBytes("Subject: testing\n");
os.writeBytes("Hi there\n"); // message body
os.writeBytes("\n.\n");
os.writeBytes("QUIT");
// keep on reading from/to the socket till we receive the "Ok" from SMTP,
// once we received that then we want to break.
String responseLine;
while ((responseLine = is.readLine()) != null) {
System.out.println("Server: " + responseLine);
if (responseLine.indexOf("Ok") != -1) {
break;
}
}
// clean up:
// close the output stream
// close the input stream
// close the socket
os.close();
is.close();
smtpSocket.close();
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
}
Console Log :
Couldn't get I/O for the connection to: hostname
The program I took is from :
http://www.javaworld.com/jw-12-1996/jw-12-sockets.html?page=4
I have already tried modifying the port from 25 to 1024
I am running it on my local PC, so I am admin on this system, but not sure if there is any default firewall issue(running this in eclipse on windows 7)
As per your comments below : DO I need to make a listner, which mean to say a Server Socket, which will listen to smtp client requests

Answer is: according to details what you have provided, there is no listener running or machine with specified IP and port number.
UPD: then you are trying to connect to somewhere you do have to be sure that there is something which listens on other side, either writing your own server code or by using a 3rd party server/code to provide certain service on a port number you are trying to reach.
Why would you expect that there is a mail server running on machine with an address you've provided?

It sounds like some other program is using port 1024.
Try a different port.

Related

Why does socket connect() method timeout? netcat (nc) works fine

I have a larger block of Java code, but the important lines are these:
public static String tcp(String hostName, Number port, Number connectionTimeOutMs, Number readTimeOutMs, String message) {
String errmsg = "";
try (
Socket socket = new Socket();
) {
Inet4Address address = (Inet4Address) Inet4Address.getByName(hostName);
System.out.println("IP address:" + address.getHostAddress());
socket.connect(new InetSocketAddress(address, port.intValue()), connectionTimeOutMs.intValue());
socket.setSoTimeout(readTimeOutMs.intValue());
When I supply an IP address in the form "45.79.112.203" or "tcpbin.com", the code gives a SocketTimeoutException.
In the latter case, the line
System.out.println("IP address:" + address.getHostAddress());
gives the correct IP address, so the hostname is resolved correctly; it matches what ping tcpbin.com returns.
I want to be able to call the function with either an IPv4 address (in String format) or a hostname.
What am I doing wrong? Why does the socket fail to establish a connection, even with a high timeout of 60,000 ms?
Notes:
tcpbin.com is an "echo" server to test socket connections. It is only used as an example and should not be the cause of the problem.
Try the following:
echo "Text to send to TCP" | nc tcpbin.com 4242
You should get back the string that was just sent.
In the tcp() function, I pass in numbers in the form of a Number object, since the Java code gets called from Karate test framework via Java inter-op and JavaScript. JavaScript has the type Number, but no int or double.
===
Update:
Here a simple tcp server TcpServer.java
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) {
System.out.println("Listening on port 4242");
ServerSocket listener = null;
try {
do {
listener = new ServerSocket(4242);
Socket other = listener.accept();
System.out.println(">>> got a new connection from "
+ other.getInetAddress().toString() + " <<<");
other.getOutputStream().write("Blah blah".getBytes());
other.close();
listener.close();
} while (true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
===
Here a test class to test the tcp() function. It is the connect() statement that times out in case host != localhost.
TestTcpFunction.java:
import java.io.*;
import java.net.*;
public class TestTcpFunction {
public static void main(String[] args) {
String sendMessage = "Blah blah";
String host = (args.length==0)
? "localhost"
: "tcpbin.com";
String result = tcp(host, 4242, 30000, 30000, sendMessage);
System.out.println("result = " + result);
System.out.println("matches = " + result.equals(sendMessage));
}
public static String tcp(String hostName, Number port, Number connectionTimeOutMs, Number readTimeOutMs, String message) {
String errmsg = "";
try (
Socket socket = new Socket();
) {
Inet4Address address = (Inet4Address) Inet4Address.getByName(hostName);
System.out.println("trying to connect to:" + address.getHostAddress());
socket.connect(new InetSocketAddress(address, port.intValue()), connectionTimeOutMs.intValue()); // <<< times out if not localhost
socket.setSoTimeout(readTimeOutMs.intValue());
try (
PrintWriter out = new PrintWriter(socket.getOutputStream(), true); // autoflush
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
) {
out.print(message);
StringBuilder sb = new StringBuilder();
String line;
boolean addNewline = false;
while ((line = in.readLine()) != null) {
if (addNewline)
sb.append('\n');
sb.append(line);
if (line.lastIndexOf("</response>") >= 0)
break;
addNewline = true;
}
return sb.toString(); // The xml may not be well formed, for instance missing </response>
} finally {}
} catch (UnknownHostException e) {
errmsg = "Unknown host " + hostName;
} catch (SocketTimeoutException e) {
errmsg = "Socket connection timeout (before connection got established)";
} catch (SocketException e) {
errmsg = "Socket error: " + e.getMessage();
} catch (IOException e) {
errmsg = "Couldn't get I/O for the connection to " + hostName;
} catch (Exception e) {
errmsg = "Unknown socket error " + e.getMessage();
}
System.err.println(errmsg);
return "<Error> function tcp (Utils.java): " + errmsg + "</Error>";
}
}
===
Compile both with javac. Then start the server with java TcpServer.
Next run java TestTcpFunction in a different shell, without parameters.
The first time (with local host) it should work correctly.
Then run again, but with any parameter(s), like java TestTcpFunction 1
This time I get a timeout while trying to connect.
The code has been build and tested on my machine.
The client does not time out in connect. A simple output after connect shows that the connection is actually successfully:
socket.connect(new InetSocketAddress(address, port.intValue()), connectionTimeOutMs.intValue()); // <<< times out if not localhost
System.out.println("connected successfully");
Instead the program hangs while reading from the server. With the current code it will wait until the server closes the connection or has send a line with </response>. But the server tcpbin.com:4242 will not do anything like this. It will simply read anything and echo it back. To get a </response> string one actually has to send this string - which is not done.
Because of this the read will time out after a while based on the timeout set with socket.setSoTimeout. The resulting SocketTimeoutException is wrongly interpreted as connection timeout, but it is a read timeout.
Given that the code expects the echoed message to include the string </response> one must add it to the sent message:
String sendMessage = "Blah blah</response>";
This is still not enough though and a tcpdump shows that the message does not even get sent. This is because the expectation, that out.print(message); is affected by the autoflush is simply wrong - see I created a PrintWriter with autoflush on; why isn't it autoflushing?. Thus, one must explicitly flush the writer:
out.print(message);
out.flush();
tcpdump shows that the message is now actually send, but nothing is echoed back. This is because the echo server actually expects to read lines, but no line end was send yet. Adding it actually helps to send the message, get an echoed message back and break out of the loop:
String sendMessage = "Blah blah</response>\n";
And why did it work with localhost? Because the sample server did not actually behave like the echo server at tcpbin.com. It did not read anything but just sent a fixed message back and closed the connection.

java.net.ConnectException/java.net.ConnectException depending on normal run or debug mode

Hi am trying to send a binary message to a server with the IP and Port:
192.168.2.101:10001
The socket is reopened every 50 messages.
If I run the application normally I get a java.net.ConnectException in line 5, even though I can ping and telnet the server.
If I debug the application I get a java.net.SocketException at a different line (11), also sometimes the first message seems to get through without any errors.
private void sendMessage(String message, int relaisId, long timestamp) {
try {
if (connCount > 50) {
s = new Socket(ip, port); //RUN NORMALLY: java.net.ConnectException: Connection refused: connect
connCount=0;
}
outputStream = s.getOutputStream();
outputStream.write(message.getBytes());
outputStream.write(new byte[]{0});//DEBUG: java.net.SocketException: Connection reset by peer: socket write error
outputStream.flush();
connCount++;
} catch (UnknownHostException ex) {
logger.error("Host not found: " + ip + ":" + port, ex);
connCount=51;
retryMessage(message, relaisId, timestamp);// basically sleep 3s then call sendMessage
} catch (IOException ex) {
logger.error("Error at Relais No. " + relaisId + ": " + ip + ":" + port, ex);
connCount=51;
retryMessage(message, relaisId, timestamp); // basically sleep 3s then call sendMessage
} finally {
try {
if (connCount > 50 && s != null) {
s.close();
}
} catch (IOException ex) {
logger.error("IOException", ex);
}
}
}
Any help or analysis tools are very much appreciated :)
I found the solution. In my program different Threads send messages to external hardware components over given IPs and Ports.
This error occured because a Thread accidently got initiated twice with the same IP address and port, resulting in timing conflicts while using the same socket.
Even more strange was, that this error started to occur permanently while changing the server machine, before it was only there sporadically and we thought it was a noise in the network communication.
Hope this is a help to anyone in the future :)

Server not receiving data / Client not sending data

I am making a chat program. The problem occurs during the connection phase. Theoretically, the connection happens this way:
[Server] accept() incoming connection
[Client] attempt connection to the server
[Server & Client] initialize IO
[Client] send the username with a suffix indicating if there is or not a password following
[Server] receive username, check if there is an incoming password (if there is, read it too)
[Server] Check in database if client is already registered
[Server] ... (multiple checks of the client's account, etc...)
[Server] send an answer to the client if he is accepted or not
[Client] receive the answer and interpret it
Note that steps 6 and 7 are not yet implemented and the server skips to directly creating a new client account and accepting it in the network.
Now, something happens at either step 4 (if the error comes from the client not sending it's name) or at step 5 (if the error comes from the server not receiving the name), in either case, the server gets stuck waiting for the client's name and the client gets stuck waiting for the server's answer (step 9). I am asking your help in finding the cause of this bug and how to fix it.
Here is the code of the function used by the server to accept new connections:
public void establishConnection() {
Socket client = null;
BufferedReader input = null;
PrintWriter output = null;
String name = null;
String password = null;
try {
client = server.accept();
System.out.println("A connection is being established");
input = new BufferedReader(new InputStreamReader(client.getInputStream()));
output = new PrintWriter(client.getOutputStream());
//LOCATION OF THE BUG
//DESCRIPTION: If the name has been sent, the server seems not to receive it.
System.out.println("Waiting for the client's name...");
name = input.readLine();
if (name.charAt(name.length() - 1) == Keywords.PASSWORD) {
System.out.println("Waiting for the client's password...");
password = input.readLine();
} else
System.out.println("The client has no password");
name = name.substring(0, name.length() - 1);
System.out.println("Creation of the metaClient");
MetaClient metaClient = new MetaClient(name);
if (password != null)
metaClient.setPassword(password);
metaClient.setSocket(client);
System.out.println("Activating listening thread");
metaClient.activateCommunications(this, this);
/* If there is no already registered clients,
* the first to connect shall be the owner.
*/
if (clientsList.size() == 0)
metaClient.addRight(MetaClient.OWNER);
clientsList.add(metaClient);
output.write(Keywords.CONNECTION_ACCEPTED);
System.out.println("The connection process of user " + metaClient.getName()
+ " is complete.");
} catch (IOException e) {
System.out.println("Error when establishing a new connection");
e.printStackTrace();
} finally {
try {
if (input != null)
input.close();
if (output != null)
output.close();
} catch (IOException e) {
System.out.println("Error when closing the IO of the new connection.");
e.printStackTrace();
}
}
}
Here is the code of the builder of the client that tries to connect to the server:
public ChatClient(String ip, int port, String name, String password) throws ConnectException {
this.name = name;
boolean connectionRefused = false;
try {
System.out.println("Establishing connection to the server...");
server = new Socket(ip, port);
BufferedReader input = new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintWriter output = new PrintWriter(server.getOutputStream());
//LOCATION OF THE BUG:
//BUG DESCRIPTION: The name seems not to be sent to the server
System.out.println("Sending name to the server...");
if (password == null || password == "") {
name += Keywords.NO_PASSWORD;
output.println(name);
} else {
name += Keywords.PASSWORD;
output.println(name);
System.out.println("Sending password to the server...");
output.println(password);
}
System.out.println("Waiting for the server's response...");
//Wait for the server's response
String response = input.readLine();
if (response.equals(Keywords.CONNECTION_ACCEPTED))
System.out.println("The connection has been accepted");
else
connectionRefused = true;
} catch (IOException e) {
System.out.println("Error when connecting to the server");
e.printStackTrace();
System.exit(1);
}
if (connectionRefused)
throw new ConnectException("The connection was refused by the server");
else {
communication = new CommunicationProtocol(server, this, this);
communication.start();
}
}
Any help would be really much appreciated, it makes quite some time now that i'm trying to fix this to no avail.
Edit to answer Scary Wombat's comment:
Yes, one machine runs the server and another runs the client. But the error happens even when both are running as two separate programs on one machine.

Bluetooth Connectivity Issues in Post ICS Versions

I am able to connect to an external paired bluetooth hardware first time. After that if i repeat connect/disconect procedures sometimes getting exception.
Exception = read failed, socket might closed or timeout, read ret: -1
and after many trials able to connect again.Sometimes second trial itself is successful
The issue is observed with Devices:Nexux7(version 4.3) and MotoG(Kitkat)
Code for connection:
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(devAddress);
socket = device.createRfcommSocketToServiceRecord(MY_UUID);
and calling socket.connect() from asynctask
Code for closing socket:Subject
if (in != null) {
Logger.loge(CLASS_NAME + "." + "resetConnection", "in != null");
try {
in.close();
} catch (Exception e) {
// Log.d(TAG,"exception in closing inputstream - " + e.getMessage());
}
in = null;
}
if (out != null) {
Logger.loge(CLASS_NAME + "." + "resetConnection", "out != null");
try {
out.close();
} catch (Exception e) {
// Log.d(TAG,"exception in closing outputstream - " + e.getMessage());
}
out = null;
}
if (socket != null) {
Logger.loge(CLASS_NAME + "." + "resetConnection", "socket != null");
try {
socket.close();
} catch (Exception e) {
//Log.d(TAG,"exception in closing socket - " + e.getMessage());
}
socket = null;
}
I have followed the links
https://groups.google.com/forum/#!topic/android-developers/UxY5xME6V5s
Android Bluetooth: java.io.IOException: Service discovery failed
Disconnect a bluetooth socket in Android
android bluetooth can't connect
None of the solution provided help me to solve the issue.
Any help will be appreciated...
Thanks
I had encountered a similar problem when I built an app involving bluetooth connectivity. After searching for a long time, I found this solution.
The Actual problem is once both the device the is connected, the socket will be open. When either one of the socket is closed, other one is not closed.When you try to reconnect the other device ll not accept the new Socket.
The solution is when any one gets disconnect you need to re-initialize the connection service on both the side,ie you need to close the socket properly on both the side. and go back to the listen mode. then only the new socket connection will accept.click Here code reference using AndroiddChat example.

How do I detect server status in a port scanner java implementation

I am writing a port scanner in Java and I want to be able to distinct the following 4 use cases:
port is open
port is open and server banner was read
port is closed
server is not live
I have the following code:
InetAddress address = InetAddress.getByName("google.com");
int[] ports = new int[]{21, 22, 23, 80, 443};
for (int i = 0; i < ports.length; i++) {
int port = ports[i];
Socket socket = null;
try {
socket = new Socket(address, port);
socket.setSoTimeout(500);
System.out.println("port " + port + " open");
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line = reader.readLine();
if (line != null) {
System.out.println(line);
}
socket.close();
} catch (SocketTimeoutException ex) {
// port was open but nothing was read from input stream
ex.printStackTrace();
} catch (ConnectException ex) {
// port is closed
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null && !socket.isClosed()) {
try {
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
The problem is that I get a ConnectionException both when the port is closed and the server cannot be reached but with a different exception message:
java.net.ConnectException: Connection timed out: connect
when the connection was never established and
java.net.ConnectException: Connection refused: connect
when the port was closed
so I cannot make the distinction between the two use cases without digging into the actual exception message.
Same thing happens when I try a different approach for the socket creation. If I use:
socket = new Socket();
socket.setSoTimeout(500);
socket.connect(new InetSocketAddress(address, port), 1000);
I have the same problem but with the SocketTimeoutException instead. I get a
java.net.SocketTimeoutException: Read timed out
if port was open but there was no banner to be read and
java.net.SocketTimeoutException: connect timed out
if server is not live or port is closed.
Any ideas? Thanks in advance!
I don't think you have any options besides parsing the exception message. Is there a reason you'd rather not do this?
ConnectException only has an empty constructor and an errorMesssage constructor. There are no subclasses of ConnectException in the API. So I am afraid, Java does no proper way to access the reason. Parsing the exception message seems to be the only way, despite it being really messy with translations.

Categories