哈哈,我也来发一段我写的tcpsocket的源代码吧---临时之作,大家指正!
zxf52 2010-01-10 09:48:10 unit GDKWithNetSocket;
interface
uses
Classes,SysUtils,Windows,WinSock;
const
SOCKET_LENGTH_READ = 1024*8;
SOCKET_LENGTH_WRITE = 1024*8;
IOC_VENDOR = $18000000;
SIO_KEEPALIVE_VALS = IOC_IN or IOC_VENDOR or 4;
type
WSAIoctl = function (s: TSocket; dwIoControlCode: DWORD; lpvInBuffer: Pointer;
cbInBuffer: DWORD; lpvOutBuffer: Pointer; cbOutBuffer: DWORD;
lpcbBytesReturned: Cardinal; lpOverlapped: Pointer;
lpCompletionRoutine: pointer): u_int; stdcall;
TTcpKeepAlive = packed record
OnOff : u_long;
KeepAliveTime : u_long;
KeepAliveInterval : u_long;
end;
////////////////////////////////////////////////////////////////////////////////
TGDKSocket = class(TObject)
private
m_Socket : TSocket;
m_TimeStamp_Read : Cardinal;
m_TimeStamp_Write : Cardinal;
public
procedure UpdateTimeStamp_Read; //自动更新时间戳;
procedure UpdateTimeStamp_Write;
function GetTimeStamp_Read : Cardinal; //获得时间戳;
function GetTimeStamp_Write : Cardinal;
public
constructor Create;
destructor Destroy; override;
published
property SocketHandle : TSocket read m_Socket;
public //以下定义主要的接口方法!
procedure Close; //关闭;
function IsValid:Boolean; //是否是合法有效的Socket?
procedure Assign(ASocket:TSocket);
function SetNonBlocking(enabled:Boolean):Boolean; //设置非堵塞模式;
function IsNonBlocking:Boolean; //查询是否处于堵塞模式
function SetReuseAddr:Boolean; //使socket可以捆绑(bind())到一个已经使用的本地地址
function SetKeepAlive:Boolean; //启用/禁用保持活动状态的套接字 TCP连接非正常断开的检测(KeepAlive探测)
function InAddrToIP(addr : TInAddr ):String;
//以下是优化Socket部分,慎用!!!!
function SetSendBufferSize (iSize:Integer):Boolean;
function SetReceiveBufferSize(iSize:Integer):Boolean;
function GetSendBufferSize : Integer;
function GetReceiveBufferSize : Integer;
end;
////////////////////////////////////////////////////////////////////////////////
TGDKTCPSocket = class(TGDKSocket)
private
m_Buffer_Read : PCHAR;
protected
function _SetOption:Boolean;
public
constructor Create ; overload;
destructor Destroy; override;
public
//下面是核心操作;
function ReadBuffer (var buffer : PChar ; len:Integer;timeout:Cardinal):Integer;
function Read (var str : string ; len:Integer;timeout:Cardinal):Integer;
function ReadLn (var str : String ; timeout:Cardinal):Integer;
function WriteBuffer(Buffer : PChar ; len:Integer;timeout:Cardinal):Integer;
function Write (str : string ; len:Integer;timeout:Cardinal):Integer;
function WriteLn (str : String ; timeout:Cardinal):Integer;
end;
////////////////////////////////////////////////////////////////////////////////
TGDKTCPClientSocket = class(TGDKTCPSocket)
public
constructor Create ; overload; //自动创建Socket!!!
destructor Destroy; override;
public
function IsConnected:Boolean; //是否已经连接?
function Connect(host:String; port:Integer;ATimeOut:Integer=3):Boolean; //核心操作!!!!
private
m_IsConnected : Boolean;
end;
////////////////////////////////////////////////////////////////////////////////
TGDKTCPServerSocket = class(TGDKTCPSocket)
public
//核心操作!!!!
function Bind(port:Integer):Boolean;
function Listen(backlog:Integer):Boolean;
function Accept(newsocket:TSocket;newip:String):Boolean;
end;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
function GDK_WSAStartup : Boolean;
var
wsd : WSADATA;
ret : Integer;
begin
ret := WinSock.WSAStartup(MAKEWORD(2, 2), wsd);
Result := ret<>SOCKET_ERROR;
end;
function GDK_WSACleanup : Boolean;
begin
WinSock.WSACleanup;
Result := true;
end;
////////////////////////////////////////////////////////////////////////////////