• 全部
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm&WPF
  • C#开发新技术
  • 问答

c# Socket 中 如何发送接受 结构 ?是用流好操作还是byte[]好操作?

lirenzhao 2003-11-11 11:59:31
还有就是我的SOCKET服务器要有很多个客户端连接(长连接),如何处理
nt recv = 0;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any,9050);
Socket newsock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(10);
listBox1.Items.Add("Waiting For a Client");

Socket client = newsock.Accept();
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
listBox1.Items.Add("Connected with :" + clientep.ToString());
string welcome = "Welcome to my Socket Server!";
data = Encoding.ASCII.GetBytes(welcome);
client.Send(data,0,data.Length,SocketFlags.None);
while(true)
{
data = new byte[1024];
recv = client.Receive(data);
if (recv == 0)
break;
listBox1.Items.Add(Encoding.ASCII.GetString(data,0,recv));
client.Send(data,0,recv,SocketFlags.None);

}
listBox1.Items.Add("Disconnected from " + clientep.ToString());
client.Close();
newsock.Close();

我知道我的这种方式肯定不行,大家指点一下。
...全文
159 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ymblue_boy 2003-11-28
public byte[] getBytes()
{
byte[] bs = new byte[48];
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(this.Version);
bw.Write(this.Name);
return ms.ToArray();
}

public PacketHeader(byte[] bs)
{
MemoryStream ms = new MemoryStream(bs,0,bs.Length);
BinaryReader bw = new BinaryReader (ms);
this.Version = bw.ReadUInt32();
this.Name = bw.ReadBytes(bs.Length - 4);
}
回复
ymblue_boy 2003-11-28
public byte[] getBytes()
{
byte[] bs = new byte[48];
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(this.Version);
bw.Write(this.Name);
return ms.ToArray();
}

public PacketHeader(byte[] bs)
{
MemoryStream ms = new MemoryStream(bs,0,bs.Length);
BinaryReader bw = new BinaryReader (ms);
this.Version = bw.ReadUInt32();
this.Name = bw.ReadBytes(bs.Length - 4);
}
回复
lirenzhao 2003-11-25
tiqian
回复
lirenzhao 2003-11-24
lovered 你的序列化的方法能够被c正确的解释吗?
还有你什么回北京?
回复
lirenzhao 2003-11-23
hehe
lao da
jiu shi yao li mian de neirong a?
please complete your methord.it's my want
回复
lovered 2003-11-21
byte[]没写完sorry
class classname
{
public int field;
public string field2;

public classname(byte[] data)
{
//将byte[]内容解析出来后赋值给field,field2
}
public byte[] getbyte()
{
//把各字段转成byte[]
}
}
。。。
后面是转帖
回复
lovered 2003-11-21
用流比较容易进行序列化
byte[]的话可以自己封装个类
例如:
class employee
{

}


(转帖)
结构文件:Astruct.cs
//csc /t:library Astruct.cs
using System;
[Serializable]
public struct Astruct{
public int seq;//结构的字段最好用public修饰
public char[] name;
public ulong len;
public char[] data;
}

服务端:
//compile with /r:Astruct.dll
using System;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;

class dropmulticast
{
public static void Main()
{
Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//用tcp协议 IPEndPoint local = new IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList[0],8080);//监听8080端口
socket.Bind(local);
socket.Listen(3);//允许3个客户连接
while (true) {
Socket accept = socket.Accept();//接受连接的客户 BinaryFormatter bf;
bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
Astruct ast = new Astruct();
ast.seq = 4;
ast.name = new char[]{'n', 'a', 'm', 'e'};
ast.len = 4;
ast.data = new char[]{'d', 'a', 't', 'a'};
bf.Serialize(stream, ast);
byte[] buff = stream.ToArray();
accept.Send(buff, buff.Length, 0);
accept.Close();
}
}
}

客户端:
//compiler with /r:Astruct.dll
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

class Accept
{
public static void Main(String[] Args)
{
Socket client = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint remote = new IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList[0],8080);
client.Connect(remote);//与服务端连接
NetworkStream stream = new NetworkStream(client);//取得服务端发送的数据
BinaryFormatter bf;
bf = new BinaryFormatter();
Astruct reci = (Astruct) bf.Deserialize(stream);//反序列化数据
client.Close();
Console.WriteLine(reci);
Console.Write("name is :");
Console.WriteLine(reci.name);
Console.WriteLine("seq is :" + reci.seq);
Console.WriteLine("len is :" + reci.len);
Console.Write("data is :");
Console.WriteLine(reci.data);
}
}


回复
lirenzhao 2003-11-20
如果我有这样的一个类该如何发送接受呢?
class Packetheader
{
uint version;
char[] password; //8
word Command;
int bodyLength;
}
class Packet
{
Packetheader ph;
char[] body; //不定长,根据包头的长度
}
回复
lbywyj 2003-11-11
如果要传输对象,必须用串行化,如果不传输对象,用bye[]好
关于串行化,请看:
http://expert.csdn.net/Expert/topic/2097/2097864.xml?temp=.6771204
回复
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2003-11-11 11:59
社区公告

让您成为最强悍的C#开发者