P
Phillip D Ferguson
Hello,
i'm beginning to get familiar with the network side of java, and as a quick
test platform for a project i wanted to send an array of bytes from the
client to a server. The server then responds with a string to say that the
array has been received correctly.
My main problem is with what inputstream to attached to the socket. I'm
finding it difficult to understand exactly what each read() command is
capable of.
I am able to send the array across with out too much bother. I do appreciate
my code is very basic, and probably not good, so any advice to change it
would be great.
I cannot reply with a string...... i don't understand why? can someone help?
At the moment the client code hangs on the readline()
The client code :
=============================================================================
public static void main(String argv[]) throws Exception
{
String response;
byte[] testArr = new byte[8];
for (int i=0; i<8;i++)
{
testArr= (byte)(i+1);
}
for (int j=0; j<8;j++)
{
System.out.print(testArr[j]);
}
// testing byte array contents
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket(hostname and port number);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
outToServer.write(testArr,0,8); // i realise this is not good so any
help here would be grand!
System.out.println(testArr);
response = inFromServer.readLine(); // hangs on this line
System.out.println("Server says:" + response);
clientSocket.close();
}
}
==============================================================================
Server code:
==============================================================================
public static void main(String argv[]) throws Exception
{
String responsetoClient;
int byteread;
byte[] RtestArr = new byte[8];
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
DataInputStream inFromClient =
new DataInputStream(new
DataInputStream(connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
byteread = inFromClient.read(RtestArr,0,8);
for (int j=0; j<8;j++)
{
System.out.print(RtestArr[j]);
}
if (byteread == 8) //
{
responsetoClient = "Array confirmed";
outToClient.writeBytes(responsetoClient);
}
}
}
}
====================================================================================
i'm at a loss as to why this isn't work.... any suggestions and criticisms?
I need to learn....
cheers
Phill
i'm beginning to get familiar with the network side of java, and as a quick
test platform for a project i wanted to send an array of bytes from the
client to a server. The server then responds with a string to say that the
array has been received correctly.
My main problem is with what inputstream to attached to the socket. I'm
finding it difficult to understand exactly what each read() command is
capable of.
I am able to send the array across with out too much bother. I do appreciate
my code is very basic, and probably not good, so any advice to change it
would be great.
I cannot reply with a string...... i don't understand why? can someone help?
At the moment the client code hangs on the readline()
The client code :
=============================================================================
public static void main(String argv[]) throws Exception
{
String response;
byte[] testArr = new byte[8];
for (int i=0; i<8;i++)
{
testArr= (byte)(i+1);
}
for (int j=0; j<8;j++)
{
System.out.print(testArr[j]);
}
// testing byte array contents
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket(hostname and port number);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
outToServer.write(testArr,0,8); // i realise this is not good so any
help here would be grand!
System.out.println(testArr);
response = inFromServer.readLine(); // hangs on this line
System.out.println("Server says:" + response);
clientSocket.close();
}
}
==============================================================================
Server code:
==============================================================================
public static void main(String argv[]) throws Exception
{
String responsetoClient;
int byteread;
byte[] RtestArr = new byte[8];
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
DataInputStream inFromClient =
new DataInputStream(new
DataInputStream(connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
byteread = inFromClient.read(RtestArr,0,8);
for (int j=0; j<8;j++)
{
System.out.print(RtestArr[j]);
}
if (byteread == 8) //
{
responsetoClient = "Array confirmed";
outToClient.writeBytes(responsetoClient);
}
}
}
}
====================================================================================
i'm at a loss as to why this isn't work.... any suggestions and criticisms?
I need to learn....
cheers
Phill