Hi I am writing a simple java program to read data from sockets, but caught in a problem, where after each input I get lots of spaces present.
AIM: Write a simple server socket which can read CORRECT data from client socket.
So far: I have been able to write the code to read from socket, even able to read the data, but the thing is I get lots of spaces at the end.
So I had to use trim(), in the end to manage spaces, but I still don't know if this is right or wrong.
Would appreciate the inputs on this.
NOTE: I am using windows7 telnet service to connect to socket.
readSocket() {
System.out.println(client_socket);
try {
System.out.println("reading socket");
/*BufferedReader brIn = new BufferedReader(new InputStreamReader(client_socket.getInputStream()));
String inputLine = null;
while ((inputLine = brIn.readLine()) != null) {
System.out.println(inputLine);
}*/
InputStream is = client_socket.getInputStream();
byte[] byteArr = new byte[1024];
int inputsize = -1;
int currentPos = 0;
StringBuffer sb = new StringBuffer(11111);
/*while((inputsize = is.read(byteArr)) != -1) {
String processed = new String(byteArr);
sb.append(processed);
}*/
int BUFFER_SIZE = 1024;
int read;
byte[] buffer = new byte[BUFFER_SIZE];
String processed = "";
while ((read = is.read(byteArr, 0, BUFFER_SIZE)) != -1) {
String current = new String(byteArr);
//os.write(buffer, 0, read);
System.out.println("current Process "+current);
//processed +=current;
sb.append(current.toString().trim());
}
System.out.println("Socket input is : "+sb.toString());
System.out.println("Sending response to client "+processed.toString());
//client_socket.getOutputStream().write(sb.toString().getBytes());
//client_socket.getOutputStream().close();
FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\himanshu2100\\eee.txt"));
fos.write(processed.getBytes());
fos.close();
is.close();
client_socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Well using the suggestion of both Roger Lindsjö and mprivat, I redesigned the part where I read from stream.
readSocket() {
System.out.println(client_socket);
try {
System.out.println("reading socket");
InputStream is = client_socket.getInputStream();
byte[] byteArr = new byte[1024];
int read;
int BUFFER_SIZE = 1024;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((read = is.read(byteArr, 0, BUFFER_SIZE)) != -1) {
baos.write(byteArr, 0, read); //This is an optimized design, instead of having so many strings
}
System.out.println("Output is :"+new String(baos.toByteArray()));
baos.close();
is.close();
client_socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I hope this one's a better solution so posting it. Suggestions welcome.
When you read into an array the whole array might not be filled. The read variable will contain the number of actual bytes read.
while ((read = is.read(byteArr, 0, BUFFER_SIZE)) != -1) {
String current = new String(byteArr, 0, read);
System.out.println("current Process "+current);
sb.append(current);
}
If you use an encoding which is multi byte the above might not work as each part of the character could be in separate reads, this creating invalid sequences.