异步无法两次连接
/// <summary>
/// 开始侦听
/// </summary>
/// <param name="pDestFold"></param>
/// <param name="pTempFileFold"></param>
public void StartListen(string pDestFold,string pTempFileFold)
{
this.destFold = pDestFold;
this.tempFold = pTempFileFold;
this.ser = new Socket(this.localAddr.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(this.localAddr,this.port);
this.ser.Bind(ep);
this.ser.Listen(5);
this.ser.BeginAccept(new AsyncCallback(this.callAccept),this.ser);
// System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(this.list));
// th.Start();
}
private void callAccept(IAsyncResult iar)
{
Socket serverMain = (Socket)iar.AsyncState;
Socket server = serverMain.EndAccept(iar);
// NetworkStream stream = new NetworkStream(server);
byte[] len = new byte[8];
try
{
//先读取8个字节的长度定义
server.Receive(len,0,8,SocketFlags.None);
int iLen = BitConverter.ToInt32(len,0);
//读取命令
byte [] dataContent = new byte[iLen];
server.Receive(dataContent,0,dataContent.Length,SocketFlags.None);
//收到的命令
string data = System.Text.Encoding.UTF8.GetString(dataContent);
Log.AddEvent("Listen",data);
//分析命令
string[] cmd = data.Split('|');
OneSync sync = new OneSync(cmd[0],cmd[1]);
//目标文件
string dest = string.Format("{0}\\{1}",this.destFold,sync.FileName);
//执行操作
//没有改名,因为考虑目标盘可能没有指定文件,于是将操作化为先在删除目标盘上的原文件,再拷贝源的新文件。
switch(sync.Method)
{
case Methods.Delete:
System.IO.File.Delete(dest);
break;
case Methods.Copy:
//读取文件长度
server.Receive(len,0,8,SocketFlags.None);
iLen = BitConverter.ToInt32(len,0);
//读取文件内容
string temp = string.Format("{0}\\{1}.tmp",this.tempFold,sync.FileName.Replace("\\","_"));
System.IO.FileStream file = System.IO.File.OpenWrite(temp);
byte [] buffer = new byte[1024];
int iRead = iLen;
int iLeft = iLen;
while(iLeft>0)
{
if(iLeft>1024)
iRead = server.Receive(buffer,0,1024,SocketFlags.None);
else
iRead = server.Receive(buffer,0,iLeft,SocketFlags.None);
iLeft = iLeft - iRead;
//写入文件
file.Write(buffer,0,iRead);
}
//关闭文件
file.Close();
//检查目标目录是否存在
System.IO.FileInfo info = new System.IO.FileInfo(dest);
if(!info.Directory.Exists)
info.Directory.Create();//创建目标目录
//改名
System.IO.File.Copy(temp,dest,true);
System.IO.File.Delete(temp);
// System.IO.File.Move(temp,dest);//目标文件存在时出
break;
}
//回送收到的命令
Byte[] msg = System.Text.Encoding.ASCII.GetBytes("OK");
server.Send(msg, 0, msg.Length,SocketFlags.None);
server.Close();
}
catch(Exception e)
{
Log.AddErr("TcpServer", e);
}
}
/// 开始侦听
/// </summary>
/// <param name="pDestFold"></param>
/// <param name="pTempFileFold"></param>
public void StartListen(string pDestFold,string pTempFileFold)
{
this.destFold = pDestFold;
this.tempFold = pTempFileFold;
this.ser = new Socket(this.localAddr.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(this.localAddr,this.port);
this.ser.Bind(ep);
this.ser.Listen(5);
this.ser.BeginAccept(new AsyncCallback(this.callAccept),this.ser);
// System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(this.list));
// th.Start();
}
private void callAccept(IAsyncResult iar)
{
Socket serverMain = (Socket)iar.AsyncState;
Socket server = serverMain.EndAccept(iar);
// NetworkStream stream = new NetworkStream(server);
byte[] len = new byte[8];
try
{
//先读取8个字节的长度定义
server.Receive(len,0,8,SocketFlags.None);
int iLen = BitConverter.ToInt32(len,0);
//读取命令
byte [] dataContent = new byte[iLen];
server.Receive(dataContent,0,dataContent.Length,SocketFlags.None);
//收到的命令
string data = System.Text.Encoding.UTF8.GetString(dataContent);
Log.AddEvent("Listen",data);
//分析命令
string[] cmd = data.Split('|');
OneSync sync = new OneSync(cmd[0],cmd[1]);
//目标文件
string dest = string.Format("{0}\\{1}",this.destFold,sync.FileName);
//执行操作
//没有改名,因为考虑目标盘可能没有指定文件,于是将操作化为先在删除目标盘上的原文件,再拷贝源的新文件。
switch(sync.Method)
{
case Methods.Delete:
System.IO.File.Delete(dest);
break;
case Methods.Copy:
//读取文件长度
server.Receive(len,0,8,SocketFlags.None);
iLen = BitConverter.ToInt32(len,0);
//读取文件内容
string temp = string.Format("{0}\\{1}.tmp",this.tempFold,sync.FileName.Replace("\\","_"));
System.IO.FileStream file = System.IO.File.OpenWrite(temp);
byte [] buffer = new byte[1024];
int iRead = iLen;
int iLeft = iLen;
while(iLeft>0)
{
if(iLeft>1024)
iRead = server.Receive(buffer,0,1024,SocketFlags.None);
else
iRead = server.Receive(buffer,0,iLeft,SocketFlags.None);
iLeft = iLeft - iRead;
//写入文件
file.Write(buffer,0,iRead);
}
//关闭文件
file.Close();
//检查目标目录是否存在
System.IO.FileInfo info = new System.IO.FileInfo(dest);
if(!info.Directory.Exists)
info.Directory.Create();//创建目标目录
//改名
System.IO.File.Copy(temp,dest,true);
System.IO.File.Delete(temp);
// System.IO.File.Move(temp,dest);//目标文件存在时出
break;
}
//回送收到的命令
Byte[] msg = System.Text.Encoding.ASCII.GetBytes("OK");
server.Send(msg, 0, msg.Length,SocketFlags.None);
server.Close();
}
catch(Exception e)
{
Log.AddErr("TcpServer", e);
}
}