fserver.cs
资源名称:类似QQ聊天程序.rar [点击查看]
上传用户:yan_wy
上传日期:2007-06-09
资源大小:112k
文件大小:5k
源码类别:
ICQ/即时通讯
开发平台:
C#
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- using System.Text;
- using System.Threading ;
- namespace qq
- {
- /// <summary>
- /// fserver 的摘要说明。
- /// </summary>
- public class fserver : System.Windows.Forms.Form
- {
- int jieshou;
- Form1 form;
- string id;
- string path;
- Thread outputThread ;
- private TcpListener listener;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Button button1;
- Socket connection;
- System.Net.Sockets .NetworkStream socketstream;
- BinaryWriter writer;
- BinaryReader reader;
- private System.Windows.Forms.OpenFileDialog openFileDialog1;
- private System.Windows.Forms.Button button2;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public fserver(Form1 f,string a)
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- form=f;
- this.id=a;
- InitializeComponent();
- this.jieshou=0;
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if(components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.label1 = new System.Windows.Forms.Label();
- this.button1 = new System.Windows.Forms.Button();
- this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
- this.button2 = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // label1
- //
- this.label1.Location = new System.Drawing.Point(64, 32);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(120, 32);
- this.label1.TabIndex = 0;
- this.label1.Text = "发送文件";
- this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(144, 112);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(56, 48);
- this.button1.TabIndex = 1;
- this.button1.Text = "发送";
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // button2
- //
- this.button2.Location = new System.Drawing.Point(32, 112);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(64, 48);
- this.button2.TabIndex = 2;
- this.button2.Text = "取消";
- this.button2.Click += new System.EventHandler(this.button2_Click);
- //
- // fserver
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(264, 213);
- this.Controls.Add(this.button2);
- this.Controls.Add(this.button1);
- this.Controls.Add(this.label1);
- this.Name = "fserver";
- this.Text = "fserver";
- this.ResumeLayout(false);
- }
- #endregion
- public void sendfile()
- {
- this.label1.Text="等待对方。。。。";
- form.use.sendmessage(form.userid);
- form.use.sendmessage(id);
- form.use.sendmessage("发送文件");
- connection=listener.AcceptSocket();
- this.label1.Text="正在发送。。。。";
- socketstream=new NetworkStream(connection);
- writer=new BinaryWriter (socketstream);
- reader=new BinaryReader(socketstream);
- try
- {
- byte[] b = new byte[1024];
- System.IO.FileInfo fle=new FileInfo(path);
- FileStream fs= File.Open(path, FileMode.Open);
- writer.Write(fs.Length.ToString());
- writer.Write(fle.Name);
- while (fs.Read(b,0,b.Length) > 0)
- {
- writer.Write(b);
- }
- b = System.Text.Encoding.ASCII.GetBytes("传输结束") ;
- writer.Write(b);
- this.label1.Text="发送结束!!!!";
- writer.Close();
- reader.Close();
- fs.Close();
- connection.Close();
- listener.Stop();
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
- public void setup()
- {
- // string path;
- if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
- {
- path = this.openFileDialog1.FileName;
- }
- try
- {
- IPAddress ipAddress = Dns.Resolve(this.form.ip).AddressList[0];
- this.listener=new TcpListener(ipAddress,5002);
- listener.Start();
- outputThread =new Thread (new ThreadStart (sendfile));
- outputThread.Start ();
- }
- catch(Exception ee)
- {
- MessageBox.Show(ee.ToString());
- }
- }
- private void button1_Click(object sender, System.EventArgs e)
- {
- this.jieshou=1;
- this.setup();
- }
- private void button2_Click(object sender, System.EventArgs e)
- {
- if(this.jieshou==1)
- {
- this.outputThread.Abort();
- listener.Stop();
- }
- this.Close();
- }
- }
- }