这个小项目背后的想法是开发一个聊天应用程序,不同之处在于我想发送对象而不只是普通字符串。到目前为止,这就是我所拥有的。
如果我在构造函数上反序列化,它就可以正常工作(UserDTO现在仅具有2个字符串字段),但是,我计划让多个客户端在他们希望的任何时候将数据发送到服务器。即使在阅读了MS的文档之后,我仍然难以理解它的工作原理以及如何解决该错误(像这样,它在Deseralize行上给出了“引发类型为'System.OutOfMemoryException'的异常。”),并且喜欢你们的一些想法。
请注意尝试编译此文件的人员:Binaryformatter的操作方法如下:假设UserDTO具有字符串名称,字符串电子邮件的属性。
将此类应用于客户端和服务器时,必须使用类库对其进行构建,并将其引用添加到两个项目中,因为不知何故,binaryformater表示即使在两个项目中都创建相同的类,反序列化也无法映射该类。宾语。我将在下面留下我正在使用的客户端示例。
服务器:
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");
}
}
最佳答案
而不是执行所有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);
}
我的猜测是,您的信息流位置已经移动了,即使不是最后。当您将其传递到
Deserialize()
时,不再有任何数据可供读取。实际上,如果DTO不能容纳超过2000个字节,则byte[] readBuffer
可能具有所需的所有数据。如果是这种情况,那么您应该能够使用readBuffer
中的字节反序列化。