/** * 接受信息 */ public void ReceiveMsg(Object obj) { Socket aSocket = (Socket)obj; while (true) { try { NetworkStream ns = new NetworkStream(aSocket); StreamReader sr = new StreamReader(ns); string resultStr = sr.ReadLine(); MsgData md = MsgData.DeserializeMsg(resultStr); string ipAndPort = aSocket.RemoteEndPoint.ToString(); switch (md.type) { case "TEXT": this.showMsg(md, ipAndPort); break; case "FILE": Boolean accecptable = this.isFileAccept(md); if (!accecptable) { continue; } string folderP = FileTranser.getFolderPath(); if (folderP.Length == 0) { continue; } this.WriteFile(md, folderP); break; } } catch (Exception ex) { f1.appendToHistory("receive异常:" + ex.Message + " - "); f1.appendToHistory("断开与" + aSocket.RemoteEndPoint.ToString() + "连接\n"); aSocket.Shutdown(SocketShutdown.Both); aSocket.Close(); break; } } }
private void btn_file_Click(object sender, EventArgs e) { String ipAndPort = this.getOneTarget(); if (ipAndPort.Length == 0) { return; } String fileFullPath = FileTranser.getFileName(); if (fileFullPath.Length == 0) { return; } translateFile_simple(ipAndPort, fileFullPath); }
/** * 传文件 */ public void translateFile_simple(String ipAndPort, String fileFullPath) { string filename = System.IO.Path.GetFileName(fileFullPath); System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileFullPath); long fileSize = fileInfo.Length; MsgData md = new MsgData(); md.type = "FILE"; md.fileName = filename; md.fileSize = fileSize; md.msg = FileTranser.getFileContent(fileFullPath); String mdString = MsgData.SerializeMsg(md); String error = alive_list.sendMsg(mdString, ipAndPort); if (error.Length > 0) { appendToHistory("文件传输异常:" + error + "\n"); return; } }
private void button1_Click_1(object sender, EventArgs e) { string folderPath = FileTranser.getFolderPath(); tb_msg.Text = folderPath; }