Z
zheng
Unregistered / Unconfirmed
GUEST, unregistred user!
由于要减少服务端执行文件的大小,所以没有用到form。
服务端:
program PServer;
//{$APPTYPE CONSOLE}
uses
Windows,
Sysutils,
Winsock;
const MyPort = 6767;
SockVer = 514;
var Str: string;
Sockfd, Sockfd2: TSocket;
Rece: TWSAData;
Len: Integer;
Server: TSockAddr;
Buf: array[0..1023] of Char;
OptVal: Integer;
begin
try
if WSAStartup(SockVer, Rece) <> 0{SOCKET_ERROR} then
begin
MessageBox(0, 'WSAStartup fail', '调试', MB_ICONINFORMATION + MB_OK);
Exit;
end;
Sockfd := Socket(AF_INET, SOCK_DGRAM, 0);
if Sockfd < 0 then
begin
MessageBox(0, 'Socket fail', '调试', MB_ICONINFORMATION + MB_OK);
Exit;
end;
Server.Sin_Family := AF_INET;
Server.Sin_Port := htons(MyPort);
Server.Sin_Addr.S_addr := INADDR_ANY;
if Bind(Sockfd, Server, Sizeof(Server)) <> 0 then
begin
MessageBox(0, 'Bind fail', '调试', MB_ICONINFORMATION + MB_OK);
Exit;
end;
while (True) do
begin
Len := Recv(Sockfd, Buf, 1024, 0);
if Len > 0 then
begin
//Len := Send(NS, Buf, Len, 0); //返回数据
MessageBox(0, Buf, '接到信息', MB_ICONINFORMATION + MB_OK);
end;
end;
finally
WSACleanup();
MessageBox(0, '套接字结束', '调试', MB_ICONINFORMATION + MB_OK);
end;
end.
客户端:
unit ClientU1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Winsock;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
const MyPort = 6767;
SockVer = 514;
var Str: string;
Sockfd, Sockfd2: TSocket;
Rece: TWSAData;
Len: Integer;
SrvAddr: TSockAddr;
Buf: array[0..1023] of Char;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var OptVal: Integer;
begin
try
if WSAStartup(SockVer, Rece) <> 0 then
begin
MessageBox(0, 'WSAStartup fail', '调试', MB_ICONINFORMATION + MB_OK);
Exit;
end;
Sockfd := Socket(AF_INET, SOCK_DGRAM, 0);
if Sockfd < 0 then
begin
MessageBox(0, 'Socket fail', '调试', MB_ICONINFORMATION + MB_OK);
Exit;
end;
SrvAddr.Sin_Family := AF_INET;
SrvAddr.Sin_Port := htons(MyPort);
SrvAddr.Sin_Addr.S_addr := inet_addr('192.168.0.2');
{if Bind(Sockfd, Client, Sizeof(Client)) <> 0 then
begin
MessageBox(0, 'Bind fail', '调试', MB_ICONINFORMATION + MB_OK);
Exit;
end; }
Buf := 'Beej was here !';
Len := SendTo(Sockfd, Buf, StrLen(Buf), 0, SrvAddr, StrLen(Buf));
MessageBox(0, PChar(IntToStr(Len)), '调试', MB_ICONINFORMATION + MB_OK);
finally
WSACleanup();
end;
end;
end.
和“http://www.delphibbs.com/delphibbs/dispq.asp?lid=2214754”内容相同,回答任一条200分都得。
服务端:
program PServer;
//{$APPTYPE CONSOLE}
uses
Windows,
Sysutils,
Winsock;
const MyPort = 6767;
SockVer = 514;
var Str: string;
Sockfd, Sockfd2: TSocket;
Rece: TWSAData;
Len: Integer;
Server: TSockAddr;
Buf: array[0..1023] of Char;
OptVal: Integer;
begin
try
if WSAStartup(SockVer, Rece) <> 0{SOCKET_ERROR} then
begin
MessageBox(0, 'WSAStartup fail', '调试', MB_ICONINFORMATION + MB_OK);
Exit;
end;
Sockfd := Socket(AF_INET, SOCK_DGRAM, 0);
if Sockfd < 0 then
begin
MessageBox(0, 'Socket fail', '调试', MB_ICONINFORMATION + MB_OK);
Exit;
end;
Server.Sin_Family := AF_INET;
Server.Sin_Port := htons(MyPort);
Server.Sin_Addr.S_addr := INADDR_ANY;
if Bind(Sockfd, Server, Sizeof(Server)) <> 0 then
begin
MessageBox(0, 'Bind fail', '调试', MB_ICONINFORMATION + MB_OK);
Exit;
end;
while (True) do
begin
Len := Recv(Sockfd, Buf, 1024, 0);
if Len > 0 then
begin
//Len := Send(NS, Buf, Len, 0); //返回数据
MessageBox(0, Buf, '接到信息', MB_ICONINFORMATION + MB_OK);
end;
end;
finally
WSACleanup();
MessageBox(0, '套接字结束', '调试', MB_ICONINFORMATION + MB_OK);
end;
end.
客户端:
unit ClientU1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Winsock;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
const MyPort = 6767;
SockVer = 514;
var Str: string;
Sockfd, Sockfd2: TSocket;
Rece: TWSAData;
Len: Integer;
SrvAddr: TSockAddr;
Buf: array[0..1023] of Char;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var OptVal: Integer;
begin
try
if WSAStartup(SockVer, Rece) <> 0 then
begin
MessageBox(0, 'WSAStartup fail', '调试', MB_ICONINFORMATION + MB_OK);
Exit;
end;
Sockfd := Socket(AF_INET, SOCK_DGRAM, 0);
if Sockfd < 0 then
begin
MessageBox(0, 'Socket fail', '调试', MB_ICONINFORMATION + MB_OK);
Exit;
end;
SrvAddr.Sin_Family := AF_INET;
SrvAddr.Sin_Port := htons(MyPort);
SrvAddr.Sin_Addr.S_addr := inet_addr('192.168.0.2');
{if Bind(Sockfd, Client, Sizeof(Client)) <> 0 then
begin
MessageBox(0, 'Bind fail', '调试', MB_ICONINFORMATION + MB_OK);
Exit;
end; }
Buf := 'Beej was here !';
Len := SendTo(Sockfd, Buf, StrLen(Buf), 0, SrvAddr, StrLen(Buf));
MessageBox(0, PChar(IntToStr(Len)), '调试', MB_ICONINFORMATION + MB_OK);
finally
WSACleanup();
end;
end;
end.
和“http://www.delphibbs.com/delphibbs/dispq.asp?lid=2214754”内容相同,回答任一条200分都得。