C# 服务器、客户端学习(三)

如果使用一个do/while循环,并将listener.AcceptTcpClient()方法放在循环之外,将TcpClient.GetStream().Read()方法放在循环以内,那么服务端可以处理一个客户端的多条请求。

1、服务器

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

using System.Net;
using System.Net.Sockets;
using System.IO;

namespace Service
{
    class Program
    {
        static void Main(string[] args)
        {
        
            const int BufferSize = 8192;
            //ConsoleKey key;
            Console.WriteLine("service is running.....");
            IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });
            TcpListener listener = new TcpListener(ip, 8500);

            listener.Start();
            Console.WriteLine("start listening.......");
            // 获取一个连接,中断方法
            TcpClient remoteClient = listener.AcceptTcpClient();   // 打印连接到的客户端信息
            Console.WriteLine("Client Connected!{0} <--{1}",
            remoteClient.Client.LocalEndPoint, remoteClient.Client.RemoteEndPoint);
            // 获得流,并写入buffer中
            NetworkStream streamToClient = remoteClient.GetStream();
            do
            {
                byte[] buffer = new byte[BufferSize];
                int bytesRead = streamToClient.Read(buffer, 0, BufferSize);
                Console.WriteLine("Reading data, {0} bytes ...", bytesRead);
                // 获得请求的字符串
                string msg = Encoding.Unicode.GetString(buffer, 0, bytesRead);
                Console.WriteLine("Received: {0}", msg);
            } while (true);
        }
    }
}

2、客户端

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

using System.Net;
using System.Net.Sockets;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Client Running ...");
            TcpClient client;
            try
            {
                client = new TcpClient();
                client.Connect("localhost", 8500);     // 与服务器连接
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message); return;
            }
            // 打印连接到的服务端信息
            Console.WriteLine("Server Connected!{0} --> {1}",
                client.Client.LocalEndPoint, client.Client.RemoteEndPoint);
            NetworkStream streamToServer = client.GetStream();
            ConsoleKey key;
            Console.WriteLine("Menu: S -Send, X -Exit");
            do
            {
                key = Console.ReadKey(true).Key;
                if (key == ConsoleKey.S)
                {       // 获取输入的字符串
                    Console.Write("Input the message: ");
                    string msg = Console.ReadLine();
                    byte[] buffer = Encoding.Unicode.GetBytes(msg);    // 获得缓存

                    streamToServer.Write(buffer, 0, buffer.Length);    // 发往服务器
                    Console.WriteLine("Sent: {0}", msg);
                }
            } while (key != ConsoleKey.X);
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值