简体   繁体   English

如何通过网络发送序列化对象?

[英]How do you send a serialized object over net?

i am trying to build a chat! 我正在尝试建立聊天! now my goal is to receive input from the user, (which will be fed to a function in a class), save it and send the object to the user over the net. 现在,我的目标是从用户那里接收输入(将输入输入到类中的函数),将其保存并通过网络将对象发送给用户。

here is my code so far: 到目前为止,这是我的代码:

namespace ConsoleApplication1
{
    class Program
    {       
        static void Main(string[] args)
        {
            TcpListener server = new TcpListener(IPAddress.Any, 5000);
            server.Start();
            Console.WriteLine("Server started");
            int a = 0;

            while (true)
            {
                TcpClient connection = server.AcceptTcpClient();
                Console.WriteLine("connection accepted");

                ThreadPool.QueueUserWorkItem(ProssecClient, connection);
            }
        }

        public static void ProssecClient(object o)
        {
            TcpClient connection = o as TcpClient;
            if (connection == null)
                return;
            StreamReader sr = new StreamReader(connection.GetStream());
            StreamWriter sw = new StreamWriter(connection.GetStream());
            string word = "";
            savedObject saved = new savedObject();

            try
            {
                while (true)
                {
                    sw.WriteLine(sr.ReadLine());
                    sw.Flush();

                    // here the server should read and retrieve, 
                    // everything that it gets to every user that logs in.
                }
            }
            catch
            { 
                Console.WriteLine("client left");
            }
        }           
    }
}

i have everything saved in the binaryFormatter, how do i send it to the user to receive? 我已将所有内容保存在binaryFormatter中,如何将其发送给用户以接收?

client side code: 客户端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient connection = new TcpClient("127.0.0.1", 5000);
            StreamReader sr = new StreamReader(connection.GetStream());
            StreamWriter sw = new StreamWriter(connection.GetStream());
            savedObject saved = new savedObject();
            Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);
            BinaryFormatter bformatter = new BinaryFormatter();
            string word = "";
            string allwords = "";

            Thread Listen = new Thread(deserialise);
            Listen.Start();

            while (true)
            {
                word = Console.ReadLine();
                allwords = saved.AllWords(word);
                sw.WriteLine(allwords);
                sw.Flush();
                Console.WriteLine(sr.ReadLine());

                //Serialize
                //bformatter.Serialize(stream, saved);
                //stream.Close();

                //sw.WriteLine(saved);
            }
        }
    }

    public static void deserialise()
    {
        //Deserialize
        //if (File.Exists("EmployeeInfo.osl"))
        //{
        //    stream = File.Open("EmployeeInfo.osl", FileMode.Open);
        //    bformatter = new BinaryFormatter();

        //    saved = (savedObject)bformatter.Deserialize(stream);
        //    stream.Close();
        //}

    }
}

[Serializable()]
class savedObject : ISerializable
{
    public string allwords;

    public string AllWords(string words)
    {
        allwords += words + " ";
        return allwords;
    }

    public void Words(SerializationInfo info, StreamingContext ctxt)
    {
        info.AddValue("RetrievedWord", allwords);
    }


    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        allwords = (String)info.GetValue("RetrievedWord", typeof(string));
    }
}

Consider WCF. 考虑WCF。

It handles all communication issues including security, different protocols, etc. from a consistent high-level perspective. 它从一致的高层角度处理所有通信问题,包括安全性,不同的协议等。

It is pretty much the standard for communication in .Net and encompasses and supersedes the older more low-level technologies. 它几乎是.Net中通信的标准,它包含并取代了较旧的更底层技术。

For a good tutorial of how to build a chat service using WCF see WCF / WPF Chat Application 有关如何使用WCF构建聊天服务的良好教程,请参阅WCF / WPF聊天应用程序。

In the ProcessClient method: 在ProcessClient方法中:

TcpClient client = (TcpClient) connection;

using(StreamWriter streamWriter = new StreamWriter(tcpClient.GetStream()))
{
    BinaryFormatter binaryFormatter = new BinaryFormatter();
    binaryFormatter.Serialize(streamWriter, savedObject);
}

You could use a SOAP Arhitecture (use XML Serialization not Bynary serialization)it would be much easyer to implement. 您可以使用SOAP Arhitecture(使用XML序列化而不是Bynary序列化),这将易于实现。 Or if you need a peer to peer chat code here . 或者,如果您需要此处的点对点聊天代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将ASP.NET中的xml序列化对象发送到Silverlight - How to send an xml serialized object from ASP.NET to Silverlight 当使用Full .net Framework序列化初始对象时,如何将二进制数据反序列化为.net Core中的对象? - How do I deserialize binary data into an object in .net Core, when the initial object was serialized with Full .net Framework? 如何让Web Api将Json.net序列化的字符串对象正确地发送回客户端? - how to have Web Api send Json.net Serialized string object back to client Correctly? ASP.NET-如何更改JSON序列化的方式? - ASP.NET - how do you change the way a JSON gets serialized? 如何在.net中管理大型序列化数据对象 - how to manage large serialized data object in .net 您如何遍历.Net中某个班级的孩子? - How do you iterate over children of a class in .Net? 你如何从 ASP.NET 发送大量电子邮件? - How do you send mass emails from ASP.NET? C#如何将序列化的.NET对象编译为.NET程序集 - C# How to compile a serialized .NET object to a .NET Assembly 如何将XML数据反序列化为从列表序列化的列表? - How do you deserialize XML data to a List that was serialized from a List? 如何正确发送 json object 到 php 后端 - How do you send a json object to php backend properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM