Hi guys so I have a task where I'm suppose to send a message to a server, and then get a reply back. I keep getting failed 1recvfrom failed: EBADF (Bad file descriptor)
error and I'm new to this socket stuff. Hopefully someone can help me out. Here's my async task:
private class SendDataAsyncTask extends AsyncTask<Void,Void,Void>
{
private String reply = "";
@Override
protected Void doInBackground(Void... params)
{
try {
Socket socket = new Socket("ip",portNumber);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
//printWriter.write("hello there");
printWriter.println("hello there");
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
printWriter.flush();
reply = bufferedReader.readLine();
if(reply != null && !reply.equals(""))
{
Log.d(SOCKET_TESTING_TAG,"my message is " + reply);
}
else
{
Log.d(SOCKET_TESTING_TAG,"no message yet");
}
Log.d(SOCKET_TESTING_TAG,"break 3");
printWriter.close();
}
catch (IOException e)
{
Log.d(SOCKET_TESTING_TAG,"failed " + e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
public void onPostExecute(Void var)
{
tv_response.setText(reply);
}
}
- Note: When I put the if statement to check for the null readline before
printWriter.flush()
I still get an error.