为什么我的第二个表单在启动时没有加载?它被卡住了吗?

时间:2016-10-23 20:33:42

标签: c# winforms sockets events tcp

所以我只想说明我对C#中的Tcp / IP编程非常陌生。另外,我已经将问题中的IP更改为与我项目中的IP不匹配,因为不是不想泄漏它。

当我启动项目时,它应该打开2个表单(客户端和服务器) 但由于某种原因,它只打开客户端winform应用程序。 (我已经改变了项目设置中的启动方法以启动它们)

我最好的猜测是,当我在Form_Load事件中调用它时,它仍然试图启动TcpListener。

为什么会发生这种情况,我该如何解决?

这是服务器(不启动的服务器)

using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Net;


namespace SimpleServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string rd;
        byte[] b1;
        string v;
        int m;
        //TcpListener list;

        Int32 port = 8080;
        Int32 port1 = 8080;


        IPAddress localAddr = IPAddress.Parse("192.168.0.1");

        private void BrowseBtn_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
                TcpListener list = new TcpListener(localAddr, port1);
                //list = new TcpListener(port1);
                list.Start();
                TcpClient client = list.AcceptTcpClient();
                Stream s = client.GetStream();
                b1 = new byte[m];
                s.Read(b1, 0, b1.Length);
                File.WriteAllBytes(textBox1.Text + "\\" + rd.Substring(0, rd.LastIndexOf('.')), b1);
                list.Stop();
                client.Close();
                statusLabel.Text = "File Received......";
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            IPAddress localAddr = IPAddress.Parse("192.168.0.1"); //changed it from my main ip
            TcpListener list = new TcpListener(localAddr, port);
            //TcpListener list = new TcpListener(port);
            list.Start();
            TcpClient client = list.AcceptTcpClient();
            MessageBox.Show("Client trying to connect");
            StreamReader sr = new StreamReader(client.GetStream());
            rd = sr.ReadLine();
            v = rd.Substring(rd.LastIndexOf('.') + 1);
            m = int.Parse(v);
            list.Stop();
            client.Close();
        }
    }
}

这是客户端源代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;

namespace SimpleClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string n;
        byte[] b1;
        OpenFileDialog op;

        private void browseButton_Click(object sender, EventArgs e)
        {
            op = new OpenFileDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {
                string t = textBox1.Text;
                t = op.FileName;
                FileInfo fi = new FileInfo(textBox1.Text = op.FileName);
                n = fi.Name + "." + fi.Length;
                TcpClient client = new TcpClient("22.232.23.22", 8080);
                StreamWriter sw = new StreamWriter(client.GetStream());
                sw.WriteLine(n);
                sw.Flush();
                statusLabel.Text = "File Transferred....";
            }
        }

        private void sendBtn_Click(object sender, EventArgs e)
        {
            TcpClient client = new TcpClient("22.232.23.22", 8080);
            Stream s = client.GetStream();
            b1 = File.ReadAllBytes(op.FileName);
            s.Write(b1, 0, b1.Length);
            client.Close();
            statusLabel.Text = "File Transferred2....";
        }
    }
}

2 个答案:

答案 0 :(得分:0)

解决方案中只能有一个启动项目...右键单击解决方案资源管理器中的项目,然后选择"设置为启动项目"

部署exe时,您必须手动,按计划任务启动服务器Exe,或者更好地使Server Exe作为服务运行。

另一种方法是System.Diagnostic.Process.Start("..\bin\Debug\SimpleServer.exe");

答案 1 :(得分:0)

问题在于它无法连接,因为ip地址在客户端和服务器中没有匹配。两者都必须是IPV4地址。