udp接受问题,大家帮忙!(100分)

  • 主题发起人 主题发起人 吃素的小狼
  • 开始时间 开始时间

吃素的小狼

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.
但是我始终收不到数据啊!
 
我顶到死为止!
 
睡了一觉起来还是没有人哦![:(][:(]
 
自己已经解决问题,大家不用在费心辣,谢谢!
欢迎有高手继续讨论下socket之udp多线程编程!
言论有理者皆有分!
 
在Delphi6/7中利用UDP协议传送文件的例子
http://www.aidelphi.com/6to23/docu/MyUDP(Delphi6_7).rar
这个你要不要参看一下???
 
这里人气好差哦,半天都没的人进来发言!高手都保留发言权哦!
 
可惜我不会呀,我才开始学习这方面的东西
 
我们好好交流一下,如何?QQ:7041779
 
看了一下,好像沒什麼大的問題哦,除了這句之外:
relong:=recvfrom(servsock,recvbuf,1024,0,fromaddr,fromsize);
應該改為:
relong:=recvfrom(servsock,recvbuf,1024,0,@fromaddr,@fromsize);
(看了一下winsock聲明,其實無所謂的,都可以:))
還請樓主指點一下,究竟問題在那啊?
 
后退
顶部