예제 #1
0
        /// <summary>
        /// 开始监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Start_Click(object sender, EventArgs e)
        {
            string host = txt_IP.Text; //服务端IP地址
            int    port = int.Parse(txt_Port.Text);

            socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //设置端口可复用
            socketWatch.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            //连接服务端
            socketWatch.Connect(host, port);

            this.txt_Log.AppendText("连接成功" + " \r \n");

            //实例化回调
            setCallBack     = new SetTextValueCallBack(SetTextValue);
            receiveCallBack = new ReceiveMsgCallBack(ReceiveMsg);
            ipCallBack      = new IPCallBack(IpChangeValue);
            setCmbCallBack  = new SetCmbCallBack(AddCmbItem);
            sendCallBack    = new SendFileCallBack(SendFile);
            sendThread      = new Thread(Send);
            sendThread.Start();
            threadReceive = new Thread(new ParameterizedThreadStart(Receive));
            threadReceive.IsBackground = true;
            threadReceive.Start(socketWatch);
        }
예제 #2
0
        /// <summary>
        /// 开始监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Start_Click(object sender, EventArgs e)
        {
            //当点击开始监听的时候 在服务器端创建一个负责监听IP地址和端口号的Socket
            socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //获取ip地址
            IPAddress ip = IPAddress.Parse(this.txt_IP.Text.Trim());
            //创建端口号
            IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(this.txt_Port.Text.Trim()));

            //绑定IP地址和端口号
            socketWatch.Bind(point);
            this.txt_Log.AppendText("监听成功" + " \r \n");
            //开始监听:设置最大可以同时连接多少个请求
            socketWatch.Listen(10);

            //实例化回调
            //setCallBack = new SetTextValueCallBack(SetTextValue); SetTextValue;
            //receiveCallBack = new ReceiveMsgCallBack(ReceiveMsg);
            //setCmbCallBack = new SetCmbCallBack(AddCmbItem);
            //sendCallBack = new SendFileCallBack(SendFile);
            setCallBack     = SetTextValue;
            receiveCallBack = ReceiveMsg;
            setCmbCallBack  = AddCmbItem;
            sendCallBack    = SendFile;

            //创建线程
            AcceptSocketThread = new Thread(new ParameterizedThreadStart(StartListen));
            AcceptSocketThread.IsBackground = true;
            AcceptSocketThread.Start(socketWatch);
        }
예제 #3
0
        /// <summary>
        /// 开始监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Start_Click(object sender, EventArgs e)
        {
            //当点击开始监听的时候 在服务器端创建一个负责监听IP地址和端口号的Socket
            int        port = int.Parse(this.txt_Port.Text.Trim());
            IPEndPoint ipe  = new IPEndPoint(IPAddress.Any, port);//new IPEndPoint(IPAddress.Any, port);//IPAddress.Parse("180.175.63.122")

            socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socketWatch.Bind(ipe);
            socketWatch.Listen(20);
            this.txt_Log.AppendText("监听成功" + " \r \n");

            //实例化回调
            setCallBack     = new SetTextValueCallBack(SetTextValue);
            receiveCallBack = new ReceiveMsgCallBack(ReceiveMsg);
            ipCallBack      = new IPCallBack(IpChangeValue);
            setCmbCallBack  = new SetCmbCallBack(AddCmbItem);
            sendCallBack    = new SendFileCallBack(SendFile);
            Thread send = new Thread(Send);

            send.Start();
            //创建线程
            AcceptSocketThread = new Thread(new ParameterizedThreadStart(StartListen));
            AcceptSocketThread.IsBackground = true;
            AcceptSocketThread.Start(socketWatch);
        }
예제 #4
0
파일: Form1.cs 프로젝트: MaYunshan/Code
        private void btnListen_Click(object sender, EventArgs e)
        {
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);

            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sock.Bind(ipep);               //设置监听地址
            sock.Listen(10);               //监听

            //实例化回调
            setCallBack     = new SetTextValueCallBack(SetTextValue);
            receiveCallBack = new ReceiveMsgCallBack(ReceiveMsg);
            setCmbCallBack  = new SetCmbCallBack(AddCmbItem);
            sendCallBack    = new SendFileCallBack(SendFile);

            //创建线程
            AcceptSocketThread = new Thread(new ParameterizedThreadStart(StartListen));
            AcceptSocketThread.IsBackground = true;
            AcceptSocketThread.Start(sock);
        }
예제 #5
0
        private void BtnStartServer_Click(object sender, RoutedEventArgs e)
        {
            #region  绑定回调函数
            setCallBack     = new SetTextValueCallBack(SetTextValue);
            receiveCallBack = new ReceiveMsgCallBack(ReceiveMsg);
            setCmbCallBack  = new SetCmbCallBack(AddCmbItem);
            sendCallBack    = new SendFileCallBack(SendFile);
            #endregion
            //监听
            Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socketWatch.Bind(point);
            socketWatch.Listen(100);
            SetTextValue(string.Format("服务器{0}:开启监听", socketWatch.LocalEndPoint.ToString()));

            //创建监听线程
            AcceptSocketThread = new Thread(new ParameterizedThreadStart(StartListen));
            AcceptSocketThread.IsBackground = true;
            AcceptSocketThread.Start(socketWatch);

            this.cmbSocket.ItemsSource = dicSocket.Keys;
        }
예제 #6
0
        /// <summary>
        /// 点击开始监听按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Start_Click(object sender, EventArgs e)
        {
            //当点击开始 监听时 在服务器端创建一个负责监听IP地址和端口号的Socket
            SocketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //获取IP地址
            IPAddress ip = IPAddress.Parse(txt_IP.Text.Trim());
            //端口
            int port = Convert.ToInt32(txt_Port.Text.Trim());

            //终结点:
            //在Internet中,TCP/IP 使用套接字(一个网络地址和一个服务端口号)来唯一标识设备。网络地址(IP)标识网络上的特定设备;端口号(Port)标识要连接到的该设备上的特定服务。
            //网络地址和服务端口的组合称为终结点,在 .NET 框架中正是由 EndPoint 类表示这个终结点,它提供表示网络资源或服务的抽象,用以标志网络地址等信息。
            //.Net同时也为每个受支持的地址族定义了 EndPoint 的子代;对于 IP 地址族,该类为 IPEndPoint。IPEndPoint 类包含应用程序连接到主机上的服务所需的主机和端口信息。

            /**
             * 作者:UnityAsk
             * 链接:https://www.jianshu.com/p/ffefd038ba11
             * 来源:简书
             * 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
             * **/
            //终结点
            IPEndPoint point = new IPEndPoint(ip, port);

            //绑定IP地址和端口号
            SocketWatch.Bind(point);
            txt_Log.AppendText("监听成功" + "\r\n");
            //开始监听:设置最大可以同时连接多少个请求
            SocketWatch.Listen(10);

            //实例化回调
            SetCallBack     = new SetTextValueCallBack(SetTextValue);
            ReceiveCallBack = new ReceiveMsgCallBack(ReceiveMsg);
            SetCmbCallBack  = new SetCmbItemCallBack(AddCmbItem);
            SendCallBack    = new SendFileCallBack(SendFile);

            //创建线程
            AcceptSocketThread = new Thread(new ParameterizedThreadStart(StartListen));
            AcceptSocketThread.IsBackground = true;
            AcceptSocketThread.Start(SocketWatch);
        }
예제 #7
0
        ///开始监听
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            //创建一个负责监听IP地址和端口号的Socket
            socketListen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //获取ip地址
            IPAddress ip = IPAddress.Parse(textBox1.Text.Trim());
            //创建端口号
            IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(textBox2.Text.Trim()));

            //绑定IP地址和端口号
            try
            {
                socketListen.Bind(point);
            }
            catch
            {
                textBox4.AppendText(@"监听失败" + Environment.NewLine);
                button1.Enabled = true;
                return;
            }
            textBox4.AppendText(@"监听成功" + Environment.NewLine);
            button2.Enabled = true;
            button5.Enabled = true;
            //开始监听:设置最大可以同时连接多少个请求
            socketListen.Listen(MAXClients);

            //实例化回调
            setCallBack     = SetTextValue;
            receiveCallBack = ReceiveMsg;
            setCmbCallBack  = AddCmbItem;
            sendCallBack    = SendFile;

            //创建线程
            AcceptSocketThread = new Thread(StartListen)
            {
                IsBackground = true
            };
            AcceptSocketThread.Start(socketListen);
        }
예제 #8
0
        //接收客户端发送消息的线程
        //Thread threadReceive;
        //转发消息的线程
        //Thread zhaunxianchen;
        /// <summary>
        /// 开始监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Start_Click(object sender, EventArgs e)
        {
            try
            {
                button2.Enabled = true;
                UDPjiantin();
                //当点击开始监听的时候 在服务器端创建一个负责监听IP地址和端口号的Socket
                socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //获取ip地址
                IPAddress ip = IPAddress.Parse(this.textBox1.Text.Trim());
                //创建端口号
                IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(this.textBox2.Text.Trim()));
                //绑定IP地址和端口号
                socketWatch.Bind(point);
                //开始监听:设置最大可以同时连接多少个请求
                socketWatch.Listen(60);

                //实例化回调
                setCallBack     = new SetTextValueCallBack(SetTextValue);
                receiveCallBack = new ReceiveMsgCallBack(ReceiveMsg);
                setCmbCallBack  = new SetCmbCallBack(AddCmbItem);//绑定soket
                sendCallBack    = new SendFileCallBack(SendFile);
                //查昵称
                zhangfalixian = new ReceiveMsgCallBack(zhuangfa);
                //创建线程
                AcceptSocketThread = new Thread(new ParameterizedThreadStart(StartListen));
                AcceptSocketThread.IsBackground = true;
                AcceptSocketThread.Start(socketWatch);
                MessageBox.Show("服务器开启成功");
                button1.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                button2.Enabled = false;
                button1.Enabled = true;
                IsUdpcRecvStart = false;
            }
        }