// 先匯入
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;



// 把下面程式碼丟入 Main() 方法裡

try
{
    Socket s = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream,
    ProtocolType.Tcp);

    IPAddress hostIP = (Dns.Resolve(IPAddress.Any.ToString())).AddressList[0];
    IPEndPoint ep = new IPEndPoint(hostIP, 39217);
    s.Bind(ep);
    s.Listen(10);

    Socket h = s.Accept();

    NetworkStream stream = new NetworkStream(h);
    StreamReader sr = new StreamReader(stream);

    Console.WriteLine(sr.ReadLine()); // 讀取從客戶端傳來的資料

}catch (Exception ex){
     Console.WriteLine("Error : " + ex.StackTrace.ToString());
}

    全站熱搜

    黃彥霖 發表在 痞客邦 留言(0) 人氣()