私はC#でTcp/IPプログラミングを始めたばかりであることを明確にしたいと思います。また、問題のIPをリークさせたくないためにプロジェクトのものと一致しないように変更しました。起動時に2番目のフォームが読み込まれないのはなぜですか?それは固まっていますか?
私はこれまでに2つのフォーム(クライアント&サーバ) を開く必要がありますが、何らかの理由でクライアントwinformアプリケーションを開くだけです。 (私はプロジェクトの設定でstartメソッドを両方とも起動するように変更しました)
Form_Loadイベントで呼び出すときにTcpListenerを開始しようとしているのではないでしょうか。
なぜこのようなことが起こり、どうすれば解決できますか?ここで
は右...あなたが唯一の解決策の1つのスタートアッププロジェクトを持つことができ、サーバ(起動does notの1)
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番目のフォームを表示しようとしています –
このif(op.ShowDialog()== DialogResult.OK)のelse if条件を追加します。いいえ||初心者のためにキャンセルする。また、あなたのコードを踏んだりしてください – MethodMan
何をしなければならないのは、1つだけでなく2つのプロジェクトを開いていることです。 そして、私はこのプロジェクトを最初に解決しようとしていませんでした。なぜ私の他のプロジェクトがロードされていないのですか?Load_Eventをチェックしましたか? –