Answer the question
In order to leave comments, you need to log in
Android socket is a problem when sending data. Why is data only sent after the socket's outgoing stream is closed?
Good afternoon!
I am writing a client application for android. Sending requests to the server and receiving responses from the server. Connecting to the server via sockets.
Faced such problem: the data is sent only after closing of an output stream of a socket. I will describe the situation: there is an Asynctask in which ONLY ONE connection to the server should be open. But I am unable to implement it. It turns out to be done only according to this scheme: opened the connection, sent the data, received the data, closed the connection. Because without closing the connection no data is sent to the server.
Here is the AsyncTask class:
class InternetRequestAsyncTask extends AsyncTask<String, Integer, String>
{
private Socket socket = null;
private DataOutputStream outStream = null;
private BufferedReader in;
private String ip, port;
final String GPS_MESSAGE = "GPS_MESSAGE";
final String LOGIN_REQUEST = "LOGIN_REQUEST";
final String LIST_REQUEST = "LIST_REQUEST";
private String command = "";
public InternetRequestAsyncTask(String IP, String PORT)
{
ip = IP;
port = PORT;
}
private void setCommandType(String commandType)
{
command = commandType;
}
private void connect()
{
try {
socket = new Socket(ip, Integer.parseInt(port));
socket.setKeepAlive(true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outStream = new DataOutputStream(socket.getOutputStream());
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("MyTag", "Connected");
}
private void disconnect()
{
if (socket != null)
{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outStream != null)
{
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.d("MyTag", "Disconnected");
}
private void writeMessage(String message) throws IOException
{
outStream.writeBytes(message + "\r\n");
}
@Override
protected String doInBackground(String... params)
{
this.connect();
while (true)
{
if (command.equals(GPS_MESSAGE))
{
try {
this.writeMessage(GPS_MESSAGE);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
command = "";
} else
{
if (command.equals(LOGIN_REQUEST))
{
try {
this.writeMessage(LOGIN_REQUEST);
StringBuilder sb = new StringBuilder();
String str;
while ((str = in.readLine()) != null)
{
sb.append(str);
}
Log.d("MyTag", sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
command = "";
} else
{
if (command.equals(LIST_REQUEST))
{
try {
this.writeMessage(LIST_REQUEST);
StringBuilder sb = new StringBuilder();
String str;
while ((str = in.readLine()) != null)
{
sb.append(str);
}
Log.d("MyTag", sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
command = "";
} else
{
if (command.equals("EXIT"))
{
break;
}
}
}
}
}
return null;
}
@Override
protected void onPostExecute(String result)
{
this.disconnect();
super.onPostExecute(result);
}
@Override
protected void onCancelled()
{
this.disconnect();
super.onCancelled();
}
}
InternetRequestAsyncTask irat = new InternetRequestAsyncTask(IP, PORT);
irat.execute();
irat.setCommandType(commandType);
Answer the question
In order to leave comments, you need to log in
stackoverflow.com/questions/7166328/when-why-to-call-system-out-flush-in-java
Try using the flush() method when you want to "push" data from the stream buffer to the network.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question