为什么我的第二个表单在启动时不能加载?它卡住了吗?
本文关键字:加载 不能 第二个 我的 表单 启动 为什么 | 更新日期: 2023-09-27 18:14:26
所以我只是想说清楚,我对c#中的Tcp/IP编程非常陌生。此外,我已经改变了问题中的IP,使其与我的项目中的IP不匹配,因为我不想泄露它。
当我开始这个项目时,它应该打开2个表单(Client &服务器)但由于某种原因,它只打开客户端窗体应用程序。(我已经改变了项目设置中的启动方法来启动两者)
我最好的猜测是,当我在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....";
}
}
}
一个解决方案中只能有一个启动项目…在解决方案资源管理器中右键单击"项目",选择"设置为启动项目"
当你部署的exe's,你将不得不启动服务器的exe要么手动,通过计划任务或更好的是,使服务器的exe运行作为一个服务。
另一种方法是System.Diagnostic.Process.Start("..'bin'Debug'SimpleServer.exe");
问题是它无法连接,因为客户端和服务器的ip地址不匹配。都必须是IPV4地址