谁有delphi编的简单socket程序(不用控件)?(50分)

  • 主题发起人 主题发起人 edwardzhao
  • 开始时间 开始时间
E

edwardzhao

Unregistered / Unconfirmed
GUEST, unregistred user!
编socket有什么经验、书、好网站啊?谢谢指教
 
介绍用delphi写socket的书很少,建议看c写的,或unix下的
 
有人贴过一个超级邮箱轰炸机,delphi+api写的,找找
 
<windows网络编程>, 讲的不错.
 
用Socket API 呀,其实TSocketClient或UDP、TCP等控件调用的也都是Socket API。

MSDN中有好几个用Socket API实现TCP和UDP的例子,可以参考TSocketClient的源码翻译成
Delphi的。这里有一个UDP的例子(不好意思,已经帖过一次了,这是别人写的,我没试过。
但我用C或VC做过。)。
unit main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Winsock, StdCtrls;

const
WM_SOCK = WM_USER + 1; //自定义windows消息
UDPPORT = 6767; //设定UDP端口号
type
Tfrmmain = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);

private
{ Private declarations }
s: TSocket;
addr: TSockAddr;
FSockAddrIn : TSockAddrIn;
procedure ReadData(var Message: TMessage);
message WM_SOCK;
// procedure SendData(HostIP: String; Content: String);
// procedure HandleData(Value: String);

public
{ Public declarations }
// procedure ReadData(var Message: TMessage);
// message WM_SOCK;
procedure SendData(HostIP: String; Content: String);
procedure HandleData(Value: String);

end;

var
frmmain: Tfrmmain;

implementation

//uses test;

{$R *.dfm}

procedure Tfrmmain.HandleData(Value: String);
begin
showmessage(Value);
end;
procedure Tfrmmain.FormCreate(Sender: TObject);
var
TempWSAData: TWSAData;
begin
if WSAStartup($101, TempWSAData)=1 then
showmessage('StartUp Error!');

s := Socket(AF_INET, SOCK_DGRAM, 17);
if (s = INVALID_SOCKET) then //Socket创建失败
begin
showmessage(inttostr(WSAGetLastError()));
CloseSocket(s);
exit;
end;
//发送方SockAddr绑定
addr.sin_family := AF_INET;
addr.sin_addr.S_addr := INADDR_ANY;
addr.sin_port := htons(UDPPORT);
Bind(s, addr, sizeof(addr));
WSAAsyncSelect(s, frmmain.Handle , WM_SOCK, FD_READ);
//接收端SockAddrIn设定
FSockAddrIn.SIn_Family := AF_INET;
FSockAddrIn.SIn_Port := HToNS(UDPPORT);
end;

procedure Tfrmmain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
CloseSocket(s);
end;

procedure Tfrmmain.ReadData(var Message: TMessage);
var
buffer: Array [1..4096] of char;
len: integer;
flen: integer;
Event: word;
value: string;
begin
Event := WSAGetSelectEvent(Message.LParam);
if Event = FD_READ then
begin
len := recvfrom(s, buffer, sizeof(buffer), 0, FSockAddrIn, flen);
value := copy(buffer, 1, len);
showmessage(value);
HandleData(value);
end;

end;

procedure Tfrmmain.SendData(HostIP: String; Content: String);
var
value,hostname: string;
len: integer;
// i: integer;
// str: string;
begin
hostname := HostIP;
FSockAddrIn.SIn_Addr.S_addr := INet_Addr(Pchar(hostname));
value := Content;
len := 1;
len := sendto(s, value[1], Length(value), 0, FSockAddrIn, sizeof(FSockAddrIn));
if (WSAGetLastError() <> WSAEWOULDBLOCK) and (WSAGetLastError() <> 0) then
showmessage(inttostr(WSAGetLastError()));
if len <> Length(value) then
showmessage('Not Send all');

end;

end.
 
左轻侯 说的东西在这
hotsky.363.net
 
《Delphi 网络通信协议分析与应用分析》有一点。
 
后退
顶部