TCP网络程序实例——服务器端与客户端交互

实例02

客户端/服务器的交互

实例位置:光盘\Code\SL\14\02

视频位置:光盘\Video\14\

◆ 服务器端

创建服务器端项目Server,在Main方法中创建TCP连接对象;然后监听客户端接入,并读取接入的客户端IP地址和传入的消息;最后向接入的客户端发送一条信息。代码如下:

01    namespace Server

02    {

03        class Program

04        {

05          static void Main()

06          {

07              int port = 888;                                               //端口

08              TcpClient tcpClient;                                          //创建TCP连接对象

09              IPAddress[] serverIP = Dns.GetHostAddresses("127.0.0.1"); //定义IP地址

10              IPAddress localAddress = serverIP[0];                       //IP地址

11              TcpListener tcpListener = new TcpListener(localAddress, port);//监听套接字

12              tcpListener.Start();                                         //开始监听

13                Console.WriteLine("服务器启动成功,等待用户接入…");          //输出消息

14                while (true)

15                {

16                    try

17                    {

18                        //每接收一个客户端则生成一个TcpClient

19                      tcpClient = tcpListener.AcceptTcpClient();

20                      NetworkStream networkStream = tcpClient.GetStream();//获取网络数据流

21                      //定义流数据读取对象

22                      BinaryReader reader = new BinaryReader(networkStream);

23                      //定义流数据写入对象

24                      BinaryWriter writer = new BinaryWriter(networkStream);

25                      while (true)

26                      {

27                          try

28                          {

29                              string strReader = reader.ReadString();//接收消息

30                              //截取客户端消息

31                              string[] strReaders = strReader.Split(new char[] { ' ' });

32                                //输出接收的客户端IP地址

33                                Console.WriteLine("有客户端接入,客户IP:" + strReaders[0]);

34                                //输出接收的消息

35                              Console.WriteLine("来自客户端的消息:" + strReaders[1]);

36                              string strWriter = "我是服务器,欢迎光临";//定义服务端要写入的消息

37                              writer.Write(strWriter);                  //向对方发送消息

38                          }

39                          catch

40                          {

41                              break;

42                          }

43                      }

44                  }

45                  catch

46                  {

47                      break;

48                  }

49                }

50            }

51        }

52    }

◆ 客户端

创建客户端项目Client,在Main方法中创建TCP连接对象,以指定的地址和端口连接服务器;然后向服务器端发送数据和接收服务器端传输的数据。代码如下:

01    namespace Client

02    {

03        class Program

04        {

05

06            static void Main(string[] args)

07          {

08              //创建一个TcpClient对象,自动分配主机IP地址和端口号

09              TcpClient tcpClient = new TcpClient();

10              //连接服务器,其IP和端口号为127.0.0.1和888

11              tcpClient.Connect("127.0.0.1", 888);

12              if (tcpClient != null)                                         //判断是否连接成功

13              {

14                  Console.WriteLine("连接服务器成功");

15                  NetworkStream networkStream = tcpClient.GetStream();   //获取数据流

16                  BinaryReader reader = new BinaryReader(networkStream); //定义流数据读取对象

17                  BinaryWriter writer = new BinaryWriter(networkStream); //定义流数据写入对象

18                  string localip="127.0.0.1";       //存储本机IP,默认值为127.0.0.1

19                  IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName()); //获取所有IP地址

20                  foreach (IPAddress ip in ips)

21                  {

22                      if (!ip.IsIPv6SiteLocal)                        //如果不是IPV6地址

23                          localip = ip.ToString();                    //获取本机IP地址

24                  }

25                  writer.Write(localip + " 你好服务器,我是客户端");  //向服务器发送消息

26                  while (true)

27                  {

28                      try

29                      {

30                          string strReader = reader.ReadString();   //接收服务器发送的数据

31                          if (strReader != null)

32                          {

33                              //输出接收的服务器消息

34                              Console.WriteLine("来自服务器的消息:"+strReader);

35                          }

36                      }

37                      catch

38                      {

39                          break;                 //接收过程中如果出现异常,退出循环

40                      }

41                  }

42              }

43              Console.WriteLine("连接服务器失败");

44            }

45        }

46    }

首先运行服务器端,然后运行客户端,运行客户端后后的服务器端效果如图14.12所示,客户端运行效果如图14.13所示。

TCP网络程序实例——服务器端与客户端交互  TCP网络程序实例——服务器端与客户端交互

图14.12  客户端运行后的服务器端效果                       图14.13  客户端运行效果

本文摘自明日科技出版的《零基础学C#》,转载请注明版权归属!!!

上一篇:jQuery 事件的命名空间简单了解


下一篇:MySQL单机优化---分表、分区、分库