日期:2014-05-17 浏览次数:20977 次
namespace Server_2
{
public partial class Form1 : Form
{
Thread aThread = null;
private delegate void FlushClient(string s); //代理
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text += ("正在开启server服务" + "\n" + ".......请稍候..." + "\n");
aThread = new Thread(new ThreadStart(StartSocketServer));
aThread.IsBackground = true;
aThread.Start();
textBox1.Text += ("server is start!" + "\n");
}
public void StartSocketServer()
{
IPAddress ip = IPAddress.Parse("192.168.168.163");
//IP地址跟端口的组合
IPEndPoint iep = new IPEndPoint(ip, 8001);
//创建Socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//绑定Socket
socket.Bind(iep);
//服务器已经做好接收任何连接的准备
socket.Listen(10);
while (true)
{
//执行accept方法
Socket Client = socket.Accept();
byte[] message = new byte[1024];
NetworkStream networkStream = new NetworkStream(Client);
int len = networkStr