Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old February 7th, 2012, 02:32 PM   #1 (permalink)
New Member
 
Join Date: Feb 2012
Posts: 3
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default Simple TCP Socket Client Application

I have currently created a simple tcp socket client application that sends and receives messages from a dotnet server application I have created. The application itself actually works however there are a few things I would like to know if they are possible or not. Also this is my first ever droid application and my first ever java application so I am sure there is a better way to do what I have written so please go easy on me.

The problem that I am having is I need a way to monitor the status of the socket so that if the connection gets dropped whether it is from the server application ending or a network drop that the android client would disconnect and put a notification on the screen letting me know. Currently if I end the server application nothign happens on the client and it acts like the connection is still open. Granted this is probably due to the way I have it written so I am looking for samples or suggestions on how to change this so I can monitor this. Any help would be great. Eventually this will be put into a much larger project for my home but I am starting off small so I can do it correctly the first time rather than having someing messy later on.

My code is listed below.

Thanks
KJ

Source:

package com.kkearney.tcp1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TCPClient1Activity extends Activity {
public static final int BUFFER_SIZE = 2048;
private Socket sck = null;
private PrintWriter out = null;
private BufferedReader in = null;

Button btnSend;
Button btnConnect;
Button btnDisconnect;
EditText textOut;
TextView textIn;
String response = "";
final Handler handler = new Handler();
final Runnable updateUI = new Runnable() {
public void run() {
textIn.setText(response);
}
};

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

textOut = (EditText)findViewById(R.id.txtSend);
textIn = (TextView)findViewById(R.id.txtMSG);
btnSend = (Button)findViewById(R.id.btnSend);
btnConnect = (Button)findViewById(R.id.btnConnect);
btnDisconnect = (Button)findViewById(R.id.btnDisconnect);
}

public void ConnectUp(View view){
OpenConnection();
}


public void OpenConnection(){
new Thread(new Runnable(){
public void run(){
try{
if (sck == null){
sck = new Socket("10.0.0.135",8080);
sck.setKeepAlive(true);
screenConfig();

in = new BufferedReader(new InputStreamReader(sck.getInputStream()));
out = new PrintWriter(sck.getOutputStream());
sendDataWithString("testing 1 2 3");
int charsRead = 0;
char[] buffer = new char[BUFFER_SIZE];

while ((charsRead = in.read(buffer)) != -1) {
response += new String(buffer).substring(0, charsRead);
handler.post( updateUI );
}

screenConfig();
}
}
catch (IOException e) {
System.out.print(e+"");
}
catch (Exception e) {
System.out.print(e+"");
}
}
}).start();


}
public void screenConfig(){
if (sck.isClosed() == false){
textOut.setClickable(true);
btnSend.setClickable(true);
btnDisconnect.setClickable(true);
btnConnect.setClickable(false);
}else{
textOut.setClickable(false);
btnSend.setClickable(false);
btnDisconnect.setClickable(false);
btnConnect.setClickable(true);
}
}

public void SendMessage(View view){
sendDataWithString(textOut.getText().toString());
textOut.setText(null);
}
public void DisConnect(View view){
CloseConnection();
}

public void CloseConnection(){
if (sck != null) {
try {
sck.close();
screenConfig();
} catch (IOException e) {
e.printStackTrace();
} finally {
sck = null;
}
}
}

public void sendDataWithString(String message) {
if (message != null && sck != null) {
out.write(message);
out.flush();
}
}

}


main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect" androidnClick="ConnectUp" android:clickable="true"/>
<Button
android:id="@+id/btnDisconnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disconnect" androidnClick="DisConnect" android:clickable="false"/>
</TableRow>
<EditText
android:id="@+id/txtSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:clickable="false">
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" androidnClick="SendMessage" android:clickable="false"/>
<TextView
android:id="@+id/txtMSG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>

Jasp3r is offline  
Reply With Quote
Sponsors
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 12:15 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo