关闭

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

标签: 网络socket编程.net通讯ie
563人阅读 评论(0) 收藏 举报
分类:

直接使用Socket做服务器端和客户端,采用Tcp协议:

客户端代码:

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

namespace TcpLabClient1
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect("127.0.0.1", 6000);
            NetworkStream netStream = new NetworkStream(socket);
            NetPacketService packetSvr = new NetPacketTcpService(netStream);

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

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


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

            packetSvr.SendFile(@"E:\WorkSpace\VCLabProjects\TcpLabSpace.zip");



            Console.ReadLine();
            
        }
    }
}

 

 

服务器端代码:

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

namespace TcpLabSvr1
{
    class Program
    {       
        static void Main()
        {
            Socket serverSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),6000);
            serverSocket.Bind(endPoint);
            serverSocket.Listen(3);
            Socket clientSocket = serverSocket.Accept();
            NetworkStream netStream = new NetworkStream(clientSocket);
            NetPacketService packetSvr = new NetPacketTcpService(netStream);
            
            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);

        }
    }
}

 

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

使用TcpListener做服务器端、TcpClient做客户端:

客户端代码:

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

namespace TcpLabClient2
{
    /// <summary>
    /// 客户端使用TcpClient
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            
            TcpClient client = new TcpClient("127.0.0.1",6000);
            NetworkStream netStream = client.GetStream();
            NetPacketService packetSvr = new NetPacketTcpService(netStream);

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

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


                packetSvr.SendText("jiang_" + i);
            }
            packetSvr.SendText("测试一下1");
            packetSvr.SendText("测试一下2");
            packetSvr.SendText("测试一下3");
            packetSvr.SendFile(@"E:\简单工厂参考.txt");
            


            Console.ReadLine();
        }
    }
}

 

 

服务端代码:

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

namespace TcpLabSvr2
{
    /// <summary>
    /// 服务器端使用TcpListener
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener svrListen = new TcpListener(IPAddress.Parse("127.0.0.1"),6000);
            svrListen.Start(3);
            TcpClient client = svrListen.AcceptTcpClient();
            NetworkStream netStream = client.GetStream();
            NetPacketService packetSvr = new NetPacketTcpService(netStream);

            do
            {
                NetPacket packet = packetSvr.ReceivePacket();
                switch (packet.PacketHead.PType)
                {
                    case PacketType.STRING:
                        Console.WriteLine(packet.Data.ToString());
                        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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:298323次
    • 积分:5031
    • 等级:
    • 排名:第5575名
    • 原创:198篇
    • 转载:35篇
    • 译文:0篇
    • 评论:44条
    最新评论