﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32 port = 6000;
            String host = "127.0.0.1";

            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);

            Socket sSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sSocket.Bind(ipe);
            sSocket.Listen(0);
            Console.WriteLine("监听已经打开，请等待");

            //接收消息
            Socket serverSocket = sSocket.Accept();
            Console.WriteLine("连接已建立");
            String receiveStr = "";
            Byte[] recByte = new Byte[4096];
            Int32 count = serverSocket.Receive(recByte, recByte.Length, 0);
            receiveStr += Encoding.ASCII.GetString(recByte, 0, count);

            //发送消息
            Console.WriteLine("服务器端获得信息：{0}", receiveStr);
            String sendStr = "send to client : hello";
            Byte[] sendBytes = Encoding.ASCII.GetBytes(sendStr);
            serverSocket.Send(sendBytes, sendBytes.Length, 0);
            serverSocket.Close();
            sSocket.Close();
        }
    }
}
