如何读取整个数据而不是C#应用程序中的一行。 [英] How do I read the entire data rather than one line on my C# application.

查看:204
本文介绍了如何读取整个数据而不是C#应用程序中的一行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将一个Android应用程序连接到一个C#服务器,该服务器将从应用程序接收一个项目列表。

Android代码:

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#代码:


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");
            }



应用程序写一系列字符串

例如:info1:aaaa

info2: bbb

info3:ccc



99.9%的时间,我只收到info1:aaa,客户端连接已关闭,所以readLine()返回null对吗?剩余0.1%的时间我收到列表中的一些数据(不是全部)。



任何帮助或建议都表示赞赏。谢谢。


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.

推荐答案

这是一个TCP连接。这意味着您发送的内容不是基于消息的,而是基于流的。您可以保证按发送顺序接收数据,但数据在流中,当接收到的线程被解除阻塞时,您将收到任意数量的信息。你必须实现自己的基于消息的系统。



我建议你放弃套接字实现并查找如何制作WCF或Web API服务,它会节省你花了几个小时的开发工作,它完全抽象出所有这些网络的东西给你。它会很棒你会爱上它。



但是,如果你想继续这个实现,你需要在你发送的数据前加一个int来指定它的长度。您要发送的数据,另一方面,您需要读取int,然后读取它指定的字节数。
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.


这篇关于如何读取整个数据而不是C#应用程序中的一行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆