在vb.net中怎样用socket传送自定义结构体

discoverer1978 2004-12-10 09:08:56
我在程序中有一个自定义结构体:
structure str
dim a() as byte
dim b() as byte
end structure
请问怎样使用socket类在两个程序之间进行传递?
...全文
203 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
discoverer1978 2004-12-15
  • 打赏
  • 举报
回复
回OneDotRed(武装到眼神) :
代码已用,但是仍有问题:
ms.Read( buffer , 0 , buffer.Length )执行过后,buffer仍为空。不知为何?
我的代码:
<System.Serializable()> Public Structure DictFileClass
Dim FileName() As Byte
Dim FileType() As Byte
Dim FileContent() As Byte
End Structure

Dim aa As New DictFileClass

Dim RenewFile As New IO.FileStream("c:\billd.txt", IO.FileMode.OpenOrCreate, IO.FileAccess.Read)
aa.FileName = System.Text.Encoding.Default.GetBytes(Disk.GetFileName("c:\billd.txt").ToCharArray)
Dim Fssize(RenewFile.Length) As Byte
Dim StrRead As New IO.BinaryReader(RenewFile)
StrRead.Read(Fssize, 0, Fssize.Length)
aa.FileContent = Fssize
aa.FileType = System.Text.Encoding.Default.GetBytes("ini".ToCharArray)

Dim Formatter As New Formatters.Binary.BinaryFormatter
Dim Ms As New IO.MemoryStream
Formatter.Serialize(Ms, aa)
Dim buffer(Ms.Length) As Byte
Ms.Read(buffer, 0, buffer.Length)

Client.GetStream.Write(buffer, 0, buffer.Length)
Ms.Close()
ballnan 2004-12-13
  • 打赏
  • 举报
回复
参考一下吧
http://www.codeproject.com/useritems/QMailClient.asp
兔子-顾问 2004-12-13
  • 打赏
  • 举报
回复
up
OneDotRed 2004-12-13
  • 打赏
  • 举报
回复
<Serealizable>
Public Structure B
..
End Structure

Sub Main
Dim b as new B();
Dim formatter as new BinaryFormatter()
Dim MemoryStream ms as new MemoryStream()

formatter.Serealize( ms , b )

Dim buffer as new byte( ms.Length )
ms.Read( buffer , 0 , buffer.Length )
ms.Close()


process byte array...
End Sub
discoverer1978 2004-12-13
  • 打赏
  • 举报
回复
虽然c语言很好,但我只懂一点,主要还是用vb.net,只好回去慢慢翻译了,呵呵,多谢!
yanransoft 2004-12-12
  • 打赏
  • 举报
回复
[Serializable]
struct B
{
public string MyStr;
}
class Program
{
static void Main(string[] args)
{
B b=new B();
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("c:\\MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, b);
stream.Close();
}
}
discoverer1978 2004-12-12
  • 打赏
  • 举报
回复
关键就是不知道怎么将自定义结构转化为流,自定义结构不能直接转化为Byte型数组。我这个自定义结构是用于存储一个文件的内容的,a()存储文件名,b()存储文件内容。
chenzhanyiczy 2004-12-11
  • 打赏
  • 举报
回复
可以用串行化传送然后再另外一端反串行化
rustical 2004-12-11
  • 打赏
  • 举报
回复
socket类 传输的是 字符流 ,所以你要先序列化为流 !
你如果要传输对象用 Remoting 或 Webservices !
Remoting 和 Webservices 可以传输 ( 可序列化的对象)

16,556

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧