boss
boss

Reputation: 1606

Android Client Communication

I want to communicate between android based client and c# based server. I already did it.. But I want to send byte array to android based client.. Normally I send string value.. When I send byte array It doesnt work !! In C# we can send byte array a function that is in Socket Class.. But In java side, I dont have any idea. I searched that I could use DatagramSocket .. But I want to do it with Socket Class.. Well this is in c# side..

public void Send(byte[] buffer)
        {
            try
            {                
                cSocket.Send(buffer);
            }
            catch (SocketException s_ex)
            {
                Console.WriteLine(s_ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
        public void ReceiveListen()
        {            
            while (true)
            {
                try
                {
                    buffer = new byte[8192];

                int socketStatus = cSocket.Receive(buffer);
                    if (socketStatus!= 0)
                    {
                        Array.Resize(ref buffer, socketStatus);

                    Parse(buffer);
                }
            }
            catch
            {
            }

            Thread.Sleep(100); 
        }
    } 

as you see, I can send byte array and recieve same type ... In Android Side, I used this code (normally when I send string value to client(Android) it works.. There are stream class in C#.. I can send them with stream class..)

     try {
          BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                        String line = null;
                        while ((line = in.readLine()) != null) {
                            Log.d("ServerActivity", line);
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                     serverStatus.setText("Connected");
                                }
                            });
                        }
                        break;
                    } catch (Exception e) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
                            }
                        });
                        e.printStackTrace();
                    }

Well.. It works with string values.. As you see .. there is getInputStream() function.. And it expects string values.. Well this function depends on Socket Class.. But I need a function that gets byte arrray instead of getInputStream() ...

I hope I could explain my problem as well..

Thank you

Upvotes: 1

Views: 702

Answers (1)

bart
bart

Reputation: 2408

You should try socket.getInputStream().read(byte[] b). Server could also send number of bytes before byte array so that client will know how many are expected.

Upvotes: 1

Related Questions