.Net网络编程——使用TcpClient与TcpListener建立客户端与服务端连接

来源:互联网 发布:虚拟机10安装mac 编辑:程序博客网 时间:2024/05/19 04:02



           

  一,.NET中如何实现建立连接



                        在网络中,我们可以通过IP地址唯一定位一台主机,而在主机中,我们要确定收到的数据包发给谁,可以通过端口号,端口号的作用简单说就是不至于使你要发给QQ好友的消息数据包被错误发到了你的OC程序上。


              通常,我们把发起连接的那一端称为客户端,这是主动的一方;而静默等待连接到来的那一端作为服务端。这个概念是比较相对的。


              在.Net中,我们可以使用TcpClient来建立连接,使用TcpListener来监听连接,从而在客户端和服务端建立连接。



二,服务端建立监听


                   

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;namespace 服务端对端口进行侦听{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("服务端正在运行呀。。。");            //IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });           // IPAddress ip = IPAddress.Parse("127.0.0.1");            IPAddress ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0]; //同上            TcpListener listener = new TcpListener(ip, 8500);  //选择侦听端口            listener.Start();//开始侦听            Console.WriteLine("开始侦听啦。。。");            Console.WriteLine("\n\n输入\"Q\"键退出。。");            ConsoleKey key;            do            {                key = Console.ReadKey(true).Key;            } while (key!=ConsoleKey.Q);        }    }}


   启动程序后,使用netstat -a查看端口情况:





     


   发现端口正在listening....




三,客户端与服务端建立连接



         在服务端侦听端口打开的情况下,就可以与服务端端口进行连接啦:



using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;namespace 服务端与客户端的连接{    class Program    {        static void Main(string[] args)        {            #region 客户端与服务端连接                //Console.WriteLine("客户端启动啦啦啦。。");                //TcpClient client = new TcpClient();                //try                //{                //    //与服务器进行连接                //    client.Connect(IPAddress.Parse("127.0.0.1"), 8500);                //}                //catch (Exception ex)                //{                //    Console.WriteLine(ex.Message);                //    return;                //}                ////打印连接到服务端的信息                //Console.WriteLine("服务端连接成功啦。。本地IP端口为:{0}------>服务IP端口为:{1}", client.Client.LocalEndPoint, client.Client.RemoteEndPoint);//client.Client获得到套接字            #endregion            #region 多个客户端与服务端的连接                Console.WriteLine("客户端启动啦啦啦啦。。。。");                TcpClient client;                for (int i = 0; i < 2; i++)                {                    try                    {                        client = new TcpClient();//每创建一个新的TcpClient便相当于创建了一个新的套接字Socket与服务端通信,.Net会自动为这个套接字分配 一个端口号。                        //与服务器建立连接                        client.Connect(IPAddress.Parse("127.0.0.1"), 8500);                    }                    catch (Exception ex)                    {                        Console.WriteLine(ex.Message);                        Console.WriteLine(ex.StackTrace);                        return;                    }                    Console.WriteLine("服务端连接成功啦。。本地IP端口为:{0}------>服务IP端口为:{1}", client.Client.LocalEndPoint, client.Client.RemoteEndPoint);//client.Client获得到套接字                }            #endregion        }    }}



   连接时,主要是使用TcpClient对象,传入要连接的服务端的IP和端口号,就像发邮件那样,只要选择好存在的发件人,就能发送那样。




       


           

                    

0 0