1. 程式人生 > 實用技巧 >C# Socket服務端和客戶端通話

C# Socket服務端和客戶端通話

服務端

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

namespace tSocket
{
    class Program
    {
        byte[] bytes = new byte[1024];
        Socket cSocket;
        static void Main(string
[] args) { Program p = new Program(); //開啟連結 p.open(); //向服務端傳送訊息 Console.WriteLine("請輸入你要對服務端傳送的訊息:"); string mes = Console.ReadLine(); string con = p.messge(mes); Console.WriteLine("接受到服務端的訊息:" + con); }
byte[] data = new byte[1024]; string messge(string mes) { //將傳送的訊息轉成位元組陣列 bytes = Encoding.UTF8.GetBytes(mes); //傳送 cSocket.Send(bytes); while (true) { //接受服務端傳送的訊息,放入位元組陣列 int len = cSocket.Receive(data);
//將位元組陣列轉成可讀明文 string con = Encoding.UTF8.GetString(data, 0, len); ////返回 return con; } } /// <summary> /// 開啟連結 /// </summary> void open() { //建立Socket物件 指定連線方式 cSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //建立IP,埠 IPAddress ip = IPAddress.Parse("10.116.253.10"); int port = 7526; //封裝IP和埠 IPEndPoint Ipoint = new IPEndPoint(ip, port); //開啟連結 cSocket.Connect(Ipoint); } } }

客戶端

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

namespace ServerSocket
{
    class Program
    {
        
        static void Main(string[] args)
        {
            //建立Socket物件,指定他的連結方式
            Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //建立IP
            string ip = "10.116.253.10";
            //建立埠
            int prot = 7526;//1~9999
            IPAddress IPAdd = IPAddress.Parse(ip);
            //封裝IP和埠
            IPEndPoint point = new IPEndPoint(IPAdd, prot);
            //繫結IP和埠
            serverSocket.Bind(point);
            //開始監聽
            serverSocket.Listen(100);
            Console.WriteLine("開始監聽!");

            int i = 0;
            while (true)
            {
                i++;
                //接受客戶連結
                
               Socket cSocket = serverSocket.Accept();
               Console.WriteLine("接受第"+i+"個客戶的連線!");
               Client c = new Client(cSocket);
            }

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ServerSocket
{
    class Client
    {
        Socket sSocket;
        byte[] data = new byte[1024];
        Thread t;
        public Client(Socket cSocket)
        {
            //接受客戶的連線
            sSocket = cSocket;
            //建立執行緒
            t = new Thread(Mess);
            //開始執行緒
            t.Start();
        }

        void Mess()
        {
            try
            {
                while (true)
                {
                    //將使用者傳送的資料以一個位元組陣列裝起
                    int length = sSocket.Receive(data);
                    Console.WriteLine("接受客戶端發的訊息!");
                    string mess = Encoding.UTF8.GetString(data, 0, length);

                    if (mess == "con")
                    {
                        string con = "DataSource =.";
                        byte[] bytes = Encoding.UTF8.GetBytes(con);
                        sSocket.Send(bytes);
                    }
                    Console.WriteLine("接到使用者的訊息:" + mess);
                }
            }
            catch (Exception)
            {
                sSocket.Close();
            }



        }
    }
}