首页 新闻 会员 周边

C# 简单P2P实现(聊天+文件传输)

0
悬赏园豆:20 [待解决问题]

我想实现能够聊天跟穿文件。

1 //侦听
2   private void Listen()
3 {
4 try
5 {
6 listener = new TcpListener(5656);
7 listener.Start();
8 while (listenerRun)
9 {
10 s = listener.AcceptSocket();
11 Byte[] stream = new Byte[80];
12 int i = s.Receive(stream);
13 string message = System.Text.Encoding.UTF8.GetString(stream);
14 //判断message类型
15   txtRecord.AppendText(message);
16 //ReceiveFile();
17   }
18 }
19 catch (Exception e)
20 {
21 MessageBox.Show(e.Message);
22 }
23 }
24
25 //发送信息
26 private void Send()
27 {
28 try
29 {
30 //信息
31 string Msg = txtNickName.Text + "\r\n" + txtMsg.Text+"\r\n";
32 client = new TcpClient(txtIP.Text, 5656);
33 NetworkStream netStream = client.GetStream();
34 StreamWriter reqStreamW = new StreamWriter(netStream);
35 reqStreamW.Write(Msg);
36 reqStreamW.Flush();
37 reqStreamW.Close();
38 client.Close();
39 txtRecord.AppendText(Msg);
40 txtMsg.Clear();
41
42 //SendFile();
43 }
44 catch (Exception)
45 {
46 throw;
47 }
48 }
1 //发送文件
2 private void SendFile()
3 {
4 if (strFile != "")//strFile是全局变量,用于保存发送发的文件路径
5 {
6 //发送传送文件请求
7 isFile = true;//isFile是个bool类型的全局变量,初始值为false,当为true是在P2P另一端弹出是否接收文件对话框
8 }
9 //对方允许接收文件
10 if (ready == true)//ready是bool类型全局变量,初始值为false,用于判断对方是否接收文件
11 {
12 //准备发送
13 FileStream file = new FileStream(strFile, FileMode.Open, FileAccess.Read);
14 BinaryReader binaryReader = new BinaryReader(file);
15 byte[] b = new byte[4098];
16 int data;
17 txtRecord.AppendText("正在发送文件...");
18 while ((data=binaryReader.Read(b,0,4098))!=0)
19 {
20 client.Client.Send(b, data, SocketFlags.None);
21 }
22 client.Client.Shutdown(SocketShutdown.Both);
23 binaryReader.Close();
24 file.Close();
25 }
26 else
27 {
28 txtRecord.AppendText("对方拒绝接收文件!");
29 }
30 }
31 //接收文件
32 private void ReceiveFile()
33 {
34 //对方发送文件
35 if (isFile = true)
36 {
37 if (MessageBox.Show("对方给你发送文件,是否接收?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
38 {
39 if (sfdFile.ShowDialog() == DialogResult.OK)
40 {
41 //开始接收文件
42 sfdFile.FileName = "文件";
43 txtRecord.AppendText("等待接收文件...");
44 FileStream fs = new FileStream("D:\\",FileMode.OpenOrCreate,FileAccess.Write);
45 BinaryWriter binaryWrite = new BinaryWriter(fs);
46 int count;
47 byte[] b = new byte[4098];
48 while ((count=s.Receive(b,4098,SocketFlags.None))!=0)
49 {
50 binaryWrite.Write(b, 0, count);
51 }
52 binaryWrite.Close();
53 fs.Close();
54 txtRecord.AppendText("文件传输完毕!");
55 }
56 }
57 else
58 {
59 //拒绝接收文件
60 ready = false;
61
62 }
63
64 isFile = false;
65 }
66 }

我设置了3个全局变量string strFile,bool isFile,bool ready。把选中要发送的文件时候把发送端文件路径给strFile,此时isFile设置为true。接收端判断isFile为true则弹出MessageBox提示是否接受文件,如果确定接收则弹出保存对话框,选中路径后将ready设置为true,发送端判断ready为true就开始发送文件

Beta3.0的主页 Beta3.0 | 初学一级 | 园豆:177
提问于:2011-04-29 22:53
< >
分享
所有回答(5)
1

啥问题呢?

1.P2P没那么简单,这能在局域网用,P2p Nat穿越很麻烦的,别那么说省得被喷···

2.你这样同步操作会卡UI的

3.如果只是发消息这样做就行,也许你注意了传文件可能不好做。也就是这个时候需要设置一套信令系统

具体可以这样:简单点,学习FTP把信令和数据传输分开做,一个端口监听信令,一个专门传输数据。

4.对于文件传输:一个是请求--应答信令 。第二个就是发送文件信息信令(文件名、类型什么的,不然对方怎么知道什么时候传完了呢?)

may this help:)

LittlePeng | 园豆:3445 (老鸟四级) | 2011-05-02 11:07
0
麒麟 | 园豆:389 (菜鸟二级) | 2011-05-04 06:34
0

(⊙o⊙)哦 看了下

寒@鹏 | 园豆:217 (菜鸟二级) | 2011-09-25 17:29
1

园子里已经有非常成熟的通信框架ESFramework,可以用来做类似QQ的IM软件,支持P2P,传文件,而且还可以支持语音视频聊天,我们公司就是用的该框架来开发视频会议系统的。可以参考一下。

C#开源即时通讯GGTalk | 园豆:162 (初学一级) | 2013-04-26 10:36
0

没有源码呢

wanglgkaka | 园豆:218 (菜鸟二级) | 2013-12-09 22:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册