Sockets - No Outgoing / Only Incoming

R

rajatag

Hello,

Attached below is a code that runs one thread per client and each
client makes a separate connection to a server.

After sometime, even though the "write()" function of the client
thread writes data to System.out, the data does not seem to go to the
server.

Does this seem like a server issue or, something is wrong in the code
below?

Thanks,


///// InitClients.java ////
///////////////////////////

import java.io.*;
import java.util.*;

public class InitClients {
ClientThread clientThread[] = new ClientThread[20];

String serverIP;

Timer KeepConnectionOpenTimer = null;

Timer FlagTimer = null;

public InitClients() {

/// start as many clients as needed by calling the
clientThread[0] = new ClientThread("www.domain.com", 90000);
clientThread[1] = new ClientThread("www.domain.com", 90000);
clientThread[2] = new ClientThread("www.domain.com", 90000);
clientThread[3] = new ClientThread("www.domain.com", 90000);


// two timers. one at 5 minute intervals
// another at 30 second intervals

KeepConnectionOpenTimer = new Timer();
KeepConnectionOpenTimer.schedule(new ConnectionOpen(), 5 * 60 *
1000,
5 * 60 * 1000);
FlagTimer = new Timer();
FlagTimer.schedule(new FlagTask(), 30 * 1000, 1 * 1000);
}

class FlagTask extends TimerTask {
public void run() {
try {

for (int i = 0; i < clientThread.length; i++) {
if (clientThread != null)
clientThread.write("SENDFLAG"));
}
} catch (Exception e) {
e.printStackTrace();
}
}

}

class ConnectionOpen extends TimerTask {
public void run() {
try {
for (int i = 0; i < clientThread.length; i++) {
if (clientThread != null)
clientThread.write("CONNECTOPEN"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

public static void main(String[] arguments) throws IOException {
new InitClients();
}
}


///// ClientThread.java ////
////////////////////////////

import java.io.*;
import java.net.Socket;
import java.util.*;

public class ClientThread implements Runnable {

Socket socket = null;

BufferedReader fromServer;

protected Thread thread;

BufferedWriter out;

public ClientThread(byte index, String server, int port) {
thread = new Thread(this);
try {
socket = new Socket(server, port);
socket.setKeepAlive(true);

fromServer = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream()));
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}

private String readline() {
try {
return this.fromServer.readLine();
} catch (IOException e) {
return null;
}
}

public void run() {
try {
inputRun();
if (socket != null && !socket.isClosed()) {
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public boolean write(String s) {
try {
out.write(s + "\r\n");
out.flush();

// DATA IS ALWAYS WRITTEN HERE!
// EVEN IF IT DOES NOT GO TO SERVER
System.out.println(System.currentTimeMillis() + ":OU: " + s);
return true;
} catch (Exception e) {
e.printStackTrace();
close();
return false;
}
}

public void close() {
try {
fromServer.close();
out.close();
socket.close();
} catch (Exception e) {

}
}

public void inputRun() {
String s = null;
while (((s = readline()) != null)) {
// process incoming here
}
}
}
 
R

rajatag

As a continuation of my message, I have logged TCP packets on the
server using TCPDUMP and it does not receive communication from the
client. So there does seem to be a problem in the code.

Thanks!
 
G

Gordon Beaton

After sometime, even though the "write()" function of the client
thread writes data to System.out, the data does not seem to go to
the server.

What server are you communicating with?

Have you used a tool like Wireshark to determine whether or not
anything is actually sent?
clientThread[0] = new ClientThread("www.domain.com", 90000);

This is probably not relevant to your current problem, but TCP port
numbers do not go higher than 65535.

/gordon
 
C

Chris Uppal

rajatag said:
Attached below is a code that runs one thread per client and each
client makes a separate connection to a server.

After sometime, even though the "write()" function of the client
thread writes data to System.out, the data does not seem to go to the
server.

Does this seem like a server issue or, something is wrong in the code
below?

It works for me. I fixed up the small typo in your code and it has been
running happily for about half an hour now -- data is still being written to
the network as expected.

Your test code lacks some necessary synchronisation (presumably because it /is/
test code), but I doubt whether that's affecting this test, and I don't see
anything other than that which might cause this kind of problem. (You still
have the buffering wrong, BTW, but that won't cause these problems either.)

It would sound like a server problem to me except that I can't imagine any kind
of server problem which would prevent a /client/ from sending without causing
the client to block eventually.

Two things I'd try. Make ClientThread.write() synchronised, just in case it is
affecting the test. Add some tracing so that you never close any stream or
socket without logging it -- it may show up something... if you're lucky ;-)

-- chris
 
R

rajatag

Two things I'd try. Make ClientThread.write() synchronised, just in case it is
affecting the test. Add some tracing so that you never close any stream or
socket without logging it -- it may show up something... if you're lucky ;-)

-- chris

I'll give these two options a try and get back with results.

Regards,
Rajat
 
R

rajatag

I'll give these two options a try and get back with results.
Regards,
Rajat

Hi,

Here is the stack trace that is generated when the socket closes:

java.net.SocketException: Connection timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown
Source)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)

Why would the socket time out even though data is being read on a
continuous basis from the socket?

Regards,
Rajat
 
G

Gordon Beaton

Here is the stack trace that is generated when the socket closes:

java.net.SocketException: Connection timed out
at java.net.SocketInputStream.socketRead0(Native Method)

It would help if you mentioned exactly where in the posted code this
exception occurs.

My guess (and it really is just a guess) is that there are firewall
issues here.

When you used Wireshark, did you run it on the *client* host?

/gordon
 
R

rajatag

It would help if you mentioned exactly where in the posted code this
exception occurs.

The exception is generated here:
private String readline() {
try {
return this.fromServer.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
My guess (and it really is just a guess) is that there are firewall
issues here.

The server admin claims that there is no firewall in between, although
I have been thinking about this for sometime too ...
When you used Wireshark, did you run it on the *client* host?

I have TCP logs of both client and the server. However, I am unable to
find anything extraordinary? Can you give a pointer as to what would
be present if it was a firewall issue?

Thanks,
Rajat
 
G

Gordon Beaton

I have TCP logs of both client and the server. However, I am unable
to find anything extraordinary? Can you give a pointer as to what
would be present if it was a firewall issue?

Not sure, but you might try to rule out potential firewall issues by
running against a different (local) server, like Chris seems to have
done.

/gordon
 
E

Esmond Pitt

rajatag said:
Here is the stack trace that is generated when the socket closes:

java.net.SocketException: Connection timed out
at java.net.SocketInputStream.socketRead0(Native Method)

Are you sure it was exactly that? It should have been "Read timed out",
not "Connection timed out". What platform?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top