⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Socket progrm make the IP and Port in listening mode.It work as server, and number of clients can co
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;									
using System.Net.Sockets;
using System.Collections;

delegate void DisplayIP(string ipMsg);

namespace ListeningPort
{
    public partial class Form1 : Form
    {
        private Socket m_mainSocket;
        private string connectedclients = "";
        private event DisplayIP ip_Message;
        public Form1()
        {
           
            InitializeComponent();
        }


        public void NewConnection(Socket sockClient)
        {
            //InitlizeSettings();
            SocketClient client = new SocketClient(sockClient);
            connectedclients += client.Sock.RemoteEndPoint.ToString() + Environment.NewLine;
            client.SetupRecieveCallback(this);

        }

        public void OnClientConnect(IAsyncResult ar)
        {
            Socket listener = (Socket)ar.AsyncState;
            NewConnection(listener.EndAccept(ar));
            listener.BeginAccept(new AsyncCallback(OnClientConnect), listener);
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            ip_Message = new DisplayIP(OnDisplayIP);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            IPAddress[] aryLocalAddr = null;
            String strHostName = "";
            
            strHostName = Dns.GetHostName();
            IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
            aryLocalAddr = ipEntry.AddressList;
           

            int port = System.Convert.ToInt32(399);
            m_mainSocket = new Socket(AddressFamily.InterNetwork,
                                      SocketType.Stream,
                                      ProtocolType.Tcp);
            IPEndPoint ipLocal = new IPEndPoint(aryLocalAddr[0], port);
            m_mainSocket.Bind(ipLocal);
            m_mainSocket.Blocking = true;
            m_mainSocket.Listen(4);
            m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), m_mainSocket);
            lblText.Text = "-- Server Started --";
            button1.Enabled = false;  
        }

        public void OnRecievedData(IAsyncResult ar)
        {
            string msgRecv = "";
            Invoke(ip_Message, new string[] { msgRecv });
           
        }

        public void OnDisplayIP(string sMessage)
        {

            rtBox.Text = connectedclients;        

        }

        internal class SocketClient
        {
            private Socket m_sock;						
            private byte[] m_byBuff = new byte[50];
            public SocketClient(Socket sock)
            {
                m_sock = sock;
            }

            public Socket Sock
            {
                get { return m_sock; }
            }

            public void SetupRecieveCallback(Form1 app)
            {
                try
                {
                    AsyncCallback recieveData = new AsyncCallback(app.OnRecievedData);
                    m_sock.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, recieveData, this);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Recieve callback setup failed! {0}", ex.Message);
                }
            }

           
            public byte[] GetRecievedData(IAsyncResult ar)
            {
                int nBytesRec = 0;
                try
                {
                    nBytesRec = m_sock.EndReceive(ar);
                }
                catch { }
                byte[] byReturn = new byte[nBytesRec];
                Array.Copy(m_byBuff, byReturn, nBytesRec);

                return byReturn;
            }
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -