C#对Socket操作的封装

None.gif using  System;
None.gif
using  System.Net ;
None.gif
using  System.Net.Sockets ;
None.gif
using  System.IO ;
None.gif
using  LogDll;
None.gif
None.gif
namespace  NetDll
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**//**//// <summary>
InBlock.gif    
/// Net : 提供静态方法,对常用的网络操作进行封装
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public sealed class Net
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
private Net()dot.gif{
ExpandedSubBlockEnd.gif      }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//**//**//// <summary>
InBlock.gif       
/// 连接使用 tcp 协议的服务端
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="ip">服务端的ip地址</param>
InBlock.gif       
/// <param name="port">服务端的端口号</param>
ExpandedSubBlockEnd.gif       
/// <returns></returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif      public static Socket ConnectServer( string ip ,int port ) dot.gif{
InBlock.gif            Socket s 
= null;
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                IPAddress ipAddress 
= IPAddress.Parse ( ip );
InBlock.gif                IPEndPoint ipEndPoint 
= new IPEndPoint ( ipAddress,port );
InBlock.gif                s 
= new Socket ( ipEndPoint.AddressFamily ,SocketType.Stream ,ProtocolType.Tcp );
InBlock.gif                s.Connect ( ipEndPoint );
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if ( s.Connected== false ) dot.gif{
InBlock.gif                    s 
= null;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch ( Exception e ) dot.gif{
InBlock.gif                Log.WriteLog ( e );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return s;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 用主机名称连接使用Tcp协议的服务端
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="hostName">在hosts 文件中存在的主机名称</param>
InBlock.gif        
/// <param name="port">服务端的端口号</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static Socket ConnectServByHostName( string hostName,int port )dot.gif{
InBlock.gif            Socket s 
= null;
InBlock.gif            IPHostEntry iphe 
= null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                iphe 
= Dns.Resolve ( hostName );            
ExpandedSubBlockStart.gifContractedSubBlock.gif                
foreach ( IPAddress ipad in iphe.AddressList ) dot.gif{
InBlock.gif                    IPEndPoint ipe 
= new IPEndPoint (ipad,port);
InBlock.gif                    Socket tmps 
= new Socket (ipe.AddressFamily ,SocketType.Stream ,ProtocolType.Tcp );
InBlock.gif                    tmps.Connect ( ipe );
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if ( tmps.Connected ) dot.gif{
InBlock.gif                        s 
= tmps;
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
InBlock.gif                        
continue;
ExpandedSubBlockEnd.gif                }
                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch ( Exception e ) dot.gif{
InBlock.gif                Log.WriteLog ( e );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return s;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 向远程主机发送数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="socket">要发送数据且已经连接到远程主机的 Socket</param>
InBlock.gif        
/// <param name="buffer">待发送的数据</param>
InBlock.gif        
/// <param name="outTime">发送数据的超时时间,以秒为单位,可以精确到微秒</param>
InBlock.gif        
/// <returns>0:发送数据成功;-1:超时;-2:发送数据出现错误;-3:发送数据时出现异常</returns>
InBlock.gif        
/// <remarks >
InBlock.gif        
/// 当 outTime 指定为-1时,将一直等待直到有数据需要发送
ExpandedSubBlockEnd.gif        
/// </remarks>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static int SendData ( Socket socket,byte[] buffer,int outTime ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( socket == null ||socket.Connected == false ) dot.gif{
InBlock.gif                
throw new ArgumentException ("参数socket 为null,或者未连接到远程计算机");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( buffer == null || buffer.Length == 0 ) dot.gif{
InBlock.gif                
throw new ArgumentException ("参数buffer 为null ,或者长度为 0");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
int flag = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                
int left = buffer.Length ;
InBlock.gif                
int sndLen  = 0;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
while ( true ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if ( ( socket.Poll (outTime*1000000,SelectMode.SelectWrite ) == true ) ) dot.gif{        // 收集了足够多的传出数据后开始发送
InBlock.gif
                        sndLen = socket.Send (buffer,sndLen ,left ,SocketFlags.None );
InBlock.gif                        left 
-= sndLen ;
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
if ( left == 0 ) dot.gif{                                        // 数据已经全部发送
InBlock.gif
                          flag = 0;
InBlock.gif                          
break;
ExpandedSubBlockEnd.gif                      }

ExpandedSubBlockStart.gifContractedSubBlock.gif                      
else dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                           
if ( sndLen > 0 ) dot.gif{                                    // 数据部分已经被发送
InBlock.gif
                                continue;
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockStart.gifContractedSubBlock.gif                            
else dot.gif{                                                // 发送数据发生错误
InBlock.gif
                                flag = -2;
InBlock.gif                                
break;
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }
                        
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockStart.gifContractedSubBlock.gif                    
else dot.gif{                                                        // 超时退出
InBlock.gif
                       flag = -1;
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch ( SocketException e ) dot.gif{
InBlock.gif                Log.WriteLog ( e );
InBlock.gif                flag 
= -3;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return flag;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 向远程主机发送数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="socket">要发送数据且已经连接到远程主机的 Socket</param>
InBlock.gif        
/// <param name="buffer">待发送的字符串</param>
InBlock.gif        
/// <param name="outTime">发送数据的超时时间,以秒为单位,可以精确到微秒</param>
InBlock.gif        
/// <returns>0:发送数据成功;-1:超时;-2:发送数据出现错误;-3:发送数据时出现异常</returns>
InBlock.gif        
/// <remarks >
InBlock.gif        
/// 当 outTime 指定为-1时,将一直等待直到有数据需要发送
ExpandedSubBlockEnd.gif        
/// </remarks>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static int SendData ( Socket socket,string buffer,int outTime ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( buffer == null || buffer.Length == 0 ) dot.gif{
InBlock.gif                
throw new ArgumentException ("待发送的字符串长度不能为零.");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return  ( SendData ( socket,System.Text .Encoding .Default .GetBytes ( buffer ),outTime) );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 接收远程主机发送的数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="socket">要接收数据且已经连接到远程主机的 socket</param>
InBlock.gif        
/// <param name="buffer">接收数据的缓冲区</param>
InBlock.gif        
/// <param name="outTime">接收数据的超时时间,以秒为单位,可以精确到微秒</param>
InBlock.gif       
/// <returns>0:接收数据成功;-1:超时;-2:接收数据出现错误;-3:接收数据时出现异常</returns>
InBlock.gif       
/// <remarks >
InBlock.gif       
/// 1、当 outTime 指定为-1时,将一直等待直到有数据需要接收;
InBlock.gif       
/// 2、需要接收的数据的长度,由 buffer 的长度决定。
ExpandedSubBlockEnd.gif       
/// </remarks>

ExpandedSubBlockStart.gifContractedSubBlock.gif       public static int RecvData ( Socket socket,byte[] buffer ,int outTime ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif           
if ( socket == null || socket.Connected == false ) dot.gif{
InBlock.gif               
throw new ArgumentException ("参数socket 为null,或者未连接到远程计算机");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( buffer == null || buffer.Length == 0 ) dot.gif{
InBlock.gif                
throw new ArgumentException ("参数buffer 为null ,或者长度为 0");
ExpandedSubBlockEnd.gif            }

InBlock.gif            buffer.Initialize ();
InBlock.gif            
int left = buffer.Length ;
InBlock.gif            
int curRcv = 0;
InBlock.gif            
int flag = 0;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
while ( true ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if ( socket.Poll (outTime*1000000,SelectMode.SelectRead ) == true ) dot.gif{        // 已经有数据等待接收
InBlock.gif
                        curRcv = socket.Receive (buffer,curRcv,left ,SocketFlags.None );
InBlock.gif                       left 
-= curRcv;
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
if ( left == 0 ) dot.gif{                                    // 数据已经全部接收 
InBlock.gif
                            flag = 0;
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockStart.gifContractedSubBlock.gif                        
else dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
if ( curRcv > 0 ) dot.gif{                                // 数据已经部分接收
InBlock.gif
                                continue;
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockStart.gifContractedSubBlock.gif                            
else dot.gif{                                            // 出现错误
InBlock.gif
                                flag = -2;
InBlock.gif                                
break;
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockStart.gifContractedSubBlock.gif                    
else dot.gif{                                                    // 超时退出
InBlock.gif
                        flag = -1;
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch ( SocketException e ) dot.gif{
InBlock.gif                Log.WriteLog ( e );
InBlock.gif                flag 
= -3;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return flag;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 接收远程主机发送的数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="socket">要接收数据且已经连接到远程主机的 socket</param>
InBlock.gif        
/// <param name="buffer">存储接收到的数据的字符串</param>
InBlock.gif        
/// <param name="bufferLen">待接收的数据的长度</param>
InBlock.gif        
/// <param name="outTime">接收数据的超时时间,以秒为单位,可以精确到微秒</param>
InBlock.gif        
/// <returns>0:接收数据成功;-1:超时;-2:接收数据出现错误;-3:接收数据时出现异常</returns>
InBlock.gif        
/// <remarks >
InBlock.gif        
/// 当 outTime 指定为-1时,将一直等待直到有数据需要接收;
ExpandedSubBlockEnd.gif        
/// </remarks>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static int RecvData ( Socket socket,string buffer ,int bufferLen,int outTime ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( bufferLen <= 0 ) dot.gif{
InBlock.gif                
throw new ArgumentException ("存储待接收数据的缓冲区长度必须大于0");
ExpandedSubBlockEnd.gif            }
None.gif using  System;
None.gif
using  System.Net ;
None.gif
using  System.Net.Sockets ;
None.gif
using  System.IO ;
None.gif
using  LogDll;
None.gif
None.gif
namespace  NetDll
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**//**//// <summary>
InBlock.gif    
/// Net : 提供静态方法,对常用的网络操作进行封装
ExpandedSubBlockEnd.gif    
/// </summary>
InBlock.gif    public sealed class Net
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
private Net()dot.gif{
ExpandedSubBlockEnd.gif      }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//**//**//// <summary>
InBlock.gif       
/// 连接使用 tcp 协议的服务端
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="ip">服务端的ip地址</param>
InBlock.gif       
/// <param name="port">服务端的端口号</param>
ExpandedSubBlockEnd.gif       
/// <returns></returns>
ExpandedSubBlockStart.gifContractedSubBlock.gif      public static Socket ConnectServer( string ip ,int port ) dot.gif{
InBlock.gif            Socket s 
= null;
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                IPAddress ipAddress 
= IPAddress.Parse ( ip );
InBlock.gif                IPEndPoint ipEndPoint 
= new IPEndPoint ( ipAddress,port );
InBlock.gif                s 
= new Socket ( ipEndPoint.AddressFamily ,SocketType.Stream ,ProtocolType.Tcp );
InBlock.gif                s.Connect ( ipEndPoint );
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if ( s.Connected== false ) dot.gif{
InBlock.gif                    s 
= null;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch ( Exception e ) dot.gif{
InBlock.gif                Log.WriteLog ( e );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return s;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 用主机名称连接使用Tcp协议的服务端
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="hostName">在hosts 文件中存在的主机名称</param>
InBlock.gif        
/// <param name="port">服务端的端口号</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>
ExpandedSubBlockStart.gifContractedSubBlock.gif        public static Socket ConnectServByHostName( string hostName,int port )dot.gif{
InBlock.gif            Socket s 
= null;
InBlock.gif            IPHostEntry iphe 
= null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                iphe 
= Dns.Resolve ( hostName );            
ExpandedSubBlockStart.gifContractedSubBlock.gif                
foreach ( IPAddress ipad in iphe.AddressList ) dot.gif{
InBlock.gif                    IPEndPoint ipe 
= new IPEndPoint (ipad,port);
InBlock.gif                    Socket tmps 
= new Socket (ipe.AddressFamily ,SocketType.Stream ,ProtocolType.Tcp );
InBlock.gif                    tmps.Connect ( ipe );
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if ( tmps.Connected ) dot.gif{
InBlock.gif                        s 
= tmps;
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
InBlock.gif                        
continue;
ExpandedSubBlockEnd.gif                }
                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch ( Exception e ) dot.gif{
InBlock.gif                Log.WriteLog ( e );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return s;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 向远程主机发送数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="socket">要发送数据且已经连接到远程主机的 Socket</param>
InBlock.gif        
/// <param name="buffer">待发送的数据</param>
InBlock.gif        
/// <param name="outTime">发送数据的超时时间,以秒为单位,可以精确到微秒</param>
InBlock.gif        
/// <returns>0:发送数据成功;-1:超时;-2:发送数据出现错误;-3:发送数据时出现异常</returns>
InBlock.gif        
/// <remarks >
InBlock.gif        
/// 当 outTime 指定为-1时,将一直等待直到有数据需要发送
ExpandedSubBlockEnd.gif        
/// </remarks>
ExpandedSubBlockStart.gifContractedSubBlock.gif        public static int SendData ( Socket socket,byte[] buffer,int outTime ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( socket == null ||socket.Connected == false ) dot.gif{
InBlock.gif                
throw new ArgumentException ("参数socket 为null,或者未连接到远程计算机");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( buffer == null || buffer.Length == 0 ) dot.gif{
InBlock.gif                
throw new ArgumentException ("参数buffer 为null ,或者长度为 0");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
int flag = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                
int left = buffer.Length ;
InBlock.gif                
int sndLen  = 0;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
while ( true ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if ( ( socket.Poll (outTime*1000000,SelectMode.SelectWrite ) == true ) ) dot.gif{        // 收集了足够多的传出数据后开始发送
InBlock.gif
                        sndLen = socket.Send (buffer,sndLen ,left ,SocketFlags.None );
InBlock.gif                        left 
-= sndLen ;
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
if ( left == 0 ) dot.gif{                                        // 数据已经全部发送
InBlock.gif
                          flag = 0;
InBlock.gif                          
break;
ExpandedSubBlockEnd.gif                      }

ExpandedSubBlockStart.gifContractedSubBlock.gif                      
else dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                           
if ( sndLen > 0 ) dot.gif{                                    // 数据部分已经被发送
InBlock.gif
                                continue;
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockStart.gifContractedSubBlock.gif                            
else dot.gif{                                                // 发送数据发生错误
InBlock.gif
                                flag = -2;
InBlock.gif                                
break;
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }
                        
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockStart.gifContractedSubBlock.gif                    
else dot.gif{                                                        // 超时退出
InBlock.gif
                       flag = -1;
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch ( SocketException e ) dot.gif{
InBlock.gif                Log.WriteLog ( e );
InBlock.gif                flag 
= -3;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return flag;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 向远程主机发送数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="socket">要发送数据且已经连接到远程主机的 Socket</param>
InBlock.gif        
/// <param name="buffer">待发送的字符串</param>
InBlock.gif        
/// <param name="outTime">发送数据的超时时间,以秒为单位,可以精确到微秒</param>
InBlock.gif        
/// <returns>0:发送数据成功;-1:超时;-2:发送数据出现错误;-3:发送数据时出现异常</returns>
InBlock.gif        
/// <remarks >
InBlock.gif        
/// 当 outTime 指定为-1时,将一直等待直到有数据需要发送
ExpandedSubBlockEnd.gif        
/// </remarks>
ExpandedSubBlockStart.gifContractedSubBlock.gif        public static int SendData ( Socket socket,string buffer,int outTime ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( buffer == null || buffer.Length == 0 ) dot.gif{
InBlock.gif                
throw new ArgumentException ("待发送的字符串长度不能为零.");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return  ( SendData ( socket,System.Text .Encoding .Default .GetBytes ( buffer ),outTime) );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 接收远程主机发送的数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="socket">要接收数据且已经连接到远程主机的 socket</param>
InBlock.gif        
/// <param name="buffer">接收数据的缓冲区</param>
InBlock.gif        
/// <param name="outTime">接收数据的超时时间,以秒为单位,可以精确到微秒</param>
InBlock.gif       
/// <returns>0:接收数据成功;-1:超时;-2:接收数据出现错误;-3:接收数据时出现异常</returns>
InBlock.gif       
/// <remarks >
InBlock.gif       
/// 1、当 outTime 指定为-1时,将一直等待直到有数据需要接收;
InBlock.gif       
/// 2、需要接收的数据的长度,由 buffer 的长度决定。
ExpandedSubBlockEnd.gif       
/// </remarks>
ExpandedSubBlockStart.gifContractedSubBlock.gif       public static int RecvData ( Socket socket,byte[] buffer ,int outTime ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif           
if ( socket == null || socket.Connected == false ) dot.gif{
InBlock.gif               
throw new ArgumentException ("参数socket 为null,或者未连接到远程计算机");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( buffer == null || buffer.Length == 0 ) dot.gif{
InBlock.gif                
throw new ArgumentException ("参数buffer 为null ,或者长度为 0");
ExpandedSubBlockEnd.gif            }

InBlock.gif            buffer.Initialize ();
InBlock.gif            
int left = buffer.Length ;
InBlock.gif            
int curRcv = 0;
InBlock.gif            
int flag = 0;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
while ( true ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if ( socket.Poll (outTime*1000000,SelectMode.SelectRead ) == true ) dot.gif{        // 已经有数据等待接收
InBlock.gif
                        curRcv = socket.Receive (buffer,curRcv,left ,SocketFlags.None );
InBlock.gif                       left 
-= curRcv;
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
if ( left == 0 ) dot.gif{                                    // 数据已经全部接收 
InBlock.gif
                            flag = 0;
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockStart.gifContractedSubBlock.gif                        
else dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
if ( curRcv > 0 ) dot.gif{                                // 数据已经部分接收
InBlock.gif
                                continue;
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockStart.gifContractedSubBlock.gif                            
else dot.gif{                                            // 出现错误
InBlock.gif
                                flag = -2;
InBlock.gif                                
break;
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockStart.gifContractedSubBlock.gif                    
else dot.gif{                                                    // 超时退出
InBlock.gif
                        flag = -1;
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch ( SocketException e ) dot.gif{
InBlock.gif                Log.WriteLog ( e );
InBlock.gif                flag 
= -3;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return flag;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 接收远程主机发送的数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="socket">要接收数据且已经连接到远程主机的 socket</param>
InBlock.gif        
/// <param name="buffer">存储接收到的数据的字符串</param>
InBlock.gif        
/// <param name="bufferLen">待接收的数据的长度</param>
InBlock.gif        
/// <param name="outTime">接收数据的超时时间,以秒为单位,可以精确到微秒</param>
InBlock.gif        
/// <returns>0:接收数据成功;-1:超时;-2:接收数据出现错误;-3:接收数据时出现异常</returns>
InBlock.gif        
/// <remarks >
InBlock.gif        
/// 当 outTime 指定为-1时,将一直等待直到有数据需要接收;
ExpandedSubBlockEnd.gif        
/// </remarks>
ExpandedSubBlockStart.gifContractedSubBlock.gif        public static int RecvData ( Socket socket,string buffer ,int bufferLen,int outTime ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( bufferLen <= 0 ) dot.gif{
InBlock.gif                
throw new ArgumentException ("存储待接收数据的缓冲区长度必须大于0");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
byte[] tmp = new byte [ bufferLen ];
InBlock.gif            
int flag = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( ( flag = RecvData ( socket,tmp,outTime)) == 0dot.gif{
InBlock.gif                buffer 
= System.Text.Encoding .Default .GetString ( tmp );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return flag;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 向远程主机发送文件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="socket" >要发送数据且已经连接到远程主机的 socket</param>
InBlock.gif        
/// <param name="fileName">待发送的文件名称</param>
InBlock.gif        
/// <param name="maxBufferLength">文件发送时的缓冲区大小</param>
InBlock.gif        
/// <param name="outTime">发送缓冲区中的数据的超时时间</param>
InBlock.gif        
/// <returns>0:发送文件成功;-1:超时;-2:发送文件出现错误;-3:发送文件出现异常;-4:读取待发送文件发生错误</returns>
InBlock.gif      
/// <remarks >
InBlock.gif      
/// 当 outTime 指定为-1时,将一直等待直到有数据需要发送
ExpandedSubBlockEnd.gif      
/// </remarks>

ExpandedSubBlockStart.gifContractedSubBlock.gif      public static int SendFile ( Socket socket ,string fileName,int maxBufferLength,int outTime ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif           
if ( fileName == null || maxBufferLength <= 0 ) dot.gif{
InBlock.gif               
throw new ArgumentException ("待发送的文件名称为空或发送缓冲区的大小设置不正确.");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
int flag = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                FileStream fs 
= new FileStream ( fileName,FileMode.Open ,FileAccess.Read );
InBlock.gif                
long fileLen = fs.Length ;                        // 文件长度
InBlock.gif
                long leftLen = fileLen;                            // 未读取部分
InBlock.gif
                int readLen = 0;                                // 已读取部分
InBlock.gif
                byte[] buffer = null;
InBlock.gif                
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if ( fileLen <= maxBufferLength ) dot.gif{            /**//**//**//* 文件可以一次读取*/
InBlock.gif                   buffer 
= new byte [ fileLen ];
InBlock.gif                   readLen 
= fs.Read (buffer,0,(int )fileLen );
InBlock.gif                   flag 
= SendData( socket,buffer,outTime );
ExpandedSubBlockEnd.gif               }

ExpandedSubBlockStart.gifContractedSubBlock.gif               
else dot.gif{                                    /**//**//**//* 循环读取文件,并发送 */                    
InBlock.gif                    buffer 
= new byte[ maxBufferLength ];
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
while ( leftLen != 0 ) dot.gif{                    
InBlock.gif                        readLen 
= fs.Read (buffer,0,maxBufferLength );
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
if ( (flag = SendData( socket,buffer,outTime ) ) < 0 ) dot.gif{
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        leftLen 
-= readLen;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                fs.Close ();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch ( IOException e ) dot.gif{
InBlock.gif                Log.WriteLog ( e );
InBlock.gif                flag 
= -4;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return flag;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 向远程主机发送文件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="socket" >要发送数据且已经连接到远程主机的 socket</param>
InBlock.gif        
/// <param name="fileName">待发送的文件名称</param>
ExpandedSubBlockEnd.gif        
/// <returns>0:发送文件成功;-1:超时;-2:发送文件出现错误;-3:发送文件出现异常;-4:读取待发送文件发生错误</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static int SendFile ( Socket socket ,string fileName ) dot.gif{
InBlock.gif            
return SendFile ( socket,fileName,2048,1 );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 接收远程主机发送的文件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="socket">待接收数据且已经连接到远程主机的 socket</param>
InBlock.gif        
/// <param name="fileName">保存接收到的数据的文件名</param>
InBlock.gif        
/// <param name="fileLength" >待接收的文件的长度</param>
InBlock.gif        
/// <param name="maxBufferLength">接收文件时最大的缓冲区大小</param>
InBlock.gif        
/// <param name="outTime">接受缓冲区数据的超时时间</param>
InBlock.gif        
/// <returns>0:接收文件成功;-1:超时;-2:接收文件出现错误;-3:接收文件出现异常;-4:写入接收文件发生错误</returns>
InBlock.gif        
/// <remarks >
InBlock.gif        
/// 当 outTime 指定为-1时,将一直等待直到有数据需要接收
ExpandedSubBlockEnd.gif        
/// </remarks>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static int RecvFile ( Socket socket ,string fileName,long fileLength,int maxBufferLength,int outTime ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif           
if ( fileName == null || maxBufferLength <= 0 ) dot.gif{
InBlock.gif               
throw new ArgumentException ("保存接收数据的文件名称为空或发送缓冲区的大小设置不正确.");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
int flag = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                FileStream fs 
= new FileStream (fileName,FileMode.Create);
InBlock.gif                
byte []  buffer = null;
InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if ( fileLength <= maxBufferLength ) dot.gif{                /**//**//**//* 一次读取所传送的文件 */
InBlock.gif                    buffer 
= new byte [ fileLength ];
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if ( ( flag =RecvData(socket,buffer,outTime ) ) == 0 ) dot.gif{
InBlock.gif                        fs.Write ( buffer,
0,(int)fileLength);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
else dot.gif{                                        /**//**//**//* 循环读取网络数据,并写入文件 */
InBlock.gif                    
int rcvLen = maxBufferLength;
InBlock.gif                    
long leftLen = fileLength;                        //剩下未写入的数据
InBlock.gif
                    buffer = new byte [ rcvLen ];
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
while (  leftLen != 0 ) dot.gif{                        
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
if ( ( flag =RecvData(socket,buffer,outTime ) ) < 0 ) dot.gif{
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        fs.Write (buffer,
0,rcvLen );
InBlock.gif                        leftLen 
-= rcvLen;                        
InBlock.gif                        rcvLen  
= ( maxBufferLength < leftLen ) ? maxBufferLength :((int) leftLen) ;
ExpandedSubBlockEnd.gif                   }

ExpandedSubBlockEnd.gif               }

InBlock.gif               fs.Close ();
ExpandedSubBlockEnd.gif           }

ExpandedSubBlockStart.gifContractedSubBlock.gif           
catch ( IOException e ) dot.gif{
InBlock.gif               Log.WriteLog ( e );
InBlock.gif               flag 
= -4;
ExpandedSubBlockEnd.gif           }

InBlock.gif           
return flag;
ExpandedSubBlockEnd.gif       }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//**//**//// <summary>
InBlock.gif       
/// 接收远程主机发送的文件
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="socket">待接收数据且已经连接到远程主机的 socket</param>
InBlock.gif       
/// <param name="fileName">保存接收到的数据的文件名</param>
InBlock.gif       
/// <param name="fileLength" >待接收的文件的长度</param>
ExpandedSubBlockEnd.gif       
/// <returns>0:接收文件成功;-1:超时;-2:接收文件出现错误;-3:接收文件出现异常;-4:写入接收文件发生错误</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static int RecvFile ( Socket socket,string fileName,long fileLength ) dot.gif{
InBlock.gif            
return RecvFile ( socket,fileName,fileLength,2048,1);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/shengshuai/archive/2006/10/14/Socket.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值