从网络流读取数据. [英] Read Data From Network Stream.

查看:296
本文介绍了从网络流读取数据.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个有关从网络流读取数据的问题.

我正在使用套接字编程在同一台计算机上运行的两个进程之间发送数据.

从过程1写入数据:

Hi Everyone,

I have a question regarding reading data from Network stream.

I am sending data between the two processes which are running on the same machine using Socket Programming.

To write Data from Process 1:

Int32 port = 13000;
TcpClient client = new TcpClient("127.0.0.1", port);

Byte[] data = System.Text.Encoding.ASCII.GetBytes
(WorkReqNbr.ToString());
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
stream.Close();
client.Close();


要从流程2中读取数据:


To Read data from Process 2:

Dim nbr As Integer = 0
Dim port As Integer = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
server = New TcpListener(localAddr, port)
server.Start()
Dim bytes As [Byte]() = New [Byte](256) {}
Dim data As String = String.Empty
While (True)
Dim client As TcpClient = server.AcceptTcpClient()
data = Nothing
Dim stream As NetworkStream = client.GetStream()
Dim i As Integer
i = stream.Read(bytes, 0, bytes.Length)
data = System.Text.Encoding.ASCII.GetString(bytes)



问题:
在上面的代码中,GetString(bytes)以以下格式返回值:
数据=测试;
现在,此字符不被视为有效字符串,因为缺少结尾(双引号).

谁能告诉我我要去哪里错了?

预先感谢,
Dilip Kumar V.



Problem:
In the above code GetString(bytes) returns value in below format:
data = "test ;
Now this is not treated as a valid string because end "(double quote) is missing.

Can any one tell me, where i am going wrong?

Thanks in advance,
Dilip Kumar V.

推荐答案

您可以尝试将字符串保存到文本文件吗?
IIRC,套接字添加回车符来标记流/传输的结束,而且我似乎很多时候VS Debugger在显示此字符时遇到问题,如果是这样,您将需要删除该字符并完成操作.
can you try saving the string to a text file?
IIRC, Sockets add the carriage return character to mark the end of stream/transmission, and I have seem that many times VS Debugger has problems displaying this, if so, you''ll need to remove the character and you''re done.


这篇关于从网络流读取数据.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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