c#2005通过socket传递文本文件c/s---c/s

接收端

Form1.Designer.cs

namespace Client
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.btnConnect = new System.Windows.Forms.Button();
            this.lblState = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(94, 124);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "接收端";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // btnConnect
            //
            this.btnConnect.Location = new System.Drawing.Point(87, 60);
            this.btnConnect.Name = "btnConnect";
            this.btnConnect.Size = new System.Drawing.Size(82, 29);
            this.btnConnect.TabIndex = 1;
            this.btnConnect.Text = "开始监听";
            this.btnConnect.UseVisualStyleBackColor = true;
            this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
            //
            // lblState
            //
            this.lblState.AutoSize = true;
            this.lblState.Location = new System.Drawing.Point(32, 200);
            this.lblState.Name = "lblState";
            this.lblState.Size = new System.Drawing.Size(41, 12);
            this.lblState.TabIndex = 2;
            this.lblState.Text = "label1";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.lblState);
            this.Controls.Add(this.btnConnect);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "接收端";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button btnConnect;
        private System.Windows.Forms.Label lblState;
    }
}

 

 

接收端Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Net.Sockets;
namespace Client
{
    public partial class Form1 : Form
    {
        const int FILE_BLOCK = 1024 * 4;
        public Form1()
        {
            InitializeComponent();
        }

        System.Net.Sockets.Socket receivesocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
        //设置接收数据的地址
        System.Net.IPEndPoint hostipendpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any,8888);

        private void btnConnect_Click(object sender, EventArgs e)
        {
        
            receivesocket.Bind(hostipendpoint);

            // //监听

            receivesocket.Listen(2);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlgsavefile = new SaveFileDialog();
            dlgsavefile.Filter = "txt files (*.doc)|*.doc|All files (*.*)|*.*";
            string strfile = null;
            if (dlgsavefile.ShowDialog() == DialogResult.OK)
            {
                strfile = dlgsavefile.FileName;
               
            }

            System.IO.FileStream recfs = new System.IO.FileStream(strfile, System.IO.FileMode.OpenOrCreate);
            byte[] recbyte = new byte[FILE_BLOCK];

          
            System.Net.Sockets.Socket hostsocket = receivesocket.Accept();

            
            System.IO.BinaryWriter newfilestr = new System.IO.BinaryWriter(recfs);
            int got = 0;
            //use loop to read block by block
            int datalength = 0;
            do
            {
                got = hostsocket.Receive(recbyte);
                newfilestr.Write(recbyte, 0, got);
                datalength = datalength + got;
            }
            while ((got == FILE_BLOCK));
            lblState.Text =(datalength-1).ToString() + " byte";
            recfs.Close();
            hostsocket.Shutdown(System.Net.Sockets.SocketShutdown.Receive);
            hostsocket.Close();
       
        }


    }
}

接收端建立窗体dlgsavefile.cs(任何控件都没有)

Program.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Client
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

 

 

 

 

 

发送端

Form1.Designer.cs

namespace Server
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.btnSend = new System.Windows.Forms.Button();
            this.lblState = new System.Windows.Forms.Label();
            this.btnConnect = new System.Windows.Forms.Button();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            //
            // btnSend
            //
            this.btnSend.Location = new System.Drawing.Point(105, 146);
            this.btnSend.Name = "btnSend";
            this.btnSend.Size = new System.Drawing.Size(75, 23);
            this.btnSend.TabIndex = 0;
            this.btnSend.Text = "发送";
            this.btnSend.UseVisualStyleBackColor = true;
            this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
            //
            // lblState
            //
            this.lblState.AutoSize = true;
            this.lblState.Location = new System.Drawing.Point(37, 204);
            this.lblState.Name = "lblState";
            this.lblState.Size = new System.Drawing.Size(41, 12);
            this.lblState.TabIndex = 1;
            this.lblState.Text = "label1";
            //
            // btnConnect
            //
            this.btnConnect.Location = new System.Drawing.Point(105, 73);
            this.btnConnect.Name = "btnConnect";
            this.btnConnect.Size = new System.Drawing.Size(65, 25);
            this.btnConnect.TabIndex = 2;
            this.btnConnect.Text = "连 接";
            this.btnConnect.UseVisualStyleBackColor = true;
            this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
            //
            // timer1
            //
            this.timer1.Enabled = true;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.btnConnect);
            this.Controls.Add(this.lblState);
            this.Controls.Add(this.btnSend);
            this.Name = "Form1";
            this.Text = "发送端";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button btnSend;
        private System.Windows.Forms.Label lblState;
        private System.Windows.Forms.Button btnConnect;
        private System.Windows.Forms.Timer timer1;
    }
}

 

 

发送端Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Net.Sockets;

namespace Server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        System.Net.Sockets.Socket sendsocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
        System.Net.IPEndPoint hostipendpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 8888);

         private void btnConnect_Click(object sender, EventArgs e)
        {
              sendsocket.Connect(hostipendpoint);
         }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (sendsocket.Connected)
            {
                lblState.Text = "连接成功";
            
            }

         }

        private void btnSend_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlgopenfile = new OpenFileDialog();
            string strfile = null;
            if (dlgopenfile.ShowDialog() == DialogResult.OK)
            {
                strfile = dlgopenfile.FileName;//要发送的文件
            }


            System.IO.FileStream fs = new System.IO.FileStream(strfile, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read);
            byte[] fssize = new byte[fs.Length + 1];
            //removed -1
            System.IO.BinaryReader strread = new System.IO.BinaryReader(fs);
            strread.Read(fssize, 0, fssize.Length - 1);
            //sendsocket.Connect(hostipendpoint);
            sendsocket.Send(fssize);
            lblState.Text = fs.Length.ToString() + " byte" ;
            fs.Close();
            sendsocket.Shutdown(System.Net.Sockets.SocketShutdown.Send);
            sendsocket.Close();

        }


    }
}

 

Program.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Server
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

 

你可能感兴趣的:(c#2005通过socket传递文本文件c/s---c/s)