.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);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值