开发者

Communicating over a socket connection:

Steps done:

I have the server running looking for connections on socket 4444.

I have the android application connect to the socket.

I have the android application send two parameters across the socket connection.

I have the server digest the two separate parameters and process them accordingly.

My problem be开发者_C百科gins when I try to send a message back.

Please could you guys help me with an example of a client class and a server class using BufferedReader and PrintWriter to send data from the client to the server, accepting the data on the server side and returning data for the client to receive?

Thanks for you help.


create a new PrintWriter from the socket output stream,

 PrintWriter writer = new PrintWriter(new BufferedOutputStream(socket.getOutputStream()));
 writer.write("blather");
 writer.flush();



Dont use reader/writer some times i cause problem such as we cannOt predict end of string,etc.So please write or read only byte or byte array.It is the better way.
The following are the sample coding snippet

                     socket=new Socket(this.ipAddress,this.port_number);

            //socket.setSocketImplFactory(fac)
            Log.i(tagName, "after creating sokcet");

            os=socket.getOutputStream();
            is=socket.getInputStream();

            dos=new DataOutputStream(os);               
            Log.i(tagName, "after creating ouput streams");

            dis=new DataInputStream(is);
            Log.i(tagName, "after creating input streams");

            //dos.writeUTF(msg[i].trim());
            //dos.write(msg[i].trim().getBytes());

            //dos.writeUTF(msg[i].trim());
            dos.write(msg[i].trim().getBytes());
            //dos.writeUTF(str)
            dos.flush();

            Log.i(tagName, "after writing data to os");

            StringBuilder sbuilder=new StringBuilder();

            ///*
            int ch;
            byte bt=1;
            while((bt=(byte) dis.read())!=-1)
            {
                Log.i(tagName, "ch="+bt);
                byte temp[]=new byte[1];
                //temp[0]=(byte)ch;
                temp[0]=(byte)bt;
                String tempStr1=new String(temp);
                Log.i(tagName, "tempstr:"+tempStr1);

                sbuilder.append(tempStr1);

                Log.i(tagName, "Data fro server : "+sbuilder.toString());
                tempStr1=null;
            }
            //*/
            //byte tt[]=new byte[dis.readLine()]
            //resultStr=dis.readLine();resultStr=resultStr.trim();
            resultStr=sbuilder.toString();
            Log.i(tagName, "server res :"+resultStr);
            (Toast.makeText(this.actitivity,"Result : "+resultStr,Toast.LENGTH_SHORT)).show();


            if(dos!=null)
            {
                try
                {
                    dos.close();
                }
                catch(Exception ex)
                {

                }
            }

            if(dis!=null)
            {
                try
                {
                    dis.close();
                }
                catch(Exception ex){}
            }
            if(socket!=null)
            {
                try
                {
                    socket.close();
                }
                catch(Exception ex)
                {

                }
            }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜