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
解决方案 »
- 求模拟鼠标单击的WinAPI
- 关于string转成keys
- 正则表达式循环准确匹配
- 找不到存储过程?
- 下面double-buffer in gdi+程序中,是如何控制刷新的速度的?
- 两个字符串的重新组合,谢谢!
- asp.net 剔除出了换行以外的其他html标签
- web窗体自动关闭时如何不出现"是否关闭该窗口"的提示
- 请教关于WinForm里面DataGrid行列的操作!
- 如何改变DataGrid控件中CheckBox列按钮的三态为两态?多谢了!
- 使用.net写的Windows服务调用组件对话框folderBrowserDialog时,界面无法正常显示。
- 100分求助:大家周末好,领导让我做地铁杂散电流监控系统,可是我没思路啊!
/// <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