XML在TCP套接字 [英] XML over TCP socket

查看:213
本文介绍了XML在TCP套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果有人能够帮助我,我有一个小问题。

I was wondering if someone would be able to help me with a small problem I am having.

我将收到的是要通过TCP套接字发送一个XML文件。我试图创建一个小的应用程序,它可以作为服务器,通过TCP套接字发送XML文件。然后我就可以开始测试我将接收和处理这个XML文档最初的申请。

I will be receiving a xml file that is going to be sent through a tcp socket. I am trying to create a small application that can act as the server and send a xml file through a tcp socket. I can then start testing my initial application that will be receiving and processing this xml document.

我曾尝试谷歌,并保持运行到这一个死角。

I have tried Google and keep running into dead ends on this one.

推荐答案

一个可能的解决方案是加载XML为一系列字符串或字节数组并发送。字节数组的方法可能是最简洁的,使用networkcomms.net 的应用程序调用网络库发送看起来会是像这样的:

One possible solution is to load the xml either as a series of strings or as a byte array and send that. The byte array approach might be the most concise, using the network library networkcomms.net the application calling the sending would look something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NetworkCommsDotNet;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] bytesToSend = File.ReadAllBytes("filename.xml");
            TCPConnection.GetConnection(new ConnectionInfo("127.0.0.1", 10000)).SendObject("XMLData", bytesToSend);

            Console.WriteLine("Press any key to exit client.");
            Console.ReadKey(true);
            NetworkComms.Shutdown();
        }
    }
}

和服务器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NetworkCommsDotNet;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            NetworkComms.AppendGlobalIncomingPacketHandler<byte[]>("XMLData", (packetHeader, connection, incomingXMLData) => 
            {
                    Console.WriteLine("Received XMLData");
                    File.WriteAllBytes("filename.xml", incomingXMLData);
            });

            TCPConnection.StartListening(true);

            Console.WriteLine("Server ready. Press any key to shutdown server.");
            Console.ReadKey(true);
            NetworkComms.Shutdown();
        }
    }
}

您显然需要下载从网站上N​​etworkCommsDotNet DLL,这样就可以在使用NetworkCommsDotNet添加引用它。也看到在客户端的例子服务器的IP地址是目前127.0.0.1,如果你在同一台机器上运行服务器和客户端这应该工作。欲了解更多信息,也检出入门或的如何创建一个客户端服务器应用程序的文章。

You will obviously need to download the NetworkCommsDotNet DLL from the website so that you can add it in the 'using NetworkCommsDotNet' reference. Also see the server IP address in the client example is currently "127.0.0.1", this should work if you run both the server and client on the same machine. For more information also checkout the getting started or how to create a client server application articles.

这篇关于XML在TCP套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆