c# socket tcp 协议应用


一、socket tcp使用流程简介

服务端:
1.1.1 申明ip地址
1.1.2 申明端口
1.1.3 申明socket对象,并指定socket类型及指定协议使用tcp
1.1.4 绑定ip及端口至socket对象上
1.1.5 启动监听
1.1.6 申明临时socket 接收客户端连接
1.1.7 向1.1.6上发送数据
1.1.8 从1.1.6上接收数据,数据接收完毕后关闭1.1.6的连接

客户端:
1.2.1 申明待连接的ip地址
1.2.2 申明socket对象,并指定socket类型及指定协议使用tcp
1.2.3 绑定ip地址至socket对象上
1.2.4 通过socket对象连接指定的ip地址和端口
1.2.5 接收数据
1.2.6 发送数据
1.2.7 关闭连接

 

二、socket tcp使用举例说明

服务端代码:

      #region  TCP 对外服务

            int recv;
            byte[] data = new byte[1024];
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9008);
            Socket newSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);
            newSock.Bind(ipep);
            newSock.Listen(100);
             
            while(true)
            {
                Socket tmpSocket = newSock.Accept();
                tmpSocket.Send(Encoding.ASCII.GetBytes("this is server send message!"));

                while (true)
                {
                    try
                    {
                        //接收客户端消息
                        byte[] t = new byte[20];
                        int tmpInt = tmpSocket.Receive(t);
                        Console.WriteLine("共接收到字节数{0}", tmpInt);
                        Console.WriteLine("接收内容:{0}", Encoding.ASCII.GetString(t).Trim());
                        //当数据接收完毕后,关闭此客户端连接
			if (tmpInt == 0)
                        {
                            tmpSocket.Shutdown(SocketShutdown.Both);
                            tmpSocket.Close();
                            break;
                        }
                    }
                    catch
                    {
                        
                        tmpSocket.Shutdown(SocketShutdown.Both);
                        tmpSocket.Close();
                        break;
                    }
                 }
                
            }
            

            #endregion

 

客户端代码:

        #region tcp 客户端

                IPAddress serverIP = IPAddress.Parse("127.0.0.1");
                IPEndPoint serverIPEndPoint = new IPEndPoint(serverIP, 9008);
                Socket t = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    t.Connect(serverIPEndPoint);
                    Console.WriteLine("连接成功!");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    return;
                }

                byte[] receiveInfo = new byte[1024];
                int receiveLength = t.Receive(receiveInfo);
                Console.WriteLine("接收服务器消息的长度:{0}", receiveLength);
                Console.WriteLine("接收消息内容:{0}", Encoding.ASCII.GetString(receiveInfo).Trim());
 
                for (int i = 0; i < 2000; i++)
                {  
                    Console.WriteLine("发送数据{0}", i.ToString());
                    try
                    {
                       // Thread.Sleep(1000);
                        t.Send(Encoding.ASCII.GetBytes("test"+i.ToString()+System.Guid.NewGuid().ToString()));

                    }
                    catch (Exception e)
                    {
                         Console.WriteLine("异常:{0}", e.ToString());                       
                    }

              


                } 
                t.Shutdown(SocketShutdown.Both);
                t.Close();
                Console.WriteLine("数据发送完毕!,按任意键退出!");
                Console.ReadLine();



                //连接服务器


                #endregion

 

相关阅读:
c# socket SocketType枚举简介
c# socket 应用(一) UDP
Socket简介