150分求购网络程序代码(100分)

  • 主题发起人 主题发起人 liuxiangyang
  • 开始时间 开始时间
L

liuxiangyang

Unregistered / Unconfirmed
GUEST, unregistred user!
我是一名delphi新手,现在很想学习delphi的网络程序开发方法.水平有限,不知从哪里着手,不知道该看些什么资料,想找个示例看看,各位大侠能否帮忙,小弟现在这里说声谢谢啦!!!

在线等!!!!
 
首先看书:
《WINDOWS网络编程技术》。
然后看一些代码。
就可以了!
还要详细就看《TCP/IP协议 卷一》
 
1、《indy in delphi〉
2、《delphi组件大全》
3、到delphi盒子去找找相关的代码
 
//这是我以前发的贴,参考一下吧!
unit uIOCompletPort;

interface
uses
WinSock2, Classes, windows, inifiles, Forms, SysUtils;
const
MAX_BUFSIZE = 4096; {最大缓冲}
MAX_QUEUESIZE = 1000; {最大监听队列}
RECV_POSTED = 0; {接受}
SEND_POSTED = 1; {发送}
SHUTDOWN_FLAG = $FFFFFFFF; {线程退出标志}
type
{服务端口}
TSocketPort = Record
GpsPort:Integer;
BusPort:Integer;
End;
{单句柄数据 和 单IO数据}
PPerHandledata = ^TPerHandledata;
TPerHandledata = record
Overlapped: OVERLAPPED;
ClientSocket: TSocket; {接入客户端Socket}
WSABuffer: WSABUF;
DataBuf: array[0..MAX_BUFSIZE - 1] of char;
OperType: Integer; {IO操作类型}
SmsCardNo: String; {车载序列号}
AccTiem: Cardinal; {开始接收时间}
IsUse: Boolean; {是否使用}
end;
TReadSocketEvent = procedure(Data: Pointer; Count: Integer; Socket: TSocket) of object;
TIOCPPORT = class(TObject)
private
procedure GetSocketPort;
public
constructor Create;
destructor Destroy; override;
function StartServer: Integer;
end;
var
g_StopThread: Boolean; {停止接收线程}
g_SocketPort: TSocketPort; {端口记录}
g_Lock: TRTLCriticalSection;
g_phioList: TList; {单句柄和单IO队列}
g_hIocp: THandle; {完成端口句柄}
g_Listen: TSocket; {监听Socket}
g_internetAddr: TSockAddr;
ReadSocketEvent: TReadSocketEvent;
procedure Lock;
procedure UnLock;
function AlloPerHandledata: PPerHandledata;
procedure AcceptThread(key: Pointer); stdcall; {接收线程}
procedure ServerWorkThread(key: Pointer); stdcall;{工作线程}
implementation

procedure Lock;
begin
EnterCriticalSection(g_Lock);
end;
procedure UnLock;
begin
LeaveCriticalSection(g_Lock);
end;

function AlloPerHandledata: PPerHandledata;
var
I: Integer;
PerHandle: PPerHandledata;
begin
PerHandle := nil;
for I := g_phioList.Count - 1 downto 0 do
begin
PerHandle := g_phioList;
if not PerHandle.IsUse then Break;
end;
if not Assigned(PerHandle) or (PerHandle.IsUse) then
begin
New(PerHandle);
ZeroMemory(PerHandle, sizeof(TPerHandledata));
PerHandle.ClientSocket := INVALID_SOCKET;
g_phioList.Add(PerHandle);
end;
Result := PerHandle;
end;

{接收线程}
procedure AcceptThread(key: Pointer); stdcall;
var
PerHandle: PPerHandledata; {单句柄数据}
ClientAddr: TSockAddr;
ClientAddrSize: Integer;
RecvBytes: Cardinal;
Flags: Cardinal;
AcceptSocket: TSOCKET;
ErrCode: Integer;
begin
while true do
begin
if g_StopThread then Break; {通知结束}
ClientAddrSize := sizeof(TSockAddr);
AcceptSocket := WinSock2.WSAAccept(g_Listen, @ClientAddr, @ClientAddrSize, nil, 0);
if AcceptSocket = INVALID_SOCKET then Continue;
PerHandle := AlloPerHandledata;
PerHandle.ClientSocket := AcceptSocket;
PerHandle.AccTiem := GetTickCount;
{将套接字跟完成端口关联}
if CreateIoCompletionPort(AcceptSocket, g_hIocp, DWORD(PerHandle), 0) = 0 then
begin
if Assigned(PerHandle) then
begin
PerHandle.ClientSocket := INVALID_SOCKET;
PerHandle.IsUse := False;
end;
Continue;
end;
{投递第一次接收}
Flags := 0;
ZeroMemory(@PerHandle^.Overlapped, sizeof(OVERLAPPED));
ZeroMemory(@PerHandle^.DataBuf, sizeof(PerHandle^.DataBuf));
PerHandle^.WSABuffer.len := MAX_BUFSIZE;
PerHandle^.WSABuffer.buf := PerHandle^.DataBuf;
PerHandle^.OperType := RECV_POSTED;
PerHandle.IsUse := True;
if WSARecv(PerHandle^.ClientSocket, @PerHandle^.WSABuffer, 1, RecvBytes,
Flags, @PerHandle^.Overlapped, nil) = SOCKET_ERROR then
begin
ErrCode := WSAGetLastError;
if ErrCode <> ERROR_IO_PENDING then
begin
closesocket(PerHandle^.ClientSocket);
PerHandle^.ClientSocket := INVALID_SOCKET;
PerHandle^.IsUse := False;
end;
end;
end; //while
end;

{工作线程}
procedure ServerWorkThread(key: Pointer); stdcall;
var
PerHandle: PPerHandledata;
PerIoData: PPerHandledata;
BytesTransferred: DWORD; {存放完成一次I/O操作后,接受实际传入的字节数}
RecvBytes, Flags: Cardinal;
ErrCode, I: Integer;
begin
while true do
begin
PerHandle := nil;
PerIoData := nil;
BytesTransferred := 0;
if not GetQueuedCompletionStatus(g_hIocp, BytesTransferred,
DWORD(PerHandle), POverLapped(PerIoData), INFINITE) then
begin
if Assigned(PerHandle) then
begin
closesocket(PerHandle^.ClientSocket);
PerHandle.ClientSocket := INVALID_SOCKET;
PerHandle.IsUse := False;
end;
Continue;
end;
if BytesTransferred = 0 then
begin
if Assigned(PerHandle) then
begin
closesocket(PerHandle^.ClientSocket);
PerHandle.ClientSocket := INVALID_SOCKET;
PerHandle.IsUse := False;
end;
Continue;
end;
if Cardinal(PerIoData) = SHUTDOWN_FLAG then Break; {通知结束}
PerHandle^.AccTiem := GetTickCount; {接收数据}
if PerIoData.OperType = RECV_POSTED then
ReadSocketEvent(@PerIoData.DataBuf, BytesTransferred, PerHandle^.ClientSocket);
{继续投递接收}
Flags := 0;
RecvBytes := 0;
ZeroMemory(@PerHandle^.Overlapped, sizeof(OVERLAPPED));
ZeroMemory(@PerHandle^.DataBuf, sizeof(PerHandle^.DataBuf));
PerHandle^.WSABuffer.len := MAX_BUFSIZE;
PerHandle^.WSABuffer.buf := PerHandle^.DataBuf;
PerHandle^.OperType := RECV_POSTED;
PerHandle^.IsUse := True;
if WSARecv(PerHandle^.ClientSocket, @PerHandle^.WSABuffer, 1, RecvBytes, Flags,
@PerHandle^.Overlapped, nil) = SOCKET_ERROR then
begin
ErrCode := WSAGetLastError;
if ErrCode <> ERROR_IO_PENDING then
begin
closesocket(PerHandle^.ClientSocket);
PerHandle^.ClientSocket := INVALID_SOCKET;
PerHandle^.IsUse := False;
end;
end;
end; //while
end;

{ TIOCPPORT }

constructor TIOCPPORT.Create;
begin
inherited Create;
g_phioList := TList.Create;
g_StopThread := False;
end;


destructor TIOCPPORT.Destroy;
var
I: Integer;
PerHandle: PPerHandledata;
begin
closesocket(g_Listen);
for I := g_phioList.Count - 1 downto 0 do
begin
PerHandle := g_phioList;
if PerHandle^.ClientSocket <> INVALID_SOCKET then
begin
closesocket(PerHandle^.ClientSocket);
PerHandle^.ClientSocket := INVALID_SOCKET;
PerHandle^.IsUse := False;
end;
Dispose(PerHandle);
end;
g_phioList.Clear;
g_phioList.Free;
PostQueuedCompletionStatus(g_hIocp, 0, 0, Pointer(SHUTDOWN_FLAG));
g_StopThread := True;
Sleep(20);
CloseHandle(g_hIocp);
WSACleanup;
inherited;
end;

procedure TIOCPPORT.GetSocketPort;
var
InifPath: String;
inifile: TIniFile;
begin
InifPath := ExtractFilePath(Application.ExeName);
inifile := TIniFile.Create(InifPath + 'SysConfig.ini');
try
g_SocketPort.GpsPort := inifile.ReadInteger('SOCKET SET', 'GpsPort', 5150);
g_SocketPort.BusPort := inifile.ReadInteger('SOCKET SET', 'BusPort', 5160);
finally
inifile.Free;
end;
end;

function TIOCPPORT.StartServer: Integer;
var
I: Integer;
WSADATA: TWSAData ;
sysInfo: SYSTEM_INFO;
Thrhandle: THandle;
ThreadID: Cardinal;
begin
{初始WinSocket2}
if WSAStartup(MakeWord(2, 2), WSADATA) <> 0 then
begin
Result := 1; {初始WinSocket2失败}
Exit;
end;
{初始完成端口句柄}
g_hIocp := CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 2);
if g_hIocp = 0 then
begin
Result := 2; {初始完成端口句柄错误}
WSACleanup;
Exit;
end;
{获取系统CPU数量}
GetSystemInfo(sysInfo);
{创建工作器线程}
for I := 0 to sysInfo.dwNumberOfProcessors - 1 do
begin
Thrhandle := CreateThread(nil, 0, @ServerWorkThread, nil, 0, ThreadID);
CloseHandle(Thrhandle);
end;
{获取端口}
GetSocketPort;
{创建监听套接字}
g_Listen := WSASocket(AF_INET, SOCK_STREAM, 0, nil, 0, WSA_FLAG_OVERLAPPED);
g_internetAddr.sin_family := AF_INET;
g_internetAddr.sin_addr.S_addr := htonl(INADDR_ANY);
g_internetAddr.sin_port := htons(g_SocketPort.BusPort);
bind(g_Listen, @g_internetAddr, sizeof(g_internetAddr));
{让套接字为监听做好准备}
listen(g_Listen, MAX_QUEUESIZE);
{创建接收线程}
Thrhandle := CreateThread(nil, 0, @AcceptThread, nil, 0, ThreadID);
CloseHandle(Thrhandle);
end;

initialization
InitializeCriticalSection(g_Lock);
finalization
DeleteCriticalSection(g_Lock);
end.
// 谢谢楼主给我发WinSocket2, 这是我近来学习写的
//那出来相互学习吧
 
感谢各位的热情帮助!!
我用delphi做过些单机的程序.我想首先连接好服务器的数据库,其他的应该和单机版的程序一样吧?
 
1.看TCP/IP
2.看例子
3 自己多动手

如果什么都要别人告诉你,那么很遗憾的告诉你,你不适合做程序!
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
890
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
881
DelphiTeacher的专栏
D
D
回复
0
查看
851
DelphiTeacher的专栏
D
后退
顶部