Question

I have written code for the client and server which will be connected over wifi and client will send string to server, but the problem is even though I think I have implemented the code correctly it doesn't seem to work. Even the logcat is not showing any errors while I run the code on android device. Can someone help please? Here's the client code.

public class ControlActivity extends Activity {
WifiManager wifi=null;
WifiInfo winfo=null;
Socket ssocket=null;
String sstr=null;
String ipadd=null;
Integer ip;
TextView text;
Integer k=0;
Handler handler;
Boolean pdown=false;
InetAddress seraddr;



public String intToIp(int i) {

       return ( i & 0xFF) + "." + ((i >> 8 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) + "." + ((i >> 24 ) & 0xFF );
    }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_control);
    wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
    winfo = wifi.getConnectionInfo();
    ip = winfo.getIpAddress();
    ipadd = intToIp(ip);
    try {
        seraddr = InetAddress.getByName(ipadd);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    text = (TextView)findViewById(R.id.textView1);
    text.setText(ipadd);


    handler = new Handler(){
        @Override
        public void handleMessage(Message msg1){
            Bundle bundle = msg1.getData();
            String str = bundle.getString("key");
            text.setText(str);
        }
    };



    ImageButton butup = (ImageButton)findViewById(R.id.buttonup);
    butup.setOnTouchListener(new View.OnTouchListener() {
        private Thread touchthread;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(pdown==false)
                    pdown=true;
                sstr = "up";
                this.touchthread = new Thread(new OnTouchClientThread());
                this.touchthread.start();
                break;

            case MotionEvent.ACTION_UP:
                pdown=false;

            }
            return true;
        }
    });


        ImageButton butdown = (ImageButton)findViewById(R.id.buttondown);
        butdown.setOnTouchListener(new View.OnTouchListener() {
            private Thread touchthread;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    if(pdown==false)
                        pdown=true;
                    sstr = "down";
                    this.touchthread = new Thread(new OnTouchClientThread());
                    this.touchthread.start();
                    break;

                case MotionEvent.ACTION_UP:
                    pdown=false;

                }
                return true;
            }
        });
        }
public class OnTouchClientThread implements Runnable{

    @Override
    public void run() {
        try {
            Message msg = handler.obtainMessage();
            Bundle bundle = new Bundle();
            ssocket = new Socket(seraddr,5479);
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(ssocket.getOutputStream())),true);
            while(pdown==true){
                k=k+1;
                bundle.putString("key",k.toString());
                msg.setData(bundle);
                handler.sendMessage(msg);
                out.println(sstr);

            }
            out.flush();
            out.close();
            ssocket.close();

    } catch (UnknownHostException e) {
            e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }
    }
}

here's the server

public class FInterActivity extends Activity {

WifiManager wifi=null;
Socket ssocket=null;
String sstr=null;
Thread recithrd;
TextView testing;
ServerSocket ss;
Handler handler;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_finter);
    wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
    testing = (TextView)findViewById(R.id.testtext);
    testing.setText("Starting thread");
    this.recithrd = new Thread(new ServerThread());
    this.recithrd.start();

    handler = new Handler(){
        @Override
        public void handleMessage(Message msg1){
            Bundle bundle = msg1.getData();
            String str = bundle.getString("key");
            testing.setText(str);
        }
    };

}

public class ServerThread implements Runnable{

    @Override
    public void run() {
        try {
            Message msg = handler.obtainMessage();
            Bundle bundle = new Bundle();
            ss = new ServerSocket(5479);
            ssocket = ss.accept();
            while(true){
                    BufferedReader input = new BufferedReader(new InputStreamReader(ssocket.getInputStream()));
                    sstr=null;
                    sstr = input.readLine();
                    if(sstr!=null){
                    bundle.putString("key", sstr);
                    msg.setData(bundle);
                    handler.sendMessage(msg);
                    }
            }


    } catch (UnknownHostException e) {
            e.printStackTrace();
            handler.post(new Runnable(){
                @Override
                public void run() {
                    testing.setText("unknown host");

                }

            });
    } catch (IOException e) {
            e.printStackTrace();
            handler.post(new Runnable(){
                @Override
                public void run() {
                    testing.setText("Io exception");
                }

            });
    }           
    }



}

and yes i have provided all the permissions required. please help. i am stuck on this for days.

Was it helpful?

Solution

Your code seems to be wrong in more than one place.

1) The client seems to be getting its own address, putting it into seraddr, and trying to connect to itself! Certainly the server has a different address, unless you are doing a local connection and in this case you should just use 127.0.0.1.

2) At server side, you are accept()ing only once, handling the connection and exiting the thread. This means that your server will accept only one connection and no more. Is this what you wanted?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top