I try to create a socket server on the phone, I check all of the methods used in my case, it keep giving me NetworkOnMainThreadException. anyone can see where is problem in my case? I try to add the internet permission, using thread and AsyncTask too, I don't know why i cannot make it.
server side code(Asynctask version):
package com.example.socketserver;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.net.ServerSocket;
import java.net.Socket;
import android.R.string;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ServerFragment extends Fragment {
ServerSocket serverSocket;
Socket connectionSocket;
private Button mSentButton;
private TextView mMessageTextView;
private EditText mMessagEditText;
private int counter =1;
private ObjectOutputStream outputStream;
private ObjectInputStream inputStream;
private static String TAG="serverFragment";
private String message;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.i(TAG,"onCreate starts");
}
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
//refer a view which wires a layout
View v = inflater.inflate(R.layout.fragment_server,parent,false);
mSentButton=(Button)v.findViewById(R.id.Sentbutton);
mMessageTextView=(TextView)v.findViewById(R.id.messageTextView);
Log.i(TAG,"onCreateView starts");
mMessageTextView.setText("Waiting for connection\n");
mMessagEditText=(EditText)v.findViewById(R.id.Message);
new connection().execute();
return v;
}
private class connection extends AsyncTask{
@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
try {
runServer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public void runServer() throws IOException
{
try {
serverSocket= new ServerSocket(8080,100);
} catch (Exception e) {
mMessageTextView.append("\nRun Server terminated connection");
}
Log.i(TAG,"runServer() starts");
while(true)
{
try {
waitForConnection();
} catch (Exception e) {
mMessageTextView.append("\nWait terminated connection");
Log.i(TAG,"\nWait terminated connection "+e.toString());
}
try {
getStream();
} catch (Exception e) {
mMessageTextView.append("\n GetStream() terminated connection");
Log.i(TAG,"\n GetStream() terminated connection"+ e.toString());
}
try{
processConnection();
} catch (Exception e) {
mMessageTextView.append("\n processConnection() terminated connection");
Log.i(TAG,"\n processConnection() terminated connection"+ e.toString());
}
finally
{
closeConnection();
counter++;
}
}
}
private void sentDate(String message)
{
try {
outputStream.writeObject("SERVER>>>"+message);
outputStream.flush();
mMessageTextView.append("\nSERVER>>>"+ message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void closeConnection() throws IOException {
// TODO Auto-generated method stub
Log.i(TAG,"closeConnection starts");
mMessageTextView.append("\nTerminating connection\n");
outputStream.close();
inputStream.close();
connectionSocket.close();
}
private void processConnection() {
// TODO Auto-generated method stub
message="Connection successful!";
sentDate(message);
Log.i(TAG,"ProcessConnection starts");
do {
try {
message=(String)inputStream.readObject();
} catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
mMessageTextView.append("\n Unknown object type received");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mMessageTextView.append("\n"+ message);
}
});
} while (!message.equals("CLIENT>>> TERMINATE"));
}
private void getStream() throws IOException {
outputStream = new ObjectOutputStream(connectionSocket.getOutputStream());
outputStream.flush();
Log.i(TAG,"getStreams()'s output starts");
inputStream = new ObjectInputStream(connectionSocket.getInputStream());
Log.i(TAG,"getStreams()'s input created");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mMessageTextView.append("\nGot i/O stream \n");
}
});
}
private void waitForConnection() throws IOException {
// TODO Auto-generated method stub
Log.i(TAG,"waitForConnection() starts");
mMessageTextView.setText("Waiting for connection\n");
try {
connectionSocket=serverSocket.accept();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mMessagEditText.append("Connection "+ counter+ " received from : "
+ connectionSocket.getInetAddress().getHostName());
}
});
}
}
}
Server side code(Thread version)
package com.example.socketserver;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerFragment extends Fragment {
ServerSocket serverSocket;
Socket connectionSocket;
private Button mSentButton;
private TextView mMessageTextView;
private EditText mMessagEditText;
private int counter =1;
private ObjectOutputStream outputStream;
private ObjectInputStream inputStream;
private static String TAG="serverFragment";
private String message;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.i(TAG,"onCreate starts");
}
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
//refer a view which wires a layout
View v = inflater.inflate(R.layout.fragment_server,parent,false);
mSentButton=(Button)v.findViewById(R.id.Sentbutton);
mMessageTextView=(TextView)v.findViewById(R.id.messageTextView);
Log.i(TAG,"onCreateView starts");
mMessageTextView.setText("Waiting for connection\n");
mMessagEditText=(EditText)v.findViewById(R.id.Message);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
serverSocket= new ServerSocket(8080,100);
} catch (Exception e) {
mMessageTextView.append("\nRun Server terminated connection");
}
try {
waitForConnection();
getStream();
processConnection();
closeConnection();
counter++;
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
thread.start();
//new connection().execute();
return v;
}
/*
private class connection extends AsyncTask{
@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
try {
serverSocket= new ServerSocket(8080,100);
} catch (Exception e) {
mMessageTextView.append("\nRun Server terminated connection");
}
try {
waitForConnection();
getStream();
processConnection();
closeConnection();
counter++;
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
*/
/*
public void runServer() throws IOException
{
try {
serverSocket= new ServerSocket(8080,100);
} catch (Exception e) {
mMessageTextView.append("\nRun Server terminated connection");
}
Log.i(TAG,"runServer() starts");
while(true)
{
try {
waitForConnection();
} catch (Exception e) {
mMessageTextView.append("\nWait terminated connection");
Log.i(TAG,"\nWait terminated connection "+e.toString());
}
try {
getStream();
} catch (Exception e) {
mMessageTextView.append("\n GetStream() terminated connection");
Log.i(TAG,"\n GetStream() terminated connection"+ e.toString());
}
try{
processConnection();
} catch (Exception e) {
mMessageTextView.append("\n processConnection() terminated connection");
Log.i(TAG,"\n processConnection() terminated connection"+ e.toString());
}
finally
{
closeConnection();
counter++;
}
}
}
*/
private void sentDate(String message)
{
try {
outputStream.writeObject("SERVER>>>"+message);
outputStream.flush();
mMessageTextView.append("\nSERVER>>>"+ message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void closeConnection() throws IOException {
// TODO Auto-generated method stub
Log.i(TAG,"closeConnection starts");
mMessageTextView.append("\nTerminating connection\n");
outputStream.close();
inputStream.close();
connectionSocket.close();
}
private void processConnection() {
// TODO Auto-generated method stub
message="Connection successful!";
sentDate(message);
Log.i(TAG,"ProcessConnection starts");
do {
try {
message=(String)inputStream.readObject();
} catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
mMessageTextView.append("\n Unknown object type received");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mMessageTextView.append("\n"+ message);
}
});
} while (!message.equals("CLIENT>>> TERMINATE"));
}
private void getStream() throws IOException {
outputStream = new ObjectOutputStream(connectionSocket.getOutputStream());
outputStream.flush();
Log.i(TAG,"getStreams()'s output starts");
inputStream = new ObjectInputStream(connectionSocket.getInputStream());
Log.i(TAG,"getStreams()'s input created");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mMessageTextView.append("\nGot i/O stream \n");
}
});
}
private void waitForConnection() throws IOException {
// TODO Auto-generated method stub
Log.i(TAG,"waitForConnection() starts");
mMessageTextView.setText("Waiting for connection\n");
try {
connectionSocket=serverSocket.accept();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mMessagEditText.append("Connection "+ counter+ " received from : "
+ connectionSocket.getInetAddress().getHostName());
}
});
}
}
error(AsyncTask version):
08-22 16:35:05.829: I/serverFragment(22134): onCreate starts
08-22 16:35:05.839: I/serverFragment(22134): onCreateView starts
08-22 16:35:05.839: I/serverFragment(22134): runServer() starts
08-22 16:35:05.839: I/serverFragment(22134): waitForConnection() starts
08-22 16:35:05.889: I/Adreno-EGL(22134): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
08-22 16:35:05.940: D/OpenGLRenderer(22134): Enabling debug mode 0
08-22 16:35:13.027: D/AndroidRuntime(22134): Shutting down VM
08-22 16:35:13.027: W/dalvikvm(22134): threadid=1: thread exiting with uncaught exception (group=0x415dcba8)
08-22 16:35:13.027: I/serverFragment(22134): getStreams()'s output starts
08-22 16:35:13.027: E/AndroidRuntime(22134): FATAL EXCEPTION: main
08-22 16:35:13.027: E/AndroidRuntime(22134): Process: com.example.socketserver, PID: 22134
08-22 16:35:13.027: E/AndroidRuntime(22134): android.os.NetworkOnMainThreadException
08-22 16:35:13.027: E/AndroidRuntime(22134): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
08-22 16:35:13.027: E/AndroidRuntime(22134): at java.net.InetAddress.getHostByAddrImpl(InetAddress.java:438)
08-22 16:35:13.027: E/AndroidRuntime(22134): at java.net.InetAddress.getHostName(InetAddress.java:307)
08-22 16:35:13.027: E/AndroidRuntime(22134): at com.example.socketserver.ServerFragment$connection$3.run(ServerFragment.java:216)
08-22 16:35:13.027: E/AndroidRuntime(22134): at android.os.Handler.handleCallback(Handler.java:733)
08-22 16:35:13.027: E/AndroidRuntime(22134): at android.os.Handler.dispatchMessage(Handler.java:95)
08-22 16:35:13.027: E/AndroidRuntime(22134): at android.os.Looper.loop(Looper.java:136)
08-22 16:35:13.027: E/AndroidRuntime(22134): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-22 16:35:13.027: E/AndroidRuntime(22134): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 16:35:13.027: E/AndroidRuntime(22134): at java.lang.reflect.Method.invoke(Method.java:515)
08-22 16:35:13.027: E/AndroidRuntime(22134): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-22 16:35:13.027: E/AndroidRuntime(22134): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-22 16:35:13.027: E/AndroidRuntime(22134): at dalvik.system.NativeStart.main(Native Method)
0
8-22 16:40:13.167: I/Process(22134): Sending signal. PID: 22134 SIG: 9
error (thread version):
ERROR:
08-22 16:28:30.488: I/serverFragment(20568): onCreate starts
08-22 16:28:30.498: I/serverFragment(20568): onCreateView starts
08-22 16:28:30.498: I/serverFragment(20568): waitForConnection() starts
08-22 16:28:30.548: I/Adreno-EGL(20568): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
08-22 16:28:30.588: D/OpenGLRenderer(20568): Enabling debug mode 0
08-22 16:28:38.836: D/AndroidRuntime(20568): Shutting down VM
08-22 16:28:38.836: W/dalvikvm(20568): threadid=1: thread exiting with uncaught exception (group=0x415dcba8)
08-22 16:28:38.836: I/serverFragment(20568): getStreams()'s output starts
08-22 16:28:38.836: E/AndroidRuntime(20568): FATAL EXCEPTION: main
08-22 16:28:38.836: E/AndroidRuntime(20568): Process: com.example.socketserver, PID: 20568
08-22 16:28:38.836: E/AndroidRuntime(20568): android.os.NetworkOnMainThreadException
08-22 16:28:38.836: E/AndroidRuntime(20568): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
08-22 16:28:38.836: E/AndroidRuntime(20568): at java.net.InetAddress.getHostByAddrImpl(InetAddress.java:438)
08-22 16:28:38.836: E/AndroidRuntime(20568): at java.net.InetAddress.getHostName(InetAddress.java:307)
08-22 16:28:38.836: E/AndroidRuntime(20568): at com.example.socketserver.ServerFragment$4.run(ServerFragment.java:248)
08-22 16:28:38.836: E/AndroidRuntime(20568): at android.os.Handler.handleCallback(Handler.java:733)
08-22 16:28:38.836: E/AndroidRuntime(20568): at android.os.Handler.dispatchMessage(Handler.java:95)
08-22 16:28:38.836: E/AndroidRuntime(20568): at android.os.Looper.loop(Looper.java:136)
08-22 16:28:38.836: E/AndroidRuntime(20568): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-22 16:28:38.836: E/AndroidRuntime(20568): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 16:28:38.836: E/AndroidRuntime(20568): at java.lang.reflect.Method.invoke(Method.java:515)
08-22 16:28:38.836: E/AndroidRuntime(20568): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-22 16:28:38.836: E/AndroidRuntime(20568): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-22 16:28:38.836: E/AndroidRuntime(20568): at dalvik.system.NativeStart.main(Native Method)
08-22 16:32:37.431: I/Process(20568): Sending signal. PID: 20568 SIG