日期:2017-05-22  浏览次数:3536 次

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
 
namespace TestSockets
{
    class Program
    {
        static void Main(string[] args)
        {
            IPAddress myIp = IPAddress.Parse("127.0.0.1");
            IPEndPoint myLocation = new IPEndPoint(myIp, 81);
            Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            mySocket.Bind(myLocation);
            mySocket.Listen(0);
            Socket s = mySocket.Accept();
            Console.WriteLine("那个鸟人进来了");
           // Console.ReadLine();
            byte[] myByte = new byte[1024];
            s.Receive(myByte);
            string str = UnicodeEncoding.UTF8.GetString(myByte);
            Console.WriteLine(str);
            Console.WriteLine(str);
            mySocket.Close();
            s.Close();
           // (new Thread(new ThreadStart(MyConnect))).Start();
        }
    }
}
 
访问:http://127.0.0.1:81/test?id=80
 
获取结果:
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8
                                                                                                                                                                                                                   
GET /test?id=80 HTTP/1.1
Host: 127.0.0.1:81
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8