public static bool WriteDynamicTextToSocket(Socket s,string strText,OnSend OnSendText)
{
byte [] buf = Encoding.UTF8.GetBytes(strText) ;
int iLen = buf.Length ;
try
{
WriteDynamicLenToSocket(s,iLen) ;
return WriteBufToSocket(s,buf,0,iLen,DEALLEN,OnSendText) ;
}
catch(Exception err)
{
MessageBox.Show("发送文本失败!"+err.Message) ;
return false ;
}
}
public static bool WriteBufToSocket(Socket s,byte [] buf,int iStart,int iCount,int iBlock,OnSend SendSuccess)
{ int iSended = 0 ;
int iSending = 0 ;
while(iSended<iCount)
{
if (iSended + iBlock <= iCount)
iSending = iBlock ;
else
iSending = iCount - iSended ;
s.Send(buf,iStart+iSended,iSending,SocketFlags.None) ;
iSended += iSending ;
if (ReadResponsionFromSocket(s)=="OK")
if (SendSuccess != null)
SendSuccess(iCount,iSended) ;
else
return false;
} return true ; }
这是一个别人写好的类
我直接调用
baseClass.CommunClass.WriteFileToSocket(socket,SendFileName,得到文本长度的回调函数)这个不会写了WriteFileToSocket是一个异步传送的方法,里面要用到回调函数,有谁以前也写的,请帮我看下,我在线的,马上结贴的}//end namespace
{
byte [] buf = Encoding.UTF8.GetBytes(strText) ;
int iLen = buf.Length ;
try
{
WriteDynamicLenToSocket(s,iLen) ;
return WriteBufToSocket(s,buf,0,iLen,DEALLEN,OnSendText) ;
}
catch(Exception err)
{
MessageBox.Show("发送文本失败!"+err.Message) ;
return false ;
}
}
public static bool WriteBufToSocket(Socket s,byte [] buf,int iStart,int iCount,int iBlock,OnSend SendSuccess)
{ int iSended = 0 ;
int iSending = 0 ;
while(iSended<iCount)
{
if (iSended + iBlock <= iCount)
iSending = iBlock ;
else
iSending = iCount - iSended ;
s.Send(buf,iStart+iSended,iSending,SocketFlags.None) ;
iSended += iSending ;
if (ReadResponsionFromSocket(s)=="OK")
if (SendSuccess != null)
SendSuccess(iCount,iSended) ;
else
return false;
} return true ; }
这是一个别人写好的类
我直接调用
baseClass.CommunClass.WriteFileToSocket(socket,SendFileName,得到文本长度的回调函数)这个不会写了WriteFileToSocket是一个异步传送的方法,里面要用到回调函数,有谁以前也写的,请帮我看下,我在线的,马上结贴的}//end namespace
/// <summary> /// 将文件写到Socket /// </summary> /// <param name="s">要发送文件的Socket</param> /// <param name="strFile">要发送的文件</param> /// <returns>是否成功</returns> public static bool WriteFileToSocket(Socket s,string strFile,OnSend OnSendFile) {
FileStream fs = new FileStream(strFile,FileMode.Open,FileAccess.Read,FileShare.Read) ; int iLen = (int)fs.Length ; WriteDynamicLenToSocket(s,iLen) ; byte [] buf = new byte[iLen] ; try { fs.Read(buf,0,iLen) ; return WriteBufToSocket(s,buf,0,iLen,DEALLEN,OnSendFile) ; } catch(Exception err) { MessageBox.Show("发送文件失败!"+err.Message) ; return false ; } finally { fs.Close() ; } }
/// <summary>
/// 将文件写到Socket
/// </summary>
/// <param name="s">要发送文件的Socket</param>
/// <param name="strFile">要发送的文件</param>
/// <returns>是否成功</returns>
public static bool WriteFileToSocket(Socket s,string strFile,OnSend OnSendFile)
{
FileStream fs = new FileStream(strFile,FileMode.Open,FileAccess.Read,FileShare.Read) ; int iLen = (int)fs.Length ; WriteDynamicLenToSocket(s,iLen) ;
byte [] buf = new byte[iLen] ;
try
{ fs.Read(buf,0,iLen) ;
return WriteBufToSocket(s,buf,0,iLen,DEALLEN,OnSendFile) ;
} catch(Exception err)
{
MessageBox.Show("发送文件失败!"+err.Message) ;
return false ;
} finally
{ fs.Close() ;
}
}
你把回调函数写好,让其他程序或windows call