开发者

server client two way communication in android

I am implementing this example

http://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/

that shows how to do a server client communication between two android phones. I've gotten it to work, but I want to try and modify it a bit. Currently the way the program is set up, the client sends messages to the server only. I was trying to modify this so that the server can also send to the client, and the client can listen as well. I have a timer going on in both phones that increments a counter, and I just want them to share their counter values back and forth. There are difficulties I've had in doing this though.

on the server side, after it receives a message from the client, I decided to add a printwriter out = new PrintWriter(client.getOutputStream(), true);

and then in the while loop, where we read input from the client, inside the runnable code i added just out.println("hello client");

the code doesn't seem to crash, so I guess that's good. So then now I'm looking at the client side. I realize that it is important to run socket code on separate threads because it is "blocking". However, the client code in that example doesn't seem to be using the handler though and I'm wondering why. I tried to put a bufferedreader in the client code and then post a new runnable, just like the server code does. I put it in the same try catch block as where the client has its printwriter

 in = new BufferedReader(new 开发者_如何学CInputStreamReader(socket.getInputStream()));

                     n=in.readLine();


                    if(n!=null)
                    {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {

                                fromserver.setText(n);
                            }
                        });
                        //

                    }

What I find interesting though is that when this happens, my server code, which normally updates the client counter values it has received and its own counter values no longer updates. It does say that it is connected though. I had programmed it so that it won't set its textlabel unless it receives a message from the client; this must mean that client is no longer sending messages. I'm wondering what I'm doing wrong on the client side.

To reiterate, what I'm trying to do is have both the server and the client sending information back and forth. And I think somewhere I must be blocking a thread. I'm not sure. Can someone tell me how to properly do this type of back and forth communication? As per Peter's statement, I'm ok with it only being able to work on a local network. I have the two phones connected via a wireless router.

Thank you


A side note: mobile networks employ NAT. NAT prevents initiating inbound (internet to device) connections. This effectivelly prevents mobile devices to host servers.

Your approach would only work on local networks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜