这个小项目背后的想法是开发一个聊天应用程序,不同的是,我想发送对象,而不仅仅是普通的字符串。到目前为止,这就是我所拥有的。
如果我在构造函数上反序列化,它就能正常工作(UserDTO现在只有2个字符串字段),然而,我计划让多个客户端随时向服务器发送数据。我很难理解它是如何工作的,以及如何修复错误(就像这样,它给出了一个“抛出了'System.OutOfMemoryException‘类型的异常”。即使在阅读了MS的文档之后,我也想从你们那里得到一些想法。
对于任何试图编译的人,请注意: Binaryformatter有一种这样做的方法:假设UserDTO有属性string Name,string Email将这个类应用到客户端和服务器,您必须使用类库构建它,并将它的引用添加到这两个项目中,因为不知何故,Binaryformatter说即使您在两个项目中创建相同的类,反序列化也会声明它无法映射对象。我将在下面留下一个我正在使用的客户端的样本。
服务器:
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");
}
}发布于 2016-09-08 00:32:50
不是执行所有的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()中时,它将不再有数据可供读取。实际上,如果您的byte[] readBuffer不能容纳超过2000个字节,那么您的DTO可能包含您需要的所有数据。如果是这种情况,那么您应该能够使用readBuffer中的字节来进行反序列化。
https://stackoverflow.com/questions/39373748
复制相似问题