Hi all.
I am having problems with a school work. Basically I am making a network program as a school project, but I have trouble getting the network part to work. The issue comes when I try to send a byte array to the client (the program will work by exchanging byte arrays). The server accepts the connection from the client, sends the response, then closes the stream and dispose of the client handler. The client connects to the server, and is then supposed to read the data the server sends before it shuts down. However, no data is read. There is no exception, no hint on what it is wrong, it is like it just decides to ignore the data and pretend like there is no issue. I can make it work, if I send a int instead of a byte array, but I need to send a byte array.
This is my client code:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
/**
*
* @author anders
*/
public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
try
{
InputStream cin = new BufferedInputStream( (new Socket(InetAddress.getLocalHost(), 9011)).getInputStream() );
byte[] b = new byte[ cin.available() ];
cin.read( b );
cin.close();
System.out.println("It worked!!! Data recieved: " + b.length);
}
catch (IOException ioe)
{
System.err.println("ERROR: " + ioe.getMessage() + "n");
}
}
}
And this is the important server code (it has grown, so I cannot post all the code):
/**
* Run the client code
*/
public void run()
{
/**
* Handle exception
*/
try
{
/**
* Send the map list
*/
OutputStream b = new BufferedOutputStream( mSocket.getOutputStream() );
byte[] toSend = new byte[ 2 ];
toSend[ 0 ] = 2;
toSend[ 1 ] = 89;
b.write( toSend );
/**
* Close the connection
*/
b.close();
}
catch (IOException ioe)
{
System.err.println("ERROR: " + ioe.getMessage() + "n");
}
finally
{
mAlive = false;
mCon.notifyUpdate();
System.out.println("Terminating thread handling client " + mSocket.getInetAddress().getHostName());
}
}
(There are no exceptions thrown in the code above, it just seems to work well.)
Anyone has a clue of what is wrong?
Take care,
Mr Z
EDIT:
Managed to solve it by using ObjectOutputStream instead. But I am still confused why it did not work to begin with

.
"Operator! Give me the number for 911!"
- Homer J. Simpson