C#TcpClient TcpListener客户端服务器程序

服务器程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace SocketServer
{
    class Program
    {
        public void ProcessConnection(Object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream networkStream = tcpClient.GetStream();
            BinaryReader br = new BinaryReader(networkStream);
            BinaryWriter bw = new BinaryWriter(networkStream);
            while (true)
            {
                try
                {
                    string sReader = br.ReadString(); //接收消息
                    Console.WriteLine(sReader); //打印消息
                    string sWriter = "接收到消息";
                    bw.Write(sWriter);   //向对方发送消息
                }
                catch
                {
                    break;
                }
            }
        }
        static void Main()
        {
            Program p = new Program();
            IPAddress localAddress;      //IP地址
            int port = 51888;     //端口
            TcpListener tcpListener;  //监听套接字
            TcpClient tcpClient;     
            IPAddress[] listenIp = Dns.GetHostAddresses("127.0.0.1");
            localAddress = listenIp[0];
            tcpListener = new TcpListener(localAddress, port);
            tcpListener.Start(); //开始监听
            while (true)
            {
                try
                {
                    tcpClient = tcpListener.AcceptTcpClient();//每接受一个客户端则生成一个TcpClient
                    Thread thread = new Thread(p.ProcessConnection); //用一个线程单独处理这个连接
                    thread.Start(tcpClient);
                }
                catch
                {
                    break;
                }
            }
        }
    }
}
客户端程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace SocketClient
{
    class Program
    {
        static void Main()
        {
            TcpClient tcpClient;   
            tcpClient = new TcpClient();  //创建一个TcpClient对象,自动分配主机IP地址和端口号
            tcpClient.Connect("127.0.0.1", 51888);   //连接服务器,其IP和端口号为127.0.0.1和51888
            if (tcpClient != null)
            {
                Console.WriteLine("连接服务器成功");
                NetworkStream networkStream= tcpClient.GetStream();
                BinaryReader br = new BinaryReader(networkStream);
                BinaryWriter bw = new BinaryWriter(networkStream);
                bw.Write("你好服务器,我是客户端");  //向服务器发送字符串
                while (true)        
                {
                    try
                    {
                        string brString = br.ReadString();     //接收服务器发送的数据
                        if (brString != null)
                        {
                            Console.WriteLine("接收到服务器发送的数据{0}", brString);
                        }
                    }
                    catch
                    {
                        break;        //接收过程中如果出现异常,将推出循环
                    }
                }
            }
            Console.WriteLine("连接服务器失败");
        }
    }
}


 
  

你可能感兴趣的:(C#)