实例介绍
【实例简介】
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | 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.Net; using System.Net.Sockets; using System.Threading; using System.IO; namespace _2222222 { public partial class frmClient : Form { public frmClient() { InitializeComponent(); TextBox.CheckForIllegalCrossThreadCalls = false ; } Thread threadClient = null ; // 创建用于接收服务端消息的 线程; Socket sockClient = null ; private void btnConnect_Click( object sender, EventArgs e) { IPAddress ip = IPAddress.Parse(txtIp.Text.Trim()); IPEndPoint endPoint= new IPEndPoint (ip, int .Parse(txtPort.Text.Trim())); sockClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { ShowMsg( "与服务器连接中……" ); sockClient.Connect(endPoint); } catch (SocketException se) { MessageBox.Show(se.Message); return ; //this.Close(); } ShowMsg( "与服务器连接成功!!!" ); threadClient = new Thread(RecMsg); threadClient.IsBackground = true ; threadClient.Start(); } void RecMsg() { while ( true ) { // 定义一个2M的缓存区; byte [] arrMsgRec = new byte [1024 * 1024 * 2]; // 将接受到的数据存入到输入 arrMsgRec中; int length = -1; try { length = sockClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度; } catch (SocketException se) { ShowMsg( "异常;" se.Message); return ; } catch (Exception e) { ShowMsg( "异常:" e.Message); return ; } if (arrMsgRec[0] == 0) // 表示接收到的是消息数据; { string strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length-1); // 将接受到的字节数据转化成字符串; ShowMsg(strMsg); } if (arrMsgRec[0] == 1) // 表示接收到的是文件数据; { try { SaveFileDialog sfd = new SaveFileDialog(); if (sfd.ShowDialog( this ) == System.Windows.Forms.DialogResult.OK) { // 在上边的 sfd.ShowDialog() 的括号里边一定要加上 this 否则就不会弹出 另存为 的对话框,而弹出的是本类的其他窗口,,这个一定要注意!!!【解释:加了this的sfd.ShowDialog(this),“另存为”窗口的指针才能被SaveFileDialog的对象调用,若不加thisSaveFileDialog 的对象调用的是本类的其他窗口了,当然不弹出“另存为”窗口。】 string fileSavePath = sfd.FileName; // 获得文件保存的路径; // 创建文件流,然后根据路径创建文件; using (FileStream fs = new FileStream(fileSavePath, FileMode.Create)) { fs.Write(arrMsgRec, 1, length - 1); ShowMsg( "文件保存成功:" fileSavePath); } } } catch (Exception aaa) { MessageBox.Show(aaa.Message); } } } } void ShowMsg( string str) { txtMsg.AppendText(str "\r\n" ); } // 发送消息; private void btnSendMsg_Click( object sender, EventArgs e) { string strMsg = txtName.Text.Trim() "\r\n" " -->" txtSendMsg.Text.Trim() "\r\n" ; byte [] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg); byte [] arrSendMsg = new byte [arrMsg.Length 1]; arrSendMsg[0] = 0; // 用来表示发送的是消息数据 Buffer.BlockCopy(arrMsg, 0, arrSendMsg, 1, arrMsg.Length); sockClient.Send(arrSendMsg); // 发送消息; ShowMsg(strMsg); txtSendMsg.Clear(); } // 选择要发送的文件; private void btnSelectFile_Click( object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = "D:\\" ; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { txtSelectFile.Text = ofd.FileName; } } //向服务器端发送文件 private void btnSendFile_Click( object sender, EventArgs e) { if ( string .IsNullOrEmpty(txtSelectFile.Text)) { MessageBox.Show( "请选择要发送的文件!!!" ); } else { // 用文件流打开用户要发送的文件; using (FileStream fs = new FileStream(txtSelectFile.Text, FileMode.Open)) { //在发送文件以前先给好友发送这个文件的名字 扩展名,方便后面的保存操作; string fileName = System.IO.Path.GetFileName(txtSelectFile.Text); string fileExtension = System.IO.Path.GetExtension(txtSelectFile.Text); string strMsg = "我给你发送的文件为: " fileName "\r\n" ; byte [] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg); byte [] arrSendMsg = new byte [arrMsg.Length 1]; arrSendMsg[0] = 0; // 用来表示发送的是消息数据 Buffer.BlockCopy(arrMsg, 0, arrSendMsg, 1, arrMsg.Length); sockClient.Send(arrSendMsg); // 发送消息; byte [] arrFile = new byte [1024 * 1024 * 2]; int length = fs.Read(arrFile, 0, arrFile.Length); // 将文件中的数据读到arrFile数组中; byte [] arrFileSend = new byte [length 1]; arrFileSend[0] = 1; // 用来表示发送的是文件数据; Buffer.BlockCopy(arrFile, 0, arrFileSend, 1, length); // 还有一个 CopyTo的方法,但是在这里不适合; 当然还可以用for循环自己转化; sockClient.Send(arrFileSend); // 发送数据到服务端; txtSelectFile.Clear(); } } } } } |
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论