London Student
London Student

Reputation: 915

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 begins 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.

Upvotes: 1

Views: 367

Answers (2)

SIVAKUMAR.J
SIVAKUMAR.J

Reputation: 4354


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)
                {

                }
            }

Upvotes: 0

sbridges
sbridges

Reputation: 25140

create a new PrintWriter from the socket output stream,

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

Upvotes: 1

Related Questions