初学C#的SOCKET编程,照着网上的代码写下来,程序总是有问题,经过自己长时间的调试,完成程序,以下是原码,有需要可以参考一下,还不完善,欢迎大家批评指正。(这里的代码没更新,附件重新上传更新,在另一个线程中使用委托来修改控件的属性,发送广播功能还有问题,稍修更新修正。)
环境:VS2008 .NET3.5
     网上给出的程序都是控制台程序,我这个是WINFORM程序。有详细的说明,以服务端程序。
    需要增加的命名空间:
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORMusing System.Threading;
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORMusing System.Net;
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORMusing System.Net.Sockets;
    以下是具体实现。
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORMnamespace TCPServer
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM{
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM        public partial class Form1 : Form
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM        {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                public Form1()
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        InitializeComponent();
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                public bool btnstatu = true;    //开始停止服务按钮状态
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                public Thread myThread;             //声明一个线程实例
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                public Socket newsock;                //声明一个Socket实例
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                public Socket Client;                    
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                public IPEndPoint localEP;        
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                public int localPort;
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                public bool m_Listening;
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                //用来设置服务端监听的端口号
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                public int setPort                        
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        get { return localPort; }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        set { localPort = value; }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                //用来往richtextbox框中显示消息
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                public void showClientMsg(string msg)
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        showClientMsg(msg+"\r\n");
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                //监听函数
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                public void Listen()
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                {     //设置端口
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        setPort=int.Parse(serverport.Text.Trim());
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        //初始化SOCKET实例
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        //初始化终结点实例
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        localEP=new IPEndPoint(IPAddress.Any,setPort);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        try
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                //绑定
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                newsock.Bind(localEP);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                //监听
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                newsock.Listen(10);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                //用于设置按钮状态
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                m_Listening = true;
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                //开始接受连接,异步。
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                newsock.BeginAccept(new AsyncCallback(OnConnectRequest), newsock);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                         }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        catch (Exception ex)
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                showClientMsg(ex.Message);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                //当有客户端连接时的处理
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                public void OnConnectRequest(IAsyncResult ar)
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                     //初始化一个SOCKET,用于其它客户端的连接
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        Socket server1 = (Socket)ar.AsyncState;
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        Client = server1.EndAccept(ar);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        //将要发送给连接上来的客户端的提示字符串
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        string strDateLine = "Welcome here";
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes(strDateLine);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        //将提示信息发送给客户端
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        Client.Send(byteDateLine, byteDateLine.Length, 0);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        //等待新的客户端连接
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        server1.BeginAccept(new AsyncCallback(OnConnectRequest), server1);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        while (true)
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                int recv = Client.Receive(byteDateLine);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                string stringdata = Encoding.ASCII.GetString(byteDateLine, 0, recv);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                DateTimeOffset now = DateTimeOffset.Now;
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                //获取客户端的IP和端口
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                string ip = Client.RemoteEndPoint.ToString();
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                if (stringdata == "STOP")
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                        //当客户端终止连接时
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                        showinfo.AppendText(ip+"已从服务器断开");
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                        break;    
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                //显示客户端发送过来的信息
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                showinfo.AppendText(ip + "    " + now.ToString("G") + "     " + stringdata + "\r\n");                            
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                                
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM            //开始停止服务按钮
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                private void startService_Click(object sender, EventArgs e)
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        //新建一个委托线程
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        ThreadStart myThreadDelegate = new ThreadStart(Listen);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        //实例化新线程
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        myThread = new Thread(myThreadDelegate);
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                            
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        if (btnstatu)
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                myThread.Start();
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                statuBar.Text = "服务已启动,等待客户端连接";
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                btnstatu = false;
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                startService.Text = "停止服务";
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        else
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                //停止服务(功能还有问题,无法停止)
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                m_Listening = false;
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                newsock.Close();
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                myThread.Abort();
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                showClientMsg("服务器停止服务");
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                btnstatu = true;
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                startService.Text = "开始服务";
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                statuBar.Text = "服务已停止";
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                m_Listening = false;
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                            
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                //窗口关闭时中止线程。
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                private void Form1_FormClosing(object sender, FormClosingEventArgs e)
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        if (myThread != null)
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        {
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                                myThread.Abort();
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                        }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM                }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM        }
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM
c# WINFORM SOCKET编程-简单聊天程序(服务端)_WINFORM}