.net C# (文件接收)
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e);
private Socket s;
TcpListener tl;
public void lis()
{
string path = "E:\\项目\\WindowsFormsApplication3\\Form1.cs"; //要传入文件的路径
tl = new TcpListener(9999);
tl.Start();
MessageBox .Show ("等待接受");
s = tl.AcceptSocket();
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); //注意这个的属性和send端有所不同
BinaryWriter binarywrite = new BinaryWriter(fs);
int count;
byte[] b = new byte[4098];
while ((count = s.Receive(b, 4098, SocketFlags.None)) != 0) //这个是接受文件流
{
binarywrite.Write(b,0,count); //将接收的流用写成文件
}
binarywrite.Close();
fs.Close();
s.Close();
tl.Stop();
Console.WriteLine("文件传输完毕");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(textBox1.Text.ToString());
}
}
}
错误 1 “WindowsFormsApplication2.Form1.textBox1_TextChanged(object, System.EventArgs)”必须声明主体,因为它未标记为 abstract、extern 或 partial
不懂,请高手帮看下 指点指点 谢谢!!!!
--------------------编程问答-------------------- private void textBox1_TextChanged(object sender, EventArgs e)
{
private Socket s;
TcpListener tl;
public void lis()
{
string path = "E:\\项目\\WindowsFormsApplication3\\Form1.cs"; //要传入文件的路径
tl = new TcpListener(9999);
tl.Start();
MessageBox .Show ("等待接受");
s = tl.AcceptSocket();
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); //注意这个的属性和send端有所不同
BinaryWriter binarywrite = new BinaryWriter(fs);
int count;
byte[] b = new byte[4098];
while ((count = s.Receive(b, 4098, SocketFlags.None)) != 0) //这个是接受文件流
{
binarywrite.Write(b,0,count); //将接收的流用写成文件
}
binarywrite.Close();
fs.Close();
s.Close();
tl.Stop();
Console.WriteLine("文件传输完毕");
} --------------------编程问答-------------------- private Socket s;
TcpListener tl;
public void lis()
这三个,放到{}内部 --------------------编程问答-------------------- 或者在界面中双击textBox1,自动会出现textBox1_TextChanged事件,然后把代码写在里面就不会出错了 --------------------编程问答-------------------- 额。。貌似是我看错了 2L说的对 --------------------编程问答-------------------- 你的代码放到代码块里吧,不然很难看。
我没有看到你的textBox1_TextChanged函数的函数体。 --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e);
Socket s;
TcpListener tl;
public void lis()
{
string path = "E:\\项目\\WindowsFormsApplication3\\Form1.cs"; //要传入文件的路径
tl = new TcpListener(9999);
tl.Start();
MessageBox.Show ("等待接受");
s = tl.AcceptSocket();
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); //注意这个的属性和send端有所不同
BinaryWriter binarywrite = new BinaryWriter(fs);
int count;
byte[] b = new byte[4098];
while ((count = s.Receive(b, 4098, SocketFlags.None)) != 0) //这个是接受文件流
{
binarywrite.Write(b,0,count); //将接收的流用写成文件
}
binarywrite.Close();
fs.Close();
s.Close();
tl.Stop();
MessageBox .Show ("文件传输完毕");
}
}
}
我放在form_load里 还是出现
错误 1 “WindowsFormsApplication3.接收.接收_Load(object, System.EventArgs)”必须声明主体,因为它未标记为 abstract、extern 或 partial E:\项目\WindowsFormsApplication3\接收.cs 23 22 WindowsFormsApplication3
--------------------编程问答-------------------- private void Form2_Load(object sender, EventArgs e);
这个必须有函数体
应该是这样的
private void Form2_Load(object sender, EventArgs e)
{
}
--------------------编程问答-------------------- 我试了,错误更多,20多个
其中:
错误 5 应输入 class、delegate、enum、inte易做图ce 或 struct E:\项目\WindowsFormsApplication3\Form2.cs 30 22 WindowsFormsApplication3
--------------------编程问答-------------------- textBox1_TextChanged为什么会有两个呢?
private void textBox1_TextChanged(object sender, EventArgs e)
{
……
}
private void textBox1_TextChanged(object sender, EventArgs e);
搞不懂,删一个试试, --------------------编程问答-------------------- 还有,就是你不用的事件删了,何必放着。
补充:.NET技术 , C#