Reputation: 4594
I've been trying for days now to create an application client server,both on android,running on the same machine.... I've used all kind of Display functions to show the state of the client and the server...after struggling I've succeeded to connect both of them,that was not very difficult to do.
The big problem I'm facing is when it comes to sending data from the client to server.
I've used a DisplayTitle()
function to show on the server side the data that reached there coming from the client-but it never shows nothing
I think that the problem is at sending the data cause I'm using PrintWriter to send the data and I've read that I should flush it in order to do this-otherwise the data will be sent only when my buffer is full....I've done that and still nothing!!!
This is my client code:
public class screen1 extends Activity {
private TextView clientState;
private String serverIpAddress="10.0.2.2";
public static final int ClientPort = 8080;
String message="Hello Server!";
int longitude;
int latitude;
Cursor c;
DBAdapter db;
private Handler handler=new Handler();
Socket socket;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
clientState = (TextView) findViewById(R.id.client_Status);
Button b = (Button)findViewById(R.id.mainMenu);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(screen1.this, screen2.class);
startActivity(i);
}
});
Thread cThread=new Thread(new ClientThread());
cThread.start();
db=new DBAdapter(this);
db.createDatabase();
db.openDataBase();
}
public class ClientThread implements Runnable{
private PrintWriter out=null;
public void run()
{
try
{
InetAddress serverAddr=InetAddress.getByName(serverIpAddress);
handler.post(new Runnable(){
public void run(){
clientState.setText(" try to connect!");
}
});
socket=new Socket(serverAddr,ClientPort);
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
}
catch(UnknownHostException e){
System.err.println("Don't know about host");
}
catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to host");
}
if(socket!=null && out!=null){
try{
c=db.getAllData();
if(c.moveToFirst())
{
do{
longitude=Integer.parseInt(c.getString(1));
out.println(longitude);
out.flush();
latitude=Integer.parseInt(c.getString(2));
out.println(latitude);
out.flush();
}while(c.moveToNext());
}
out.close();
socket.close();
}
catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
}
}
And here is my server where in DisplayTitle()
I try to display the latitude and longitude that I should receive from the client:
public class Server1 extends Activity {
//the ip the server is listening to
public static String SERVERIP="10.0.2.15";
Socket client;
Scanner s=null;
TextView serverStatus;
Handler handler=new Handler();
protected int ServerPort= 6000;
int longitude;
int latitude;
protected boolean isStopped = false;
private ServerSocket serverSocket=null;
private Socket clientSocket=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server1);
Button b = (Button)findViewById(R.id.mainMenu);
serverStatus =(TextView)findViewById(R.id.server_Status);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0){Intent i = new Intent(Server1.this, Server2.class);
startActivity(i);}});
Thread fst=new Thread(new ServerThread());
fst.start();
// MultiThreadedServer server = new MultiThreadedServer(6000);
// new Thread(server).start();
}
public class ServerThread implements Runnable{
public void run(){
try{
InetSocketAddress serverAddr =new InetSocketAddress(SERVERIP,6000);
serverSocket = new ServerSocket();
serverSocket.bind(serverAddr);
handler.post(new Runnable(){public void run() {
serverStatus.setText("wait for clients");
}});
while(true){
//accept for incoming clients
client= serverSocket.accept();
handler.post(new Runnable(){public void run() {
serverStatus.setText("Connected.");
}});
try{
s=new Scanner(new BufferedReader(new InputStreamReader(client.getInputStream())));
while(s.hasNext()){
longitude=Integer.parseInt(s.next());
System.out.println(longitude);
latitude=Integer.parseInt(s.next());
System.out.println(latitude);
// db.insertData2(longitude,latitude);
handler.post(new Runnable() {
public void run() {
DisplayTitle(longitude,latitude);
}
});
}
break;
}
catch(Exception e)
{
handler.post(new Runnable(){
public void run(){
serverStatus.setText("Connection interrupted!!");
}
});
e.printStackTrace();
}
}
}
catch(Exception e)
{
handler.post(new Runnable()
{
public void run(){
serverStatus.setText("Error");
}
});
e.printStackTrace();
}
}
}
public void DisplayTitle(int longitude,int latitude)
{
Toast.makeText(this,
"longitude: " + longitude +"\n" +
"latitude: " + latitude +"\n" ,
Toast.LENGTH_LONG).show();
}
}
If someone has any idea of what I'm doing wrong please tell me! I'm here for further details.
Upvotes: 3
Views: 1488
Reputation: 34544
If you are just wanting to view the data you are sending in your client or server, what is wrong with using debug output to show that you are receiving the data? You can use logcat for this. Just print the data to logcat when you receive or send it.
Log.d(string, string);
Upvotes: 2