. NET Beauty reading note 10 (Network programming One)

Source: Internet
Author: User
Tags getstream

Network Programming (Socket)

Here the network programming to do a simple collation, to fill the gaps in their own knowledge. If you want to know, please refer to Zhang Ziyang. The beauty of net.
***
Concept Storytelling

  • TCP: Connection-oriented transport protocol (one of the network protocols)
  • Remote host: Remote access for cross-process access
  • Sockets: The link between the transport layer and the application layer, allowing the application layer to program without concern for the transport layer

    Server and Client Links

    The service side opens the listening service, the client links the server. Create a new two console program server/client.
    Server:

    static void Main(string[] args)    {        Console.WriteLine("Server is running ...");        IPAddress ip = IPAddress.Parse("127.0.0.1");        int port = 8500;        TcpListener listener = new TcpListener(ip, port);        listener.Start();        Console.WriteLine("\n\n Enter \"Q\" exit.");        ConsoleKey key;        do        {            key = Console.ReadKey(true).Key;        } while (key != ConsoleKey.Q);    }

    Client:

    static void Main(string[] args)    {        Console.WriteLine("Client Running");        TcpClient client = new TcpClient();        IPAddress serverIp = IPAddress.Parse("127.0.0.1");        int serverPort = 8500;        try        {            client.Connect(serverIp, serverPort);        }        catch (Exception ex)        {            Console.WriteLine(ex.Message);            return;        }        Console.WriteLine("Server Connected!{0} --> {1}", client.Client.LocalEndPoint, client.Client.RemoteEndPoint);        Console.WriteLine("\n\n Enter \"Q\" exit.");        ConsoleKey key;        do        {            key = Console.ReadKey(true).Key;        } while (key != ConsoleKey.Q);    }

    Command line run netstat -a view Port connection Status

    Server-side Get Client link

    AcceptTcpClient()As a blocking method, the thread waits for the client to link after the call
    Server:

        static void Main(string[] args)    {        Console.WriteLine("Server is running ...");        IPAddress ip = IPAddress.Parse("127.0.0.1");        int port = 8500;        TcpListener listener = new TcpListener(ip, port);        listener.Start();        Console.WriteLine("Start Listening ...");        //目的获取多个客户端信息        while(true)        {            try            {                //获取一个连接,中断方法(等待客户端的链接)                TcpClient remoteClient = listener.AcceptTcpClient();                //打印链接到的客户端信息                Console.WriteLine("Client Connected!{0}<-{1}", remoteClient.Client.LocalEndPoint, remoteClient.Client.RemoteEndPoint);            }            catch (Exception ex)            {                Console.WriteLine("Error:" + ex.Message);            }        }    }
    Client sends the message server to accept

    Server:

    static void Main (string[] args) {int buffersize = 8192;        Console.WriteLine ("Server is running ...");        IPAddress IP = ipaddress.parse ("127.0.0.1");        int port = 8500;        TcpListener listener = new TcpListener (IP, port); Listener.        Start ();        Console.WriteLine ("Start Listening ...");                Purpose to obtain multiple client information while (true) {try {//Get a connection, interrupt method (Waiting for client link) TcpClient remoteclient = listener.                AcceptTcpClient (); Print the client information that is linked to Console.WriteLine ("Client connected!{                0}<-{1} ", RemoteClient.Client.LocalEndPoint, RemoteClient.Client.RemoteEndPoint);                Obtain the stream and write to buffer networkstream streamtoclient = Remoteclient.getstream ();                byte[] buffer = new Byte[buffersize]; int bytesread = streamtoclient.read (buffer, 0, buffer.                Length);                Console.WriteLine ("Reading data,{0}bytes", bytesread); Get the requestedStrings String msg = Encoding.Unicode.GetString (buffer, 0, bytesread);            Console.WriteLine ("Received:{0}", msg); } catch (Exception ex) {Console.WriteLine ("Error:" + ex.)            Message); }        }    }

    Client:

      static void Main (string[] args) {Console.WriteLine ("Client Running");        TcpClient client = new TcpClient ();        IPAddress ServerIP = Ipaddress.parse ("127.0.0.1");        int serverport = 8500; try {client.        Connect (ServerIP, ServerPort); } catch (Exception ex) {Console.WriteLine (ex.            Message);        Return } Console.WriteLine ("Server connected!{ 0} to {1} ", client. Client.localendpoint, client.        Client.remoteendpoint);        string msg = "\" Welcome to Tracefact.net\ ""; NetworkStream streamtoserver = client.        GetStream ();        byte[] buffer = Encoding.Unicode.GetBytes (msg); Streamtoserver.write (buffer, 0, buffer.)        Length);        Console.WriteLine ("Sent:{0}", msg);        Console.WriteLine ("\ n \ nthe Enter \" Q\ "exit.");        Consolekey key; do {key = Console.readkey (true).        Key;    } while (Key! = CONSOLEKEY.Q); }

. NET Beauty reading note 10 (Network programming I)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.