由于目标机器主动拒绝,因此无法建立连接

时间:2014-07-10 04:12:04

标签: c# multithreading sockets tcp windows-applications

我意识到这可能被视为重复,但我查看了其他回复,他们没有为我解决问题。 我试过了: 禁用防火墙; 使用netstat命令检查端口; 我用c#写了一个windows应用程序视频聊天。 当我运行我的程序时,几秒钟后发生了这个错误。

我的代码:

    Thread th;
    TcpClient tcpC;
    NetworkStream ns;
    NetworkStream nst;
    TcpListener tcpL;
    BinaryWriter bw;
    MemoryStream ms;
    private Socket r;

    public Form1()
    {
        InitializeComponent();

        r = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    }
    private void  webCamCapture1_ImageCaptured(object source, WebCam_Capture.WebcamEventArgs e)
    {
    this.pictureBox1.Image = e.WebCamImage;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        th = new Thread(new System.Threading.ThreadStart(Start_Receiving_Video_Conference));
        th.Start();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        th.Abort();
        tcpC.Close();
        tcpL.Stop();
        ns.Close();
        nst.Flush();
        nst.Close();
        bw.Close();
        ms.Close();
        r.Close();
    }

    private void Start_Receiving_Video_Conference()
    {
        try
        {

            // Open The Port
             tcpL = new TcpListener(int.Parse(txtPort.Text));
            tcpL.Start();                        // Start Listening on That Port
            Socket  s = tcpL.AcceptSocket();         // Accept Any Request From Client and Start a Session
            ns = new NetworkStream(s);   // Receives The Binary Data From Port
            pictureBox2.Image = Image.FromStream(ns);
            tcpL.Stop();                             // Close TCP Session
            if (s.Connected == true)             // Looping While Connected to Receive Another Message 
            {
                    Start_Receiving_Video_Conference();              // Back to First Method
            }
            ns.Flush();
        }
        catch (Exception) { }
    }

    private void Start_Sending_Video_Conference(string remote_IP, int port_number)
    {
        try
        {
            ms = new MemoryStream();// Store it in Binary Array as Stream
            pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] arrImage = ms.GetBuffer();
            tcpC = new TcpClient(remote_IP, port_number);//Connecting with server
            nst = tcpC.GetStream();
            bw  = new BinaryWriter(nst);
            bw.Write(arrImage);//send the stream to above address
            ms.Flush();
            bw.Flush();
            nst.Flush();
            ms.Close();
            bw.Close();
            nst.Close();
            tcpC.Close();
        }
        catch (Exception ex)
        {
            timer1.Enabled = false;
            MessageBox.Show(ex.Message, "Video Conference Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Start_Sending_Video_Conference(txtIp.Text, int.Parse(txtPort.Text));
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        webCamCapture1.TimeToCapture_milliseconds = 1;
        webCamCapture1.Start(0);
        timer1.Enabled = true;
    }

    private void btnStop_Click(object sender, EventArgs e)
    {
        webCamCapture1.Stop();
        timer1.Enabled = false;
    }

0 个答案:

没有答案