Пример #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
            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);
        }