1. 程式人生 > >C#TcpClient TcpListener客戶端伺服器程式

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;
                }
            }
        }
    }
}
客戶端程式:
<pre name="code" class="csharp">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("連線伺服器失敗");
        }
    }
}