核心提示:unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Di...
unit Unit1;interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Winsock;
type
TForm1 = class(TForm)
Button2: TButton; //开始接收数据
ListBox1: TListBox; //将接收到的数据显示到列表中
Edit1: TEdit; //显示接收了多少次数据
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure CheckRc(szErr : PChar);
var
Form1: TForm1;
const
DEFAULT_PORT = 5150; //接收端口号
DEFAULT_COUNT = 20; //接收20次
DEFAULT_BUFFER_LENGTH = 4096; //接收数据的最大缓冲区大小
var
nPort : Integer = DEFAULT_PORT;
dwCount : DWORD = DEFAULT_COUNT;
dwLength : DWORD = DEFAULT_BUFFER_LENGTH ;
szInterface : array[0..31] of char;
wsd : WSAData; //保存初始化信息
soRecv : TSOCKET; //接收数据的socket
pszRecv: PChar; //接收数据的缓冲区指针
nRet : integer;
i : integer;
dwSenderSize : Integer;
siSender,local : SOCKADDR_IN;
nError : Integer;
implementation
{$R *.dfm}
procedure CheckRc(szErr : PChar);
begin
MessageBox(0,szErr,'Error',MB_OK);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i: integer;
nLastError : Integer;
nNumOfRecv : Integer;
begin
nNumOfRecv := 0;
//初始化socket
nError := WSAStartup(MakeWord(2,2),wsd);
if Boolean(nError) then
MessageBox(0,'WSAStartup','Error',MB_OK);
//建立socket
soRecv := socket(AF_INET,SOCK_DGRAM,0);
if soRecv = SOCKET_ERROR then
begin
CheckRc('socket');
end;
//设置端口与地址
local.sin_family := AF_INET;
local.sin_port := htons(5150);
local.sin_addr.S_addr := inet_addr('127.0.0.1');
//将socket绑定地址
nError := bind(soRecv,local,sizeof(local));
if nError = SOCKET_ERROR then
CheckRc('bind');
//分配内存
pszRecv := AllocMem(4096);
for i := 0 to dwCount -1 do
begin
dwSenderSize := SizeOf(siSender);
//接收发送过来的数据,并保存到缓冲区中
nRet := recvFrom(soRecv,pszRecv[0],4096,0,siSender,dwSenderSize);
if nRet = SOCKET_ERROR then
begin
CheckRc('recvFrom');
break;
end else if nRet = 0 then
break
else
begin
pszRecv[nRet] := #0;
//添加到列表中,仅用于显示
self.ListBox1.Items.Add(pszRecv);
end;
Inc(nNumOfRecv);
self.Edit1.Text := IntToStr(nNumOfRecv);
end; //end of for
//关闭 socket
closesocket(soRecv);
//释放分配的内存
FreeMem(pszRecv);
//清理winsocket
WSACleanup();
end;
end.