.Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务2[使用IE浏览本页]

直接使用Socket做客户端,采用Udp

客户端代码:

示范代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using TcpLabCommon;
using System.Net;

namespace UdpLabClient1
{
    class Program
    {
        static void Main(string[] args)
        {

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            EndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6000);
            //socket.Bind(endPoint);

            NetPacketService packetSvr = new NetPacketUdpService(socket, endPoint);

            for (int i = 0; i < 10; i++)
            {

                packetSvr.SendObject(new Book { Title = "Java编程思想_" + i, Price = i + 1 });


                packetSvr.SendText("jiang_" + i);
            }

            packetSvr.SendFile(@"E:\简单工厂参考.txt");



            Console.ReadLine();
        }
    }
}

 

 

服务端代码:

示范代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using TcpLabCommon;
using System.IO;

namespace UdpLabSvr1
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),6000);
            serverSocket.Bind(endPoint);
            
            NetPacketService packetSvr = new NetPacketUdpService(serverSocket,endPoint);
            
            do
            {
                NetPacket packet = packetSvr.ReceivePacket();
                switch (packet.PacketHead.PType)
                {
                    case PacketType.STRING:
                        Console.WriteLine(Convert.ToString(packet.Data));
                        break;
                    case PacketType.BINARY:
                        NetFile f = packetSvr.ParseFile((Byte[])packet.Data);
                        Console.WriteLine("成功接收文件:" + f.FileName);
                        FileStream fs = new FileStream(f.FileName, FileMode.Create);
                        fs.Write(f.Content, 0, f.Content.Length);
                        fs.Flush();
                        fs.Close();
                        break;
                    case PacketType.COMPLEX:
                        Book book = packet.Data as Book;
                        Console.WriteLine(book.Title + "\t" + book.Price);
                        break;
                    default:
                        break;
                }
            } while (true);
        }
    }
}

 

//----------------------------------------------------------------------------------------------------------------------------------------

采用UdpClient做客户端和服务端,简化代码:

客户端代码:

示范代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using TcpLabCommon;

namespace UdpLabClient2
{
    class Program
    {
        static void Main(string[] args)
        {
            //Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6000);
            //socket.Bind(endPoint);

            UdpClient udpClient = new UdpClient();

            NetPacketService packetSvr = new NetPacketUdpService(udpClient.Client, endPoint);

            for (int i = 0; i < 10; i++)
            {

                packetSvr.SendObject(new Book { Title = "Java编程思想_" + i, Price = i + 1 });


                packetSvr.SendText("jiang_" + i);
            }

            packetSvr.SendFile(@"E:\简单工厂参考.txt");



            Console.ReadLine();
        }
    }
}

 

服务端代码:

示范代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using TcpLabCommon;
using System.IO;

namespace UdpLabSvr2
{
    class Program
    {
        static void Main(string[] args)
        {
            //Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6000);
            //serverSocket.Bind(endPoint);

            UdpClient udpClient = new UdpClient(endPoint);


            NetPacketService packetSvr = new NetPacketUdpService(udpClient.Client, endPoint);

            do
            {
                NetPacket packet = packetSvr.ReceivePacket();
                switch (packet.PacketHead.PType)
                {
                    case PacketType.STRING:
                        Console.WriteLine(Convert.ToString(packet.Data));
                        break;
                    case PacketType.BINARY:
                        NetFile f = packetSvr.ParseFile((Byte[])packet.Data);
                        Console.WriteLine("成功接收文件:" + f.FileName);
                        FileStream fs = new FileStream(f.FileName, FileMode.Create);
                        fs.Write(f.Content, 0, f.Content.Length);
                        fs.Flush();
                        fs.Close();
                        break;
                    case PacketType.COMPLEX:
                        Book book = packet.Data as Book;
                        Console.WriteLine(book.Title + "\t" + book.Price);
                        break;
                    default:
                        break;
                }
            } while (true);
        }
    }
}
阅读更多
个人分类: 网络通信
想对作者说点什么? 我来说一句

Socket通信之序列化对象通信

2014年11月11日 1KB 下载

没有更多推荐了,返回首页

不良信息举报

.Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务2[使用IE浏览本页]

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭