这个小项目背后的想法是开发一个聊天应用程序,区别在于我想发送对象而不是简单的字符串。到目前为止,这就是我所拥有的。
如果我对构造函数进行反序列化,它可以正常工作(UserDTO现在只有2个字符串字段),但是,我计划让多个客户端随时将数据发送到服务器。我很难理解它是如何工作的以及如何修复错误(就像这样,它在“Deseralize”行中提出了“类型'System.OutOfMemoryException'的异常。”即使在阅读了MS的文档之后我也是如此喜欢你们的一些想法。
注意尝试编译它的人:Binaryformatter有一种方法可以这样做:假设UserDTO有属性字符串Name,string Email 将此类应用于客户端和服务器,您必须使用类库构建它并将此引用添加到两个项目中,因为无论如何二进制格式化表示即使您在两个项目中创建相同的类,反序列化声明它也无法映射宾语。我将在下面留下我正在使用的客户端样本。
服务器:
class Program {
const int serverPort = 60967;
static List<UserConnection> clientList = new List<UserConnection>();
static TcpListener listener;
static Thread listenerThread;
static void Main(string[] args) {
listenerThread = new Thread(new ThreadStart(DoListen));
listenerThread.Start();
Console.WriteLine("Server Started");
//while (true) {
string a = Console.ReadLine()
//}
}
static void DoListen() {
try {
listener = new TcpListener(System.Net.IPAddress.Any, serverPort);
listener.Start();
Console.WriteLine("Listening [...]");
do {
UserConnection client = new UserConnection(listener.AcceptTcpClient());
//clientList.Add(client);
Console.WriteLine("New connection found");
} while (true);
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
}
public class UserConnection {
private TcpClient clientInfo;
private byte[] readBuffer = new byte[2000];
const int READ_BUFFER_SIZE = 2000;
public UserConnection(TcpClient client) {
clientInfo = client;
clientInfo.GetStream().BeginRead(readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(StreamReceiver), null);
}
private void StreamReceiver(IAsyncResult ar) {
try
{
if (client.GetStream().CanRead) {
lock (clientInfo.GetStream()) {
var strm = clientInfo.GetStream();
int BytesRead = clientInfo.GetStream().EndRead(ar);
BinaryFormatter formatter = new BinaryFormatter();
var mydat = (UserDTO)formatter.Deserialize(strm);
}
lock (clientInfo.GetStream()) {
clientInfo.GetStream().BeginRead(readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(StreamReceiver), null);
}
}
catch (Exception e) {
Console.WriteLine(ex.ToString());
}
}
客户端:
class Program {
static void Main(string[] args) {
ConnectResult("localhost", 60967);
Console.ReadLine();
}
}
static string ConnectResult(string ip, int port) {
try {
TcpClient client = new TcpClient(ip, port);
AttemptLogin(client);
return "Connection Succeeded";
}
catch (Exception ex) {
return "Server is not active. Please start server and try again. " + ex.ToString();
}
}
static void AttemptLogin(TcpClient client) {
UserDTO obj = new UserDTO("email", "username");
IFormatter formatter = new BinaryFormatter();
var stream = client.GetStream();
formatter.Serialize(stream, obj);
Console.WriteLine("Sent Object");
}
}
答案 0 :(得分:1)
不要执行所有type="button"
次调用,只需抓取流并将其传递到BeginRead()
方法。
BinaryFormatter.DeSerialize()
我的猜测是你的流位置已经移动,如果不是最后的话。当您将其传递到public UserConnection(TcpClient client) {
clientInfo = client;
//clientInfo.GetStream().BeginRead(readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(StreamReceiver), null);
var strm = clientInfo.GetStream();
BinaryFormatter formatter = new BinaryFormatter();
var mydat = (UserDTO)formatter.Deserialize(strm);
}
时,就不再有数据可供阅读。实际上,如果您的DTO不能容纳超过2000个字节,那么您的Deserialize()
可能包含您想要的所有数据。如果是这种情况,那么您应该能够使用byte[] readBuffer
中的字节来反序列化。