吃
吃素的小狼
Unregistered / Unconfirmed
GUEST, unregistred user!
先给出程序先:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ComCtrls, StdCtrls,winsock,unit2;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
N1: TMenuItem;
connect: TMenuItem;
N3: TMenuItem;
StatusBar1: TStatusBar;
Memo1: TMemo;
Button1: TButton;
procedure connectClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
newthread1:newthread;
{ Public declarations }
end;
var
Form1: TForm1;
servsock:tsocket;
servaddr:tsockaddr;
implementation
{$R *.dfm}
procedure TForm1.connectClick(Sender: TObject);
begin
servsock:=socket(af_inet,sock_dgram,ipproto_udp);
if servSock=INVALID_SOCKET then
begin
messagebox(application.handle,'无法创建句柄!','警告',MB_OK or MB_APPLMODAL or MB_ICONWARNING);
exit;
end;
servaddr.sin_family:=af_inet;
servaddr.sin_addr.S_addr:=htonl(INADDR_ANY);
//servaddr.sin_addr.S_addr:=inet_addr(pchar('127.0.0.1'));
servaddr.sin_port:=htons(5150);
if bind(servsock,servaddr,sizeof(servaddr))=SOCKET_ERROR then
begin
messagebox(application.handle,'绑定地址错误!','警告',MB_OK or MB_APPLMODAL or MB_ICONWARNING);
exit;
end;
statusbar1.Panels[0].Text:='网络服务器开始运行';
end;
procedure TForm1.FormCreate(Sender: TObject);
var wsaData:TwsaData;
begin
if WSAStartup (makeword(2,2), wsaData)<>0 then // WSAStartup返回值为0表示成功
begin
messagebox(application.handle,'无法启动winsock动态连接库!','警告',MB_OK or MB_APPLMODAL or MB_ICONWARNING);
Application.Terminate;
end;
end;
procedure TForm1.N3Click(Sender: TObject);
begin
closesocket(servsock);
wsacleanup;
statusbar1.Panels[0].Text:='网络服务器开始关闭';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
newthread1:=newthread.Create(false);
end;
end.
开个线程接受数据!
unit thread;
interface
uses
Classes,stdctrls,sysutils,winsock;
type
newthread = class(TThread)
private
memo1:tmemo ;
{ Private declarations }
protected
procedure Execute; override;
public
procedure show2;
end;
implementation
uses unit1 ;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure newthread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ newthread }
var recvbuf: array[0..1024] of char ;
relong,fromsize:integer;
re:boolean;
fromaddr:tsockaddr;
procedure newthread.show2;
begin
memo1.Text:=recvbuf;
end;
procedure newthread.Execute;
begin
fromsize:=sizeof(fromaddr);
re:=true;
while re do
begin
relong:=recvfrom(servsock,recvbuf,1024,0,fromaddr,fromsize);
synchronize(show2);
end;
{ Place thread code here }
end;
end.
但是我始终收不到数据啊!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ComCtrls, StdCtrls,winsock,unit2;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
N1: TMenuItem;
connect: TMenuItem;
N3: TMenuItem;
StatusBar1: TStatusBar;
Memo1: TMemo;
Button1: TButton;
procedure connectClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
newthread1:newthread;
{ Public declarations }
end;
var
Form1: TForm1;
servsock:tsocket;
servaddr:tsockaddr;
implementation
{$R *.dfm}
procedure TForm1.connectClick(Sender: TObject);
begin
servsock:=socket(af_inet,sock_dgram,ipproto_udp);
if servSock=INVALID_SOCKET then
begin
messagebox(application.handle,'无法创建句柄!','警告',MB_OK or MB_APPLMODAL or MB_ICONWARNING);
exit;
end;
servaddr.sin_family:=af_inet;
servaddr.sin_addr.S_addr:=htonl(INADDR_ANY);
//servaddr.sin_addr.S_addr:=inet_addr(pchar('127.0.0.1'));
servaddr.sin_port:=htons(5150);
if bind(servsock,servaddr,sizeof(servaddr))=SOCKET_ERROR then
begin
messagebox(application.handle,'绑定地址错误!','警告',MB_OK or MB_APPLMODAL or MB_ICONWARNING);
exit;
end;
statusbar1.Panels[0].Text:='网络服务器开始运行';
end;
procedure TForm1.FormCreate(Sender: TObject);
var wsaData:TwsaData;
begin
if WSAStartup (makeword(2,2), wsaData)<>0 then // WSAStartup返回值为0表示成功
begin
messagebox(application.handle,'无法启动winsock动态连接库!','警告',MB_OK or MB_APPLMODAL or MB_ICONWARNING);
Application.Terminate;
end;
end;
procedure TForm1.N3Click(Sender: TObject);
begin
closesocket(servsock);
wsacleanup;
statusbar1.Panels[0].Text:='网络服务器开始关闭';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
newthread1:=newthread.Create(false);
end;
end.
开个线程接受数据!
unit thread;
interface
uses
Classes,stdctrls,sysutils,winsock;
type
newthread = class(TThread)
private
memo1:tmemo ;
{ Private declarations }
protected
procedure Execute; override;
public
procedure show2;
end;
implementation
uses unit1 ;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure newthread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ newthread }
var recvbuf: array[0..1024] of char ;
relong,fromsize:integer;
re:boolean;
fromaddr:tsockaddr;
procedure newthread.show2;
begin
memo1.Text:=recvbuf;
end;
procedure newthread.Execute;
begin
fromsize:=sizeof(fromaddr);
re:=true;
while re do
begin
relong:=recvfrom(servsock,recvbuf,1024,0,fromaddr,fromsize);
synchronize(show2);
end;
{ Place thread code here }
end;
end.
但是我始终收不到数据啊!