• [转]Socket面向连接简单实例


    http://songchao.blog.51cto.com/133022/37993

             基于Windows套接字Socket,简单的面向连接的TCP服务器和客户机,实例:

             简单的TCP服务器:
                    在服务器能够向客户机连接传输数据之前,必须做以下几件事情:
                    1: 创建一个套接字;
                    2:将所创建的套接字与本地的IPEndPoint绑定;
                    3:设置套接字为收听模式;
                    4:在套接字上接受接入的连接。
                    源程序如下:程序在VS2005中调试通过
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    namespace FixedTcpServer
    {
        class Program
        {
            /// <summary>
            /// 发送数据
            /// </summary>
            /// <param name="s">发送数据的套接字</param>
            /// <param name="data">缓冲区</param>
            /// <returns></returns>
            private static int SendData(Socket s, byte[] data)
            {
                int total = 0;
                int size = data.Length;
                int dataleft = size;
                int sent;
                while (total < size)
                {
                    sent = s.Send(data, total, SocketFlags.None);
                    total += sent;
                    dataleft -= sent;
                }
                return total;
            }
            /// <summary>
            /// 接受数据
            /// </summary>
            /// <param name="s">接受数据的套接字</param>
            /// <param name="size"></param>
            /// <returns></returns>
            private static byte[] ReceiveData(Socket s, int size)
            {
                int total = 0;
                int dataleft = size;
                byte[] data = new byte[size];
                int recv;
                while (total < size)
                {
                    recv = s.Receive(data, total, dataleft, 0);
                    if (recv == 0)
                    {
                        data = Encoding.ASCII.GetBytes("exit");
                        break;
                    }
                    total += recv;
                    dataleft -= recv;
                }
                return data;
            }

            static void Main(string[] args)
            {
                byte[] data = new byte[1024];//定义缓冲区
                //定义一个服务器
                IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
                Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //绑定并且开始监听
                newsock.Bind(ipep);
                newsock.Listen(10);
                Console.WriteLine("Waiting for a client....");
                //接受客户端的连接请求
                Socket client = newsock.Accept();
                //利用新定义的套接字接受客户端连接请求的返回值来输出客户端的IP,端口信息
                IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
                Console.WriteLine("Connected with {0} at port {1}", newclient.Address, newclient.Port);
                //向客户端发送欢迎信息
                string welcome = "Welcome to my test server";
                data = Encoding.ASCII.GetBytes(welcome);
                int sent = SendData(client, data);//调用发送信息函数
                for (int i = 0; i < 5; i++)
                {
                    data = ReceiveData(client, 9);
                    Console.WriteLine(Encoding.ASCII.GetString(data));
                }
                Console.WriteLine("Disconnected from {0}", newclient.Address);
                client.Close();
                newsock.Close();
            }
        }
    }
     
             简单的TCP客户机:
                    基于上面的TCP服务器,因此可以创建一个简单的TCP客户机与它进行通信。
                    创建与TCP服务器相连的客户机如下:
                    1:创建一个套接字;
                    2:将套接字与远程服务器地址相连。
                    源代码如下:此程序在VS2005中调试通过。
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    namespace FixedTcpClient
    {
        class Program
        {
            private static int SendData(Socket s, byte[] data)
            {
                int total = 0;
                int size = data.Length;
                int dataleft = size;
                int sent;
                while (total < size)
                {
                    sent = s.Send(data, total, dataleft, SocketFlags.None);
                    total += sent;
                    dataleft -= sent;
                }
                return total;
            }
            private static byte[] ReceiveData(Socket s, int size)
            {
                int total = 0;
                int dataleft = size;
                byte[] data = new byte[size];
                int recv;
                while (total < size)
                {
                }
            }
            static void Main(string[] args)
            {
            }
        }
    }
           以上程序可以在一台电脑中进行测试,先打开服务器,再打开客户机。希望这两个程序能帮网友理解一点的Socket知识。由于工作原因只是放上两个程序的源码。具体解释,可以看MSDN,希望网友们不要骂我。
  • 相关阅读:
    Uniapp px与rpx的转换
    主流布局Flex
    Uniapp 父组件调用子组件方法给子组件动态传值
    数据仓库(七)——DWD 层
    Kafka Stream
    回溯算法
    数仓重点
    数据仓库(五)——ODS层
    数据仓库(六)——DMI 层
    Spark (二)核心编程
  • 原文地址:https://www.cnblogs.com/lauer0246/p/1383027.html
Copyright © 2020-2023  润新知