using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;using System.Net;
using System.Net.Sockets;
using System.Threading;namespace MyServer
{
    public partial class ServerForm : Form
    {
        private IPEndPoint ipep;
        private Socket socket;
        private Thread thread;        private delegate void MyThread(Socket socket);
        private delegate void SetListBoxItem(string text);        private void setText(string text)
        {
            lbMessage.Items.Add(text);
        }        public ServerForm()
        {
            InitializeComponent();            btnStar.Enabled = true;
            btnEnd.Enabled = false;
        }        private void btnStar_Click(object sender, EventArgs e)
        {
            try
            {
                this.ipep = new IPEndPoint(IPAddress.Any, 6001);
                this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);                this.socket.Bind(ipep);
                this.socket.Listen(5);                this.thread = new Thread(new ThreadStart(starListen));
                this.thread.IsBackground = true;
                this.thread.Start();                lbMessage.Items.Add("服务器开启成功");                btnStar.Enabled = false;
                btnEnd.Enabled = true;
            }
            catch (Exception)
            {
                lbMessage.Items.Add("服务器开启失败");
            }
        }        private void btnEnd_Click(object sender, EventArgs e)
        {
            try
            {
                this.thread.Abort();                this.socket.Shutdown(SocketShutdown.Both);
                this.socket.Close();                this.ipep = null;
                this.socket = null;                lbMessage.Items.Add("服务器关闭成功");                btnStar.Enabled = true;
                btnEnd.Enabled = false;
            }
            catch (Exception)
            {
                lbMessage.Items.Add("服务器关闭失败");
            }
        }        private void starListen()
        {
            do
            {
                try
                {
                    MyThread myThread = new MyThread(getLink);
                    myThread.BeginInvoke(this.socket.Accept(), null, null);
                }
                catch (Exception)
                {
                    continue;
                }
            } while (true);
        }        private void getLink(Socket socket)
        {
            Thread thread = Thread.CurrentThread;
            thread.IsBackground = true;            int length;
            byte[] data;
            bool temp = true;
            SetListBoxItem setListBoxItem = new SetListBoxItem(setText);
            IPEndPoint ipep = (IPEndPoint)socket.RemoteEndPoint;            string ipAndPort = ipep.Address + ":" + ipep.Port;
            if (lbMessage.InvokeRequired)
                lbMessage.Invoke(setListBoxItem, new object[] { ipAndPort + " 已连接" });
            else
                lbMessage.Items.Add(ipAndPort + " 已连接");            do
            {
                try
                {
                    data = new byte[1024];
                    length = socket.Available;
                    socket.Receive(data, 0, length, SocketFlags.None);                    if (length == 0)
                        continue;                    if (lbMessage.InvokeRequired)
                        lbMessage.Invoke(setListBoxItem, new object[] { Encoding.Default.GetString(data) });
                    else
                        lbMessage.Items.Add(Encoding.Default.GetString(data));                    socket.Send(Encoding.Default.GetBytes("发送成功"), 8, SocketFlags.None);
                }
                catch (Exception)
                {
                    temp = false;
                }
            } while (temp);            if (lbMessage.InvokeRequired)
                lbMessage.Invoke(setListBoxItem, new object[] { ipAndPort + " 已断开" });
            else
                lbMessage.Items.Add(ipAndPort + " 已断开");            socket.Close();
            thread.Abort();
        }
    }
}不管有或者无客户端连接情况下
this.socket.Shutdown(SocketShutdown.Both);
都会报错 错误是 由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。
求解答

解决方案 »

  1.   

    看了一下你的代码,this.socket.Listen(5);最多只能接受5个客户端的连接
    但是,这句并没有起到作用,因为你的设计模式中,只能接受一个客户端的连接
    而且,你在发送和接受数据后,就把连接关闭了socket.Close();
    thread.Abort();把这两句代码注释掉试试先。
    还有在关闭连接按钮事件里,不要一下就关闭,要先判断一下socket是否为空,如:         if(socket != null)  
             { 
                try
                {
                    this.thread.Abort();                this.socket.Shutdown(SocketShutdown.Both);
                    this.socket.Close();                this.ipep = null;
                    this.socket = null;                lbMessage.Items.Add("服务器关闭成功");                btnStar.Enabled = true;
                    btnEnd.Enabled = false;
                }
                catch (Exception)
                {
                    lbMessage.Items.Add("服务器关闭失败");
                }
             }
    再者,在接受客户端连接的时候,不要直接就去getLink(Socket socket)
    先判断一下Socket socketServer = this.socket.Accept();
    if(socketServer.Connected)
    {
         //再调用getLink(Socket socket)
    }希望对你有帮助