mrboombastic
Lurker
Does anyone know the right way to write a networking program for Android?
I attempted to send an object from my phone to a regular socketServer. It uses a ObjectOutputStream & the Socket.accept() method.
The android client seems to be working. It seems to send the object.
However, the server doesnt seem to accept the object.
Does anyone know if its possible to create a networking program in this way for Android?
Here's the client
And heres the server
I attempted to send an object from my phone to a regular socketServer. It uses a ObjectOutputStream & the Socket.accept() method.
The android client seems to be working. It seems to send the object.
However, the server doesnt seem to accept the object.
Does anyone know if its possible to create a networking program in this way for Android?
Here's the client
Code:
package com.example.Plus;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.EditText;
import java.net.*;
import java.io.*;
public class PlusRemote extends Activity {
private EditText text;
//InetAddress address;
Socket s;
OutputStream out=null;
ObjectOutputStream oos=null;
String cmd ="";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(R.id.defaultIp);
}
public void myClickHandler(View view) {
switch (view.getId()){
case R.id.CON:
cmd = "Connect";
//Connect to Server
try{
this.text.setText("Connecting....");
//substitute x's for your real ip address
s=new Socket("xxx.xxx.xxx.xxx", 1234);
out=s.getOutputStream();
oos=new ObjectOutputStream(out);
oos.writeObject(new String(cmd));
oos.flush();
oos.close();
s.close();
}catch(Exception e){
this.text.setText(e.toString());
}
//SET status "Connected Success!"
this.text.setText(R.string.EditText01);
break;
case R.id.REW:
Toast.makeText(this, "REWIND ", Toast.LENGTH_LONG).show();
break;
case R.id.STOP:
Toast.makeText(this, "STOP", Toast.LENGTH_LONG).show();
break;
case R.id.PLAY:
Toast.makeText(this, "PLAY", Toast.LENGTH_LONG).show();
break;
case R.id.FWD:
Toast.makeText(this, "FOWARD", Toast.LENGTH_LONG).show();
break;
}
}
}
Code:
//PlusServer.java
//Server that takes commands from Android Client, executes commands & finally returns result.
import java.net.*;
import java.io.*;
public class PlusServer
{
//instance variables
static Boolean finished = false;
//Main method
public static void main(String[] args)
{
System.out.println(" 000 Entered Main method");
//Disable Security Manager
//System.setSecurityManager(null);
try
{
ServerSocket listener = new ServerSocket(1234);
System.out.println("0 setup ServerSocket on port 1234");
while(!finished)
{
Socket client = listener.accept();//wait for connection
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
//read cmd
ObjectInputStream oin = new ObjectInputStream(in);
String cmd = (String)oin.readObject();
if (cmd.equals("Connect"))
{
client.close();
finished=true;
}
//Send results
// blah, blah, blah
}
listener.close();
}
catch(Exception e){System.out.println("Error: "+e);}
}
}