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

Java & C TCP problem

0 views
Skip to first unread message

gary

unread,
Jul 29, 2003, 8:01:14 PM7/29/03
to
Hi,

I'm working on a project which involves communicating betweem X embedded
devices (clients), with embedded TCP/IP firmware and a PC Server over
Ethernet. The clients firmware and their application are built with an
ANSI C Compiler.

I've been investigating the use of J2SE for the software on the Server
side as I'm familiar with the language. However, this is with Java
applications at both ends of the connection and not C and Java at
opposite ends.

Anyway, with the Java Server app running as soon as a client attempts to
connect I getting the following exception:

garjoh@linux:~/PROJECT_JAVA> java Server
Server: Socket Error ** caught by my application **
java.net.SocketException: Connection reset by peer: Connection reset by peer
at java.net.SocketInputStream.socketRead(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:90)
at java.net.SocketInputStream.read(SocketInputStream.java:112)
at java.io.DataInputStream.readByte(DataInputStream.java:222)
at Server.runServer(Server.java:85)
at Server.main(Server.java:136)

A snippet of the server code is as follows (a client sends a byte
representing some action and the Server responds with an "OK" message).

try
{
connection = new ServerSocket(LOCAL_PORT_NUM, 5,
InetAddress.getLocalHost() );

while (true)
{
Socket clientConnect = connection.accept();

DataInputStream input = new
DataInputStream(clientConnect.getInputStream());
DataOutputStream output = new
DataOutputStream(clientConnect.getOutputStream());

int count = 99999;

while (true)
{
String numOptions = "";
int val = input.readByte() - '@';

numOptions += String.valueOf(val);

displayArea.append(numOptions + "\n");
output.writeUTF("OK");

count--;

if (count == 0) break;
}
input.close();
output.close();
clientConnect.close();
connection.close();
break;
}

}
catch (IOException ioe)
{
System.out.println("Server: Socket Error");
ioe.printStackTrace();
}


I was wondering what the "Connection reset by peer" meant and how I
would go about fixing this to get the desired behaviour?

I tried it with a Java PC Client and it worked as expected. I wrote an
equivalent C PC client and got the above exception and something similar
with the embedded client.


Thanks

Roger Lindsjö

unread,
Jul 30, 2003, 9:29:24 AM7/30/03
to
On Wed, 30 Jul 2003 02:01:14 +0200, gary wrote:

Does it fail on the first or the second read?

Are you sure you want to use writeUTF? writeUTF("OK") will generate the
following bytes: 0x00 0x02 0x4F 0x4B

//Roger

Steve Horsley

unread,
Jul 30, 2003, 10:50:21 AM7/30/03
to
On Wed, 30 Jul 2003 00:01:14 +0000, gary wrote:

<snip>

> I was wondering what the "Connection reset by peer" meant and how I
> would go about fixing this to get the desired behaviour?

"Connection reset by peer" means that the entity at the other end of the
connection closed it - "Click, Brrrrr". I guess the remote box doesn't
like something that you are doing.

I would make a guess that you haven't fully understood the javadocs for
DataOutputStream.writeUTF. writeUTF sends a 2-byte length indication
before sending the string itself so that output.writeUTF("OK") sends the
4-byte sequence {0x00, 0x02, 'O', 'K'}. I guess the C code doesn't expect
this response. Better to send the explicit byte values that the C code
expects:
output.write("OK".getBytes("ASCII"));

Also, check that the C code isn't expecting a zero byte after the 'K'. C
often uses zero-terminated strings.

Steve

0 new messages