from now on (ICS+), to make a network connection, you will need to use an AsyncTask (or handler), unless you are willing to remove restrictions (unadvisable!). network connections are, by default, restricted from main activity thread.
this AsyncTask does the trick (on the other side i had a c++ socket server). in this case i open the socket, send the string that i received from main activity, receive an answer, send another message (this time "hello there") and receive a final message before closing connection.
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.AsyncTask;
import android.util.Log;
public class SocketConnect extends AsyncTask <String, Integer, String> {
String TAG = "TGsocket";
@Override
protected String doInBackground(String... params) {
String url = params[0];
String textSend = params[1];
Log.i(TAG, "textsend = |" + textSend + "|");
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket(url, 9090);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
Log.i(TAG,"socket OK");
dataOutputStream.writeUTF(textSend);
String textInNow = dataInputStream.readLine();
Log.w(TAG, "says Server = " + textInNow);
dataOutputStream.writeUTF("hello there");
textInNow = dataInputStream.readLine();
Log.w(TAG, "says 2ndTime Server = " + textInNow);
} catch (UnknownHostException e) {
Log.e(TAG, "at thread unknownHost " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.e(TAG, "at thread IO " + e.getMessage());
e.printStackTrace();
}
finally{
Log.i(TAG, "finally");
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
Log.e(TAG, "at thread dataoutput IO " + e.getMessage());
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
Log.e(TAG, "at thread datainput IO " + e.getMessage());
e.printStackTrace();
}
}
if (socket != null){
try {
Log.i(TAG, "socket closed");
socket.close();
} catch (IOException e) {
Log.e(TAG, "at thread finally IO " + e.getMessage());
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//displayProgressBar("Downloading...");
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//updateProgressBar(values[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
call using (within try catch):
SocketConnect("100.100.100.100","some string");
where "100.100.100.100" is your server's IP.
NOTE1: do not forget to set permissions for internet use (in your manifest file)
NOTE2: you can safely strip away the Log calls and most of the checks, as well as the 3 final overrides (i just left it all there because it makes it easier, in some cases, to build upon).
tested on froyo, gingerbread, ics and jb.