小弟在做一个文件传输的小程序(C#),一个客户端,一个服务器端,两者是独立的,现在让客户端发文件给服务器端,socket传输。
现在服务器端文件接收的时候,怎样保证服务器端文件名与客户端名一致。并且可以让他们自动运行,人为的不用去管。
我服务器端的程序如下,请大家指教啊using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Text;namespace server
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Thread thThreadRead;
//创建线程,用以侦听端口号,接收信息
private TcpListener tlTcpListen;
//侦听端口号
private bool blistener = true;
//设定标示位,判断侦听状态
private Socket stRead; private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.TextBox textBox2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// 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.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(96, 48);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "8000";
// 
// textBox2
// 
this.textBox2.Location = new System.Drawing.Point(96, 112);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
// 
// button1
// 
this.button1.Font = new System.Drawing.Font("宋体", 10F);
this.button1.Location = new System.Drawing.Point(64, 200);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 32);
this.button1.TabIndex = 4;
this.button1.Text = "开启线程";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// button2
// 
this.button2.Font = new System.Drawing.Font("宋体", 10F);
this.button2.Location = new System.Drawing.Point(152, 200);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 32);
this.button2.TabIndex = 4;
this.button2.Text = "退出程序";
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(24, 56);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 5;
this.label1.Text = "端口号";
// 
// label2
// 
this.label2.Location = new System.Drawing.Point(16, 120);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 8;
this.label2.Text = "存放路径";
// 
// button3
// 
this.button3.Location = new System.Drawing.Point(216, 112);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(40, 23);
this.button3.TabIndex = 10;
this.button3.Text = "浏览";
this.button3.Click += new System.EventHandler(this.button3_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "server";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}
private void Listen()
{
try
{
IPHostEntry ieh = Dns.GetHostByName(Dns.GetHostName());
IPAddress LocalIP = ieh.AddressList[0]; int LocalPort = Int32.Parse(textBox1.Text);
                string ReceiveFileName = textBox2.Text.Trim();
tlTcpListen = new TcpListener(LocalIP, LocalPort);
//以8000端口号来初始化TcpListener实例
tlTcpListen.Start();
//开始监听网络的连接请求
stRead = tlTcpListen.AcceptSocket();
//通过连接请求,并获得接收数据时使用的Socket实例
EndPoint tempRemoteEP = stRead.RemoteEndPoint;
IPEndPoint tempRemoteIP = (IPEndPoint)tempRemoteEP;
//获取请求的远程计算机名称
IPHostEntry host = Dns.GetHostByAddress(tempRemoteIP.Address);
string sHostName = host.HostName;
string ReceiveContent = "";
string sTime = DateTime.Now.ToShortTimeString();
int iRead;
//循环侦听
while (blistener)
{
//获取接收数据时的时间
Byte[] byRead = new Byte[229888];
iRead = stRead.ReceiveFrom(byRead, ref tempRemoteEP);
                   
//读取完成后退出循环 
if (iRead <= 0)
{
//  this.button1.Enabled = true;
//保存数据流为本地文件
byte[] content = Encoding.Default.GetBytes(ReceiveContent);
try
{
//创建文件流对象实例 
FileStream fs = new FileStream(ReceiveFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
// 写入文件
                            
fs.Write(content,0,content.Length); 
fs.Close();
}
catch (Exception fe)
{
MessageBox.Show("文件创建/写入错误:" + fe.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information);
} stRead.Close();
tlTcpListen.Stop();
//关闭侦听
thThreadRead.Abort();
//中止线程
break;
}
else
{
//将读取的字节数转换为字符串 
ReceiveContent += Encoding.Default.GetString(byRead);

} }
}
catch (System.Security.SecurityException)
{
MessageBox.Show("侦听失败!", "错误");
this.button1.Enabled = true;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
this.Close();
} private void button1_Click(object sender, System.EventArgs e)
{
thThreadRead = new Thread(new ThreadStart(Listen));
//以Listen过程来初始化Thread实例
thThreadRead.Start();
//启动线程
button1.Enabled = false;
button2.Enabled = true; } private void button3_Click(object sender, System.EventArgs e)
{
saveFileDialog1.Title = "请选择要用来保存的文件夹";
DialogResult drTemp = saveFileDialog1.ShowDialog();
if (drTemp ==DialogResult.OK && saveFileDialog1.FileName != string.Empty)
{
textBox2.Text = saveFileDialog1.FileName;
button1.Enabled = true;
}
}
}
}如果你知道的告诉我,好么?谢谢你们啦............
我是第一次在这里面发帖子,希望有好心的帮忙.............
再次感谢