As I'm learning about networking and io in Java, I'm slowly building client/server apps to apply what I'm reading on different tutorials. I'm stumped though, and I've been trying to figure out why my code isn't working for a long time. So I decided to turn to SO's infinite wisdom :)
After accepting the client socket connection from localhost, I have this on my server:
BufferedInputStream in = new BufferedInputStream(socket.getInputStream(),Config.BUFFER_SIZE_NET);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream(),Config.BUFFER_SIZE_NET);
String msg = "";
byte buffer[] = new byte[Config.BUFFER_SIZE_READ];
int bytesRead;
System.out.println("Server is waiting for data");
while ((bytesRead = in.read(buffer)) > 0) {
msg = msg + new String(buffer,Config.CHARSET);
}
System.out.println("Server received: "+msg);
After connecting to the server, this is executed on the client after I press a JButton:
BufferedInputStream in = new BufferedInputStream(socket.getInputStream(),Config.BUFFER_SIZE_NET);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream(),Config.BUFFER_SIZE_NET);
String msg = "msg";
try{
out.write(msg.getBytes(Config.CHARSET));
out.flush();
System.out.println("Client sent: "+msg);
}catch(Throwable e){e.printStackTrace();}
After pressing the button on the client, I get this output:
Client sent: msg
On the server side, I get:
Server is waiting for data
If I debug the server, I see it blocked forever on the following line:
while ((bytesRead = in.read(buffer)) > 0) {
No exceptions are thrown. What am I missing here? I had it working before but I've done many changes and now I can't get it back to work.
Note: This is a slightly modified version of the actual code, to make it easier to be reviewed. If you think there's something relevant missing, let me know!
I got out of the hole I dug myself into. It turns out I wasn't checking for the end on the data being received, so the code was stuck either blocking for reads, or in the loop that followed. By checking for a specific sequence of bytes in the data read by the server, inside the while loop, I was able to identify the end of the data and break from the loop.
I don't know if this is the (most) correct answer to the problem above but I solved it in this way, so I'm submitting it as an answer.
Related
I have Service which records video from back camera:
this.mMediaRecorder = new MediaRecorder();
this.mMediaRecorder.setCamera(mCamera);
this.mMediaRecorder.setVideoSource(VideoSource.CAMERA);
this.mMediaRecorder.setOutputFormat(OutputFormat.DEFAULT);
this.mMediaRecorder.setVideoEncoder(VideoEncoder.DEFAULT);
this.mMediaRecorder.setVideoSize(photo_resolution[0], photo_resolution[1]);
this.mParcelFileDescriptor = ParcelFileDescriptor.fromSocket(this.mSocket);
this.mMediaRecorder.setOutputFile(this.mParcelFileDescriptor.getFileDescriptor());
this.mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
this.mMediaRecorder.prepare();
this.mMediaRecorder.start();
Video doesn't have a sound. I connecting to the server with following code:
this.mSocket = new Socket();
this.mSocket.connect(new InetSocketAddress(...), 30000);
this.in = new BufferedReader(new InputStreamReader(this.mSocket.getInputStream(), "UTF-8"));
this.out = this.mSocket.getOutputStream();
this.out.write(("some string" + "\n").getBytes("UTF-8"));
this.out.flush();
...
Every needed permissions I have already added to AndroidManifest.xml.
The problem is that video isn't broadcasted to server. I tried to replace this.mParcelFileDescriptor.getFileDescriptor() to "/sdcard/video.mp4", everything was alright - video recorded correclty. But it doesn't sends to socket. Nothing happened clientside (in my app), but serverside thrown me exception java.net.SocketTimeoutException: read timed out. I tried this code serverside:
byte[] bytes = new byte[1024];
int read = mSocket_inputStream.read(bytes, 0, 1024);
System.out.println(read);
which debug me count of read bytes from client (from my app) - nothing was debugged in console, because 0 bytes was sent. Help me solve this problem please. I didn't found solution in google.
File descriptor only works for local server socket in MediaRecorder.
this.mParcelFileDescriptor = ParcelFileDescriptor.fromSocket(this.mSocket);
this.mMediaRecorder.setOutputFile(this.mParcelFileDescriptor.getFileDescriptor();
You can create your own LocalServerSocket and then pass data to actual server socket from your local server socket
To know more about local server socket you can go through https://developer.android.com/reference/android/net/LocalServerSocket.html
The more efficient way will be to use MediaCodec API for recording as it will provide you data frame by frame in format of byte buffer and you can send that data on to server.
I have the following code in Java that sends an HTTP request to a web server and read the response:
StringBuilder response = new StringBuilder(50000);
URL url2 = new URL(ServiceURL);
connection = (HttpURLConnection)url2.openConnection();
connection.setRequestMethod("POST");
//... (some more connection settings) ...
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
wr.write(Request);
wr.flush ();
wr.close ();
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
int i = 0;
while ((i = rd.read()) > 0) {
response.append((char)i);
}
It works for most cases, but I have a problem with one server that returns a rather large XML (something like 500KB; I guess this is pretty large for just a bunch of text..), where I keep getting a read timeout exception.
I believe it's not a network problem, because I've tried making the same request using curl and the response just arrived all right and pretty quick, something like two seconds.
When I look what's going on in the network (using wireshark to capture the packets), I noticed that the TCP receive window in my computer gets full at some point. The TCP stack sometimes survives this; I can see the server sending TCP keep-alive to keep the connection up, but in the end the TCP connection just breaks down.
Could it be that the reading part of the code (appending the received response character-by-character) is slowing my code down? Is there a more efficient way to read an HTTP response?
Reading character by character is quite slow, yes. Try reading chunks at a time into a buffer:
char[] buf = new char[2048];
int charsRead;
while((charsRead = rd.read(buf, 0, 2048)) > 0) {
response.append(buf, 0, charsRead);
}
As Phil already said reading the stream byte by byte is kinda slow. I prefer using the readLine() method of BufferedReader :
StringBuilder response = new StringBuilder();
String line = "";
while((line = rd.readLine()) != null) {
response.append(line + System.getProperty("line.separator");
}
If possible, I would consider using the Apache HTTP Client library. It is easy to use and very powerful in handling HTTP stuff.
http://hc.apache.org/httpcomponents-client-ga/
You should also keep in mind to set the socket and connection timeouts. This way you can control how long a connection is kept open (alt least on you side of the connection).
And last but not least always close your HTTP connections in a finally block after you received the response, otherwise you may run into a too many open files problem.
Hope this heps ;)
I want to recognize end of data stream in Java Sockets. When I run the code below, it just stuck and keeps running (it stucks at value 10).
I also want the program to download binary files, but the last byte is always distinct, so I don't know how to stop the while (pragmatically).
String host = "example.com";
String path = "/";
Socket connection = new Socket(host, 80);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.write("GET "+ path +" HTTP/1.1\r\nHost: "+ host +"\r\n\r\n");
out.flush();
int dataBuffer;
while ((dataBuffer = connection.getInputStream().read()) != -1)
System.out.println(dataBuffer);
out.close();
Thanks for any hints.
Actually your code is not correct.
In HTTP 1.0 each connection is closed and as a result the client could detect when an input has ended.
In HTTP 1.1 with persistent connections, the underlying TCP connection remains open, so a client can detect when an input ends with 1 of the following 2 ways:
1) The HTTP Server puts a Content-Length header indicating the size of the response. This can be used by the client to understand when the reponse has been fully read.
2)The response is send in Chunked-Encoding meaning that it comes in chunks prefixed with the size of each chunk. The client using this information can construct the response from the chunks received by the server.
You should be using an HTTP Client library since implementing a generic HTTP client is not trivial (at all I may say).
To be specific in your code posted you should have followed one of the above approaches.
Additionally you should read in lines, since HTTP is a line terminated protocol.
I.e. something like:
BufferedReader in =new BufferedReader(new InputStreamReader( Connection.getInputStream() ) );
String s=null;
while ( (s=in.readLine()) != null) {
//Read HTTP header
if (s.isEmpty()) break;//No more headers
}
}
By sending a Connection: close as suggested by khachik, gets the job done (since the closing of the connection helps detect the end of input) but the performance gets worse because for each request you start a new connection.
It depends of course on what you are trying to do (if you care or not)
You should use existing libraries for HTTP. See here.
Your code works as expected. The server doesn't close the connection, and dataBuffer never becomes -1. This happens because connections are kept alive in HTTP 1.1 by default. Use HTTP 1.0, or put Connection: close header in your request.
For example:
out.write("GET "+ path +" HTTP/1.1\r\nHost: "+ host +"\r\nConnection: close\r\n\r\n");
out.flush();
int dataBuffer;
while ((dataBuffer = connection.getInputStream().read()) != -1)
System.out.print((char)dataBuffer);
out.close();
I am currently working on a simple proxy server, which receives http request from browser, process it, then forward it to the desire web server.
I try to get the request from the input stream of the socket connected by the browser, everything is fine except that the stream get stuck after receiving the last block of data.
My code is in fact very simple, as shown below:
ServerSocket servSocket = new ServerSocket(8282);
Socket workSocket = servSocket.accept();
InputStream inStream = workSocket.getInputStream();
byte[] buffer = new byte[1024];
int numberRead = 0;
while ((numberRead = inStream.read(buffer, 0, 1024)) != -1){
System.out.println(new String(buffer));
}
The loop simply cannot exit, even the request reception is finished.
Is there any method to workaround this problem?
Thanks in advance for any advice.
As in InputStream javadoc the method will block until the data is available or the EOF is encountered. So, the other side of Socket needs to close it - then the inStream.read() call will return.
Another method is to send the size of message you want to read first, so you know ahead how many bytes you have to read. Or you can use BufferedReader to read from socket in line-wise way. BufferedReader has a method readLine() which returns every time a line is read, which should work for you as HTTP protocol packages are nice divided into lines.
It will cycle until the connection is closed, and the client is probably waiting for HTTP response from you and doesn't close it.
The browser is waiting for a response before it closes the connection.
Your read-method on the other hand will block until the stream/connection is closed or new data is received.
Not a direct solution according to your current code.
As HTTP is a line based protocol, you might want to use a Buffered Reader and call readLine() on it.
The when a http request comes in it will always be concluded with a blank line, for example:
GET /someFile.html HTTP/1.1
Host: www.asdf.com
After sending that request the client connection will then wait for a response from the server before closing the connection. So if you want to parse the request from the user you are probably better off using a BufferedReader and reading full lines until you reach a lines of text that is blank line.
I have the following Java socket client app, that sends same string to socket server:
import java.net.*;
import java.io.*;
public class ServerClient {
public static void main(String[] args) throws IOException {
System.out.println("Starting a socket server client...");
Socket client = new Socket("XXX.X.XXX.XX", 12001);
BufferedOutputStream stream = new BufferedOutputStream(client.getOutputStream());
String message = "ABC";
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
String input = null;
while ( true ) {
System.out.print("Would you like to send a message to Server? ");
input = inputReader.readLine();
if ( !input.equals("Y") ) break;
System.out.println("Message to send: " + message);
System.out.println("Message length is: " + message.length());
byte[] messageBytes = message.getBytes("US-ASCII");
stream.write(messageBytes, 0, messageBytes.length);
stream.flush();
}
System.out.println("Shutting down socket server client...");
stream.close();
client.close();
inputReader.close();
}
}
The first time message is sent, server receives the message; however, every subsequent time I'm trying to send this message, server is not receiving anything. Message simply disappears. I am writing to the socket successfully (no exceptions) but nothing is coming on the other side of the pipe (or so I'm told).
I do not have access to the server app, logs or code, so I'm wondering if there is any approach you can recommend to figure out why server is not receiving subsequent messages. Any ideas would be greatly appreciated!
Clarification:
New lines are not expected by the server; otherwise, how would it even receive message the first time? As a trial and error, I did try sending '\n' and "\r\n" and 0x00 characters at the end of the string - all without any luck.
I thought flushing was an issue, so I tried various outputstream classes (PrintStream, PrintWriter, FilterOutputStream), but was still running into same exact issues. Then, if "flushing" is an issue, how is it working the first time?
Other tests:
1 - use a network sniffer to see what is realy hapening on the network
2 - use some program like TCP Test Tool to send data to the server and simulate your program. (netcat can also be used, but it sends a newline after each line)
Remember:
TCP is stream oriented. not message oriented.
One write on the client could take several reads on the server to .. read
Multiple writes on the client could get read by the server in one read
You'll hardly see the above scenarios in a test application on a local network, you will see them very quick in a production environemnt, or when you start to really speed up the sending/receiving.
Following this, if you are sending messages you need a delimiter, or some other way of indicating 'here's one message', e.g. defining the protocol to be 'the first byte is the length of the following message'.
And you'd need to check the receiving end wether it read a partial message, a whole message, and any combination thereof (e.e.g one read might have read 3 and a half message..).
A quick solution for your test app, write lines. That is, a string followed by a newline character. A bufferedreader's ReadLine() could then take care of the reassembly for you on the receiving end.
It works correctly here... but I am missing a carriage return or some other end of message after sending the message.
Hard to write more without knowing what the server expects (protocol)...
Maybe you should try something like
String message = "ABC\n";