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.
Related
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.
I am trying to make a proxy server in java and I was able to make a working proxy which handles http requests properly. After searching a lot I was also able to extend my program for https requests by following this answer to a similar question: https://stackoverflow.com/a/9389125/5309299
Here's my code after a TCP connection is established between client and proxy:
String request = "";
byte[] requestByteArr;
//read the complete request
while(true){
String requestLine = bufferedReaderFromClient.readLine() + "\r\n";
if (requestLine.trim().length()==0 && !request.equals("")){
request+=requestLine;
requestByteArr = request.getBytes();
System.out.println(request);
break;
} else {
request+=requestLine;
}
}
String hostname = getHostFromRequest(request);
int remoteport = getRemotePortFromRequest(request);
if (request.startsWith("CONNECT")){
//establish connection between host and proxy
final Socket hostSocket = new Socket(hostname, remoteport);
//tell client that connection was successful
String statusLine = "HTTP/1.1 200 Connection established \n" + "Proxy-agent: ProxyServer/1.0\n" + "\r\n";
outToClient.write(statusLine.getBytes());
outToClient.flush();
//new thread to handle incoming responses from host
new Thread(){
public void run(){
try{
InputStream inFromHost = hostSocket.getInputStream();
while(true){
byte[] bufread = new byte[128];
int bytes_received;
while ((bytes_received = inFromHost.read(bufread)) > 0){
outToClient.write(bufread, 0, bytes_received);
outToClient.flush();
}
}
} catch (IOException e){
e.printStackTrace();
}
}
}.start();
//main thread handles incoming requests from client
OutputStream outToHost = hostSocket.getOutputStream();
while (true){
byte[] bufread = new byte[128];
int bytes_received;
while ((bytes_received = inFromClient.read(bufread)) > 0){
outToHost.write(bufread, 0, bytes_received);
outToHost.flush();
}
}
}
Obviously, this only works for one host, i.e. when a client (e.g. chrome browser) sends a CONNECT request for one host (e.g. "www.google.com:443"). I want my client to be able to connect with multiple hosts. The problem is that since all the requests that come after CONNECT request are encrypted, my proxy server will not be able to determine which request is meant for which host, so it cannot forward the requests.
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.
I got to implement a chat in my application. Connection to a server is made using sockets. I should register to that server and the server will aknowledge that with a reply.
I have implemented this in a single method where I send the command using a BufferedWriter, and then start reading from the input stream until it tells me there is no more data.
I read properly the server reply. However, I never get the negative value from the second in.read call and thus my method stays blocked in the while loop (in the conditionnal statement where I make that call).
How should this be done with sockets? I usually do that with files or other input streams without problem.
If I should read only the bytes I am supposed to read, does that mean that I either have to:
Know in advance the length of the server response?
or make the server send a code to notify it has finished to send its response?
Currently I am doing the following:
private String sendSocketRequest(String request, boolean skipResponse) throws ChatException {
if (!isConnected()) openConnection();
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()), 2048);
out.append(request);
out.flush();
out = null;
} catch (IOException e) {
LogHelper.error("Unable to send socket request: " + request, e);
throw new ChatException("Unable to send socket request: " + request, e);
}
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()), 2048);
StringBuffer response = new StringBuffer();
char[] buffer = new char[2048];
int charsRead = -1;
// >>>>>>>> This is where it gets blocked <<<<<<<<<
while ((charsRead = in.read(buffer)) >= 0) {
if (charsRead > 0) response.append(new String(buffer, 0, charsRead));
}
return response.toString();
} catch (IOException e) {
LogHelper.error("Unable to read socket response: " + request, e);
throw new ChatException("Unable to read socket response: " + request, e);
}
}
Connection to the server is made with the following method:
public synchronized void openConnection() throws ChatException {
try {
socket = new Socket(Constants.API_CHAT_SERVER_ADDRESS, Constants.API_CHAT_SERVER_PORT);
socket.setKeepAlive(true);
LogHelper.debug("CHAT >> Connected to the chat server: " + Constants.API_CHAT_SERVER_ADDRESS);
} catch (UnknownHostException e) {
LogHelper.error("Unable to open chat connection", e);
throw new ChatException("Unable to open chat connection", e);
} catch (IOException e) {
LogHelper.error("Unable to open chat connection", e);
throw new ChatException("Unable to open chat connection", e);
}
}
The amount of data to be sent/received over a socket based connection is protocol dependend and not known to the TCP/IP stack, but only to the application layer.
The protocol used is developer dependend ... ;-) so coming to your questions:
If I should read only the bytes I am supposed to read, does that mean that I either have to:
Know in advance the length of the server response?
Yes, this is one possibility.
or make the server send a code to notify it has finished to send its response?
Also yes, as this is another possibility. Common markers are \n or \r\n. The NUL/'\0' character also might make sense.
A third option is to prefix each data chunk with a constant number of bytes describing the amount of bytes to come.
Instead of dealing with bytes, maybe it's simpler handling instances of ad-hoc classes, like - for instance - a Message class:
The server:
// Streams
protected ObjectInputStream fromBuffer = null;
protected ObjectOutputStream toBuffer = null;
// Listening for a new connection
ServerSocket serverConn = new ServerSocket(TCP_PORT);
socket = serverConn.accept();
toBuffer = new ObjectOutputStream(socket.getOutputStream());
fromBuffer = new ObjectInputStream(socket.getInputStream());
// Receiving a new Message object
Message data = (Message)fromBuffer.readObject();
The client then sends a message by simply:
// Sending a message
Message data = new Message("Hello");
toBuffer.writeObject(data);
Message can be as complex as needed as long as its members implement Serializable interface.
I have a server/client chat room. When the client connects to the server, and sends a message, the client program prints out the message that it sent, but on another client program that is connected to the server, it does not print out until the user presses the 'enter' key.
On the client side:
try {
//Strings to hold messages in and out:
String userinput, serverinput;
//Getting input from the user:
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
//Messages from the server will be printed to the console, messages from console will be sent to the socket:
while(true) {
userinput = input.readLine();
serverout.println(username + "> " + userinput);
serverinput = clientin.readLine();
System.out.println(serverinput);
}
}
On the server side:
public void run() {
PrintWriter output = null;
BufferedReader input = null;
String message;
SchoolRoomServer server = new SchoolRoomServer();;
try {
//i/o for clients:
output = new PrintWriter(socket.getOutputStream());
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException ioe) {
System.err.println(ioe);
System.exit(1);
}
try {
while((message = input.readLine()) != null) {
server.showAll(message, output);
}
} catch (IOException ioe) {
System.err.println(ioe);
System.err.println("Damn errors.");
System.exit(1);
}
}
By the way- the server.showAll(message, output); method is this:
public void showAll(String msg, PrintWriter printwriter) {
for(int i = 0; i < listWriters.size(); i++) {
if(listWriters.get(i) != printwriter) {
listWriters.get(i).println(msg);
}
}
}
listWriters is an Arraylist of PrintWriters, which gets a PrintWriter associated with a client each time a new thread is made for that client.
So: any ideas on how to immediately print out messages?
Thank you for any help.
You are having the same problem as here. System.in doesn't return anything until the user pressed enter. Then you get the whole line to read.
Read on Socket & ServerSocket
Basically you need to establish a client socket which connects to a server socket then you need to pass the i/o streams between the two to print messages from the client to the server & vice-versa.