Hello,
Hopefully, this is the right mailing list for my problems, if not please
let me know which one is the better list for my request.
I am using:
gcj 4.0.2
build with crosstool with glibc-2.3.2
for an embedded ARM Box with Linux 2.6.14
I had some deadlock problems on a big application and couldn't find the
error, so I started working on a small test program.
This program is just a (very) easy application, which exchanges data
between a TCP Client and Server. The error is:
This data sharing works for several times, but after exchanging some
packets (might be 3 or even a thousand), the client stops before calling
or inside the method socket.write(...).
For better understanding, I have attached the code.
Thank you for your help
Michael
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
public class Client extends Thread {
private static String str_data = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private int in_port;
public Client( int in_serverport ) {
in_port = in_serverport;
this.start();
}
public void run() {
while ( true ) {
Socket ds_socket = null;
try {
ds_socket = new Socket("localhost", in_port);
DataInputStream in = new DataInputStream ( ds_socket.getInputStream());
DataOutputStream out = new DataOutputStream( ds_socket.getOutputStream());
//-------------------------------------------------------------
// sending data
//-------------------------------------------------------------
if ( start.bo_printmess ) {
System.out.println("CL - write start");
System.out.flush();
}
out.writeUTF(str_data);
out.flush();
if ( start.bo_printmess ) {
System.out.println("CL - write ready");
System.out.flush();
}
if ( start.bo_useyield ) {
Thread.yield();
}
//-------------------------------------------------------------
// receive data
//-------------------------------------------------------------
if ( start.bo_printmess ) {
System.out.println("CL - read start");
System.out.flush();
}
String str_data = in.readUTF();
if ( start.bo_printmess ) {
System.out.println("CL - read ready");
System.out.flush();
}
if ( start.bo_useyield ) {
Thread.yield();
}
if ( start.bo_countruns ) {
start.m_count( "CL - count " );
}
//-------------------------------------------------------------
// print length of received data
//-------------------------------------------------------------
if ( start.bo_printrec ) {
System.out.println("CL - received " + str_data.length() + " bytes" );
System.out.flush();
}
//-------------------------------------------------------------
// close socket
//-------------------------------------------------------------
if ( start.bo_printmess ) {
System.out.println("CL - close start");
System.out.flush();
}
in.close();
out.close();
ds_socket.close();
ds_socket = null;
if ( start.bo_useyield ) {
Thread.yield();
}
if ( start.bo_printmess ) {
System.out.println("CL - close ready");
System.out.flush();
}
} catch ( Throwable e ) {
System.out.println("CL - ");
e.printStackTrace();
System.out.flush();
}
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends Thread {
private ServerSocket ds_serversocket;
public Server ( int in_serverport ) {
try {
ds_serversocket = new ServerSocket(in_serverport);
this.start();
} catch( Throwable ds_e ) {
System.out.println(ds_e.getMessage());
System.out.flush();
}
}
public void run(){
m_run();
}
private void m_run() {
try {
Socket ds_socket = null;
while ( true ) {
//-------------------------------------------------------------
// wait for a connection
//-------------------------------------------------------------
if ( start.bo_printmess ) {
System.out.println("SV - accept start");
System.out.flush();
}
ds_socket = ds_serversocket.accept();
if ( start.bo_printmess ) {
System.out.println("SV - accept ready");
System.out.flush();
}
if ( start.bo_countruns ) {
start.m_count( "SV - count " );
}
m_server( ds_socket );
//-------------------------------------------------------------
// close socket
//-------------------------------------------------------------
if ( start.bo_printmess ) {
System.out.println("SV - close start");
System.out.flush();
}
ds_socket.close();
ds_socket = null;
if ( start.bo_printmess ) {
System.out.println("SV - close ready");
System.out.flush();
}
//-------------------------------------------------------------
// print memory overview
//-------------------------------------------------------------
if ( start.bo_printmem ) {
System.out.println( "Free mem: " + Runtime.getRuntime().freeMemory() );
System.out.println( "Total mem: " + Runtime.getRuntime().totalMemory() );
}
//-------------------------------------------------------------
// call garbage collector
//-------------------------------------------------------------
if ( start.bo_usegc ) {
System.out.println("GC - start");
System.out.flush();
System.gc();
System.out.println("GC - ready");
System.out.flush();
}
//-------------------------------------------------------------
// print memory overview
//-------------------------------------------------------------
if ( start.bo_printmem ) {
System.out.println( "Free mem: " + Runtime.getRuntime().freeMemory() );
System.out.println( "Total mem: " + Runtime.getRuntime().totalMemory() );
}
}
} catch ( Throwable e ){
System.out.println( e.getMessage() );
System.out.flush();
}
}
private void m_server( Socket ds_socket ) {
try {
DataInputStream ds_in = new DataInputStream ( ds_socket.getInputStream() );
DataOutputStream ds_out = new DataOutputStream( ds_socket.getOutputStream() );
//-------------------------------------------------------------
// receive data
//-------------------------------------------------------------
if ( start.bo_printmess ) {
System.out.println("SV - read start");
System.out.flush();
}
String str_data = ds_in.readUTF();
if ( start.bo_printmess ) {
System.out.println("SV - read ready");
System.out.flush();
}
if ( start.bo_useyield ) {
Thread.yield();
}
//-------------------------------------------------------------
// print length of received data
//-------------------------------------------------------------
if ( start.bo_printrec ) {
System.out.println("SV - received " + str_data.length() + " bytes" );
System.out.flush();
}
//-------------------------------------------------------------
// sending data back:
//-------------------------------------------------------------
if ( start.bo_printmess ) {
System.out.println("SV - write start");
System.out.flush();
}
ds_out.writeUTF(str_data);
ds_out.flush();
if ( start.bo_printmess ) {
System.out.println("SV - write ready");
System.out.flush();
}
if ( start.bo_useyield ) {
Thread.yield();
}
} catch ( Throwable e ){
System.out.println("SV - Throwable: "+e.getMessage());
System.out.flush();
}
}
}
public class start {
private static int in_serverport = 12345;
private static long in_counter;
public static boolean bo_useyield = true; // use Thread.yield() after send and receive
public static boolean bo_usegc = false; // call garbage collector explicit
public static boolean bo_printmem = false; // print a memory overview
public static boolean bo_printmess = true; // print message before send and receive
public static boolean bo_printrec = false; // print length of received data
public static boolean bo_countruns = false; // count runs
public static void main(String[] args) {
in_counter = 0;
System.out.println( "Configuration:");
System.out.println( "==============");
System.out.println( " Use yield: " + (bo_useyield ? "yes" : "no") );
System.out.println( " Call GC explicit: " + (bo_usegc ? "yes" : "no") );
System.out.println( " Print Memory overview: " + (bo_printmem ? "yes" : "no") );
System.out.println( " Print send/rec Messages: " + (bo_printmess ? "yes" : "no") );
System.out.println( " Print length rec data: " + (bo_printrec ? "yes" : "no") );
System.out.println( " Count runs: " + (bo_countruns ? "yes" : "no") );
System.out.println( "" );
System.out.println( "" );
System.out.flush();
// start Server:
new Server( in_serverport );
// start client:
new Client(in_serverport);
}
public static void m_count( String str_prefix ) {
System.out.println( str_prefix + in_counter );
System.out.flush();
in_counter++;
}
}