Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sockets - No Outgoing / Only Incoming

0 views
Skip to first unread message

rajatag

unread,
Feb 26, 2007, 7:13:57 AM2/26/07
to
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[i] != null)
clientThread[i].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[i] != null)
clientThread[i].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
}
}
}

rajatag

unread,
Feb 26, 2007, 7:55:29 AM2/26/07
to
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!

Gordon Beaton

unread,
Feb 26, 2007, 8:03:05 AM2/26/07
to
On 26 Feb 2007 04:13:57 -0800, rajatag wrote:
> 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

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e

rajatag

unread,
Feb 26, 2007, 8:20:56 AM2/26/07
to
> What server are you communicating with?

It is our own server that runs non blocking sockets.

> Have you used a tool like Wireshark to determine whether or not
> anything is actually sent?

Yes, I analyzed the packets with Wireshark and the data is not
transmitted to the server.

> > clientThread[0] = new ClientThread("www.domain.com", 90000);
>

:) Realized after posting


Thanks!

Chris Uppal

unread,
Feb 26, 2007, 12:54:35 PM2/26/07
to
rajatag wrote:

> 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


rajatag

unread,
Feb 26, 2007, 1:06:17 PM2/26/07
to
> 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

rajatag

unread,
Feb 27, 2007, 2:40:12 AM2/27/07
to
> 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

Gordon Beaton

unread,
Feb 27, 2007, 7:46:59 AM2/27/07
to
On 26 Feb 2007 23:40:12 -0800, rajatag wrote:
> 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?

rajatag

unread,
Feb 27, 2007, 8:36:32 AM2/27/07
to
> 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

Gordon Beaton

unread,
Feb 27, 2007, 9:37:39 AM2/27/07
to
On 27 Feb 2007 05:36:32 -0800, rajatag wrote:
> 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.

Esmond Pitt

unread,
Feb 27, 2007, 10:28:10 PM2/27/07
to
rajatag wrote:
>
> 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?

0 new messages