主题:求助文件传输问题
小菜我刚学C#,尝试编写一个简单的文件传输程序,差不多是照书上抄的,可是调试的时候老是报错,求助各位大侠,不胜感激!
一下是客户端代码,设计界面就是3框2按钮不发图了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace sj1
{
public partial class Form1 : Form
{
private Socket mySock;
private TcpClient listener;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.TextBox textBox1; //发送方IP
private System.Windows.Forms.TextBox textBox2; //发送方端口
private System.Windows.Forms.TextBox textBox3; //监听端口
private System.Windows.Forms.Button button1; //监听按钮
private System.Windows.Forms.Button button2; //关闭按钮
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
listener = new TcpClient(Dns.Resolve(Dns.GetHostName()).AddressList[0], Int32.Parse(textBox3.Text));
listener.Start();
Thread thread = new Thread(new ThreadStart(receive));
thread.Start();
}
catch (Exception ee) { MessageBox.Show(ee.Message); }
}
private void receive()
{
mySock = listener.AcceptSocket();
if (mySock.Connected)
{
MessageBox.Show("you have a file to save now!");
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = "C:\\";
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
IPEndPoint ipep = (IPEndPoint)mySock.RemoteEndPoint;
textBox2.Text = ipep.Port.ToString();
textBox1.Text = ipep.Address.ToString();
FileStream fileStream = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Write);
int readNumber = 0;
byte[] bye = new byte[8];
NetworkStream networkStream = new NetworkStream(mySock);
while ((readNumber = networkStream.Read(bye, 0, 8)) > 0)
{
fileStream.Write(bye, 0, readNumber);
fileStream.Flush();
}
flieStream.Close();
}
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
mySock.Close();
listener.Stop();
textBox1.Text = textBox2.Text = "";
}
catch { }
}
}
}
报错内容是 类型sj1.Form1已经包含"textBox1,textBox2,textBox3,button1,button2"的定义
我尝试删除
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.TextBox textBox1; //发送方IP
private System.Windows.Forms.TextBox textBox2; //发送方端口
private System.Windows.Forms.TextBox textBox3; //监听端口
private System.Windows.Forms.Button button1; //监听按钮
private System.Windows.Forms.Button button2; //关闭按钮
报错更多。
可怜我刚学C#根本看不懂都啥意思。
神啊救救我吧!
一下是客户端代码,设计界面就是3框2按钮不发图了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace sj1
{
public partial class Form1 : Form
{
private Socket mySock;
private TcpClient listener;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.TextBox textBox1; //发送方IP
private System.Windows.Forms.TextBox textBox2; //发送方端口
private System.Windows.Forms.TextBox textBox3; //监听端口
private System.Windows.Forms.Button button1; //监听按钮
private System.Windows.Forms.Button button2; //关闭按钮
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
listener = new TcpClient(Dns.Resolve(Dns.GetHostName()).AddressList[0], Int32.Parse(textBox3.Text));
listener.Start();
Thread thread = new Thread(new ThreadStart(receive));
thread.Start();
}
catch (Exception ee) { MessageBox.Show(ee.Message); }
}
private void receive()
{
mySock = listener.AcceptSocket();
if (mySock.Connected)
{
MessageBox.Show("you have a file to save now!");
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = "C:\\";
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
IPEndPoint ipep = (IPEndPoint)mySock.RemoteEndPoint;
textBox2.Text = ipep.Port.ToString();
textBox1.Text = ipep.Address.ToString();
FileStream fileStream = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Write);
int readNumber = 0;
byte[] bye = new byte[8];
NetworkStream networkStream = new NetworkStream(mySock);
while ((readNumber = networkStream.Read(bye, 0, 8)) > 0)
{
fileStream.Write(bye, 0, readNumber);
fileStream.Flush();
}
flieStream.Close();
}
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
mySock.Close();
listener.Stop();
textBox1.Text = textBox2.Text = "";
}
catch { }
}
}
}
报错内容是 类型sj1.Form1已经包含"textBox1,textBox2,textBox3,button1,button2"的定义
我尝试删除
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.TextBox textBox1; //发送方IP
private System.Windows.Forms.TextBox textBox2; //发送方端口
private System.Windows.Forms.TextBox textBox3; //监听端口
private System.Windows.Forms.Button button1; //监听按钮
private System.Windows.Forms.Button button2; //关闭按钮
报错更多。
可怜我刚学C#根本看不懂都啥意思。
神啊救救我吧!