How to pass string from TCP server to main activity?

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

How to pass string from TCP server to main activity?

Postby madhunandan » Fri Dec 10, 2010 2:59 pm

Hi,

Please help me with passing a string from TCP server to the main activity.

The code for server and main activity is below.

Thank you for any suggestion and time spent on this.

Cheers,
Madhu Nandan

TCPIPServer


public class TCPIPServer extends Activity implements Runnable{

public static final String SERVERIP = "127.0.0.1";
public static final int SERVERPORT = 7777;
//private final Handler mHandler;

//public TCPIPServer(Context context, Handler handler) {
// mHandler = handler;
//}

public void run() {
try {
Log.d("TCP","S: Connecting...");

ServerSocket serverSocket = new ServerSocket(SERVERPORT);
while (true) {

Socket client = serverSocket.accept();
Log.d("TCP","S: Receiving...");

try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = in.readLine();
Log.d("TCP","S: Received: '" + str + "'");
//Pass str to main activity for parsing
} catch(Exception e) {
Log.d("TCP","S: Error", e);
e.printStackTrace();
} finally {
client.close();
Log.d("TCP","S: Done danaadan.");
}

}

} catch (Exception e) {
Log.d("TCP","S: Error", e);
e.printStackTrace();
}
}


}


TCPIPCommn (the main activity)


public class TCPIPCommn extends Activity {

//private TCPIPServer mServer = null;
//mServer = new TCPIPServer(this, mHandler);

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Thread cThread = new Thread(new TCPIPServer());

cThread.start();
}

//private final Handler mHandler = new Handler() {
//Function to extract name and value from the received message
public void extractData(String tempStr) {
code...
code...
}

//Function to display driver messages/images based on individual messages
public void actionOnData(String name, double value) {
code...
code...
}

}
User avatar
madhunandan
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Dec 08, 2010 4:40 pm
Location: Rüsselsheim, Germany

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests