Reputation: 4467
I need to connect to a server via TCPIP. I found out how to do this, but the examples do not make a permanent connection, it just connects sends data and drops the connection. I need to leave the connection open to pass back and forth several packages.
For example I am used to requesting a connection, then having a process to listen for incoming data. Like this:
Is there anything that I can read on this or better yet sample code?
Upvotes: 0
Views: 6516
Reputation: 9574
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.
Upvotes: 4
Reputation: 4740
Maybe you should opt for a higher level library like "Apache MINA" or "Netty" anyway?
Upvotes: 0
Reputation: 596
I made a similar application with a different use, only there was no listening involved on the server side, only sending. However, it does stay open in a loop, and you could modify it to, upon receiving a specific message, close the loop and the connection using the strstr command in the while loop. See this topic: Garbage data transmitted in WiFi TCP connection from desktop to Android for the code, and change the line:
n = write(newsockfd, string, sizeof(string));
to
n = write(newsockfd, string, strlen(string));
in order to fix the issue I was having.
Upvotes: 0