Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to connect an android application to a C# server which would receive a list of items from the application.
Android code:
socket = new Socket(dstAddress, dstPort);

                ByteArrayOutputStream byteArrayOutputStream =
                        new ByteArrayOutputStream(1024);
                byte[] buffer = new byte[1024];

                int bytesRead;
                InputStream inputStream = socket.getInputStream();
                OutputStream outputStream = socket.getOutputStream();
                // bufferwriter it sends to the server ->OutputStreamWriter -> OutputStream -> socket.getOutputStream();
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
                // to send
                bufferedWriter.write(response);
                // to refresh

                bufferedWriter.flush();
                socket.close();

C# code:
TcpListener listen = new TcpListener(IPAddress.Any, 1237);
            listen.Start();
            TcpClient client = listen.AcceptTcpClient();
            Stream sckStream = client.GetStream();
            StreamReader str = new StreamReader(sckStream);
            while (client.Connected)
            {
                string receive;
                try
                {
                    while (( receive = str.ReadLine()) != null)
                    {
                        
                        Action action = () => richTextBox1.AppendText(receive + "\n");
                        richTextBox1.Invoke(action);
                        receive = null;
                    }
                }
                catch (Exception x)
                {
                    MessageBox.Show(x.ToString());
                }
                client.Close();
                //MessageBox.Show("Csfha");
            }

The application writes a series of strings
Eg: info1: aaaa
info2:bbb
info3:ccc

99.9% of the time , I receive only info1:aaa, and the client connection is closed, so the readLine() returns null right? The remaining 0.1% of the time I receive some data present in the list( not all ) .

Any help or suggestion is appreciated.Thanks.
Posted

1 solution

This is a TCP connection. Meaning what you send is not message-based, its stream based. You are guaranteed to receive data in the order of which it was sent, but the data is in a stream, and you will receive any arbitrary amount of information when the recieve thread is unblocked. You have to implement your own message-based system.

I would recommend you ditch the socket implementation and lookup on how making a WCF or Web API service, it will save you hours of development work, it completely abstracts all this network stuff for you. Its great you will fall in love.

However, if you want to continue this implementation you need to prefix the data you send with an int to specify the length of the data you are sending, on the other end you will need to read the int and then the amount of bytes it specifies.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900