TCP / IP learning summary

server:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace TCP服务器端
{
    class Program
    {
        static void Main(string[] args)
        {
            StartServerAsync();
            Console.ReadKey();
        }

        static void StartServerAsync()
        {
            Socket serverSocket = newThe Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
             // this machine the IP: 192.168.1.5 127.0.0.1
             // IpAddress xxx.xx.xx.xx IPEndPoint xxx.xx.xx.xx: Port
             // the IPAddress the iPAddress = new new ipAddress (new new byte [] {192, 168,. 1,}. 5); 
            the iPAddress IPAddress.Parse ipAddress = ( " 192.168.1.5 " ); 
            the IPEndPoint IPEndPoint = new new the IPEndPoint (ipAddress, 88 ); 
            ServerSocket.bind (IPEndPoint ); // bind ip and port number 
            serverSocket.Listen ( 0 ); // start listening port number 

            //= ServerSocket.accept the clientSocket, the Socket (); // receiving a client link 
            serverSocket.BeginAccept (AcceptCallBack, serverSocket); 
        } 
        static the Message MSG = new new the Message ();
         static  void AcceptCallBack (the IAsyncResult Ar) 
        { 
            the Socket serverSocket = ar.AsyncState AS the Socket; 
            the Socket the clientSocket, = serverSocket.EndAccept (Ar); 

            // send a message to the client 
            String msgid = " the Hello client Hello ....! " ;
             byte [] = Data System.Text.Encoding.UTF8.GetBytes(msgStr);
            clientSocket.Send(data);

            clientSocket.BeginReceive(msg.Data, msg.StartIndex, msg.RemainSize, SocketFlags.None, ReceiveCallBack, clientSocket);

            serverSocket.BeginAccept(AcceptCallBack, serverSocket);
        }
        
        static byte[] dataBuffer = new byte[102400];
        static void ReceiveCallBack(IAsyncResult ar)
        {
            Socket clientSocket = null ;
            try
            {
                clientSocket = ar.AsyncState as Socket;
                int count = clientSocket.EndReceive(ar);//
                if (count == 0)
                {
                    clientSocket.Close();
                    return;
                }
                msg.AddCount(count);
                //string msgStr = Encoding.UTF8.GetString(dataBuffer, 0, count);
                //Console.WriteLine("从客户端接收到数据:"+ msgStr);
                msg.ReadMessage();
                //clientSocket.BeginReceive(dataBuffer, 0, 1024, SocketFlags.None, ReceiveCallBack, clientSocket);
                clientSocket.BeginReceive(msg.Data, msg.StartIndex, msg.RemainSize, SocketFlags.None, ReceiveCallBack, clientSocket);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                if (clientSocket != null)
                {
                    clientSocket.Close();
                }
            }
        }

        void StartServerSync()
        {
            Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //本机IP:192.168.1.5  127.0.0.1
            // IpAddress xxx.xx.xx.xx IPEndPoint xxx.xx.xx.xx: Port
             // the IPAddress the IPAddress ipAddress = new new (new new byte [] {192, 168,. 1,}. 5); 
            the IPAddress IPAddress.Parse ipAddress = ( " 192.168.1.5 " ); 
            IPEndPoint IPEndPoint = new new IPEndPoint (ipAddress, 88 ); 
            ServerSocket.bind (IPEndPoint); // bind ip and port number 
            serverSocket.Listen ( 0 ); // start listening port number 
            Socket clientSocket = serverSocket .Accept (); // receiving a client link 

            // send a message to the client 
            String MSG ="Hello client!你好....";
            byte[] data = System.Text.Encoding.UTF8.GetBytes(msg);
            clientSocket.Send(data);

            //接收客户端的一条消息
            byte[] dataBuffer = new byte[1024];
            int count = clientSocket.Receive(dataBuffer);
            string msgReceive = System.Text.Encoding.UTF8.GetString(dataBuffer, 0, count);
            Console.WriteLine(msgReceive);

            Console.ReadKey();
            clientSocket.Close();
            serverSocket.Close();
        }
    }
}

Client:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace TCP客户端
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSocket.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.5"), 88));

            byte[] data = new byte[1024];
            int count = clientSocket.Receive(data);
            string msg = Encoding.UTF8.GetString(data, 0, count);
            Console.Write(msg);

            //while (true)
            //{
            //    string s = Console.ReadLine();
            //    //Console.Write(s);
            //    if (s == "c")
            //    {
            //        clientSocket.Close (); return;
             //     }
             //     clientSocket.Send (Encoding.UTF8.GetBytes (S));
             // } 

            for ( int I = 0 ; I < 100 ; I ++ ) 
            { 
                clientSocket.Send (the Message. GetBytes (i.ToString () + " length " )); 
            } 

            // number of coffee machines number string s = @ "to see is to leave the room to see the scenery Steven blue is the agency fees Bars received a negative space utilities Slovakia knot hair band drooling lose weight faster liberated faster liberation received a customer clicks on the exam utilities hair Ding Shan received a call billing intersection of technology is to leave the room received a call to leave the room Stephen blue coffee machine "; 

            // clientSocket.Send (Encoding.UTF8.GetBytes (S));

            Console.ReadKey();
            clientSocket.Close();
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/krystalstar/p/11372589.html