我把整个代码贴上来,你帮我看看。
procedure TFrmMain.madeSocket;//初始化socket
begin
If WSAStartup(MAKEWORD(2,2), MyWSA) <> 0 Then
Begin
WSACleanup;
MessageDlg('&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr; Socket &sup3;&ouml;&acute;í&iexcl;&pound;', mtError, [mbYes], 0);
Exit;
end;
hSocket := Socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
If hSocket = INVALID_SOCKET Then
Begin
WSACleanup;
MessageDlg('&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr; Socket &sup3;&ouml;&acute;í&iexcl;&pound;', mtError, [mbYes], 0);
Exit;
End;
Svr.sin_family := AF_INET;
Svr.sin_port := htons(LPort);
Svr.sin_addr.S_addr := INADDR_ANY;//inet_addr(PChar('127.0.0.1'));
If Bind(hSocket, Svr, SizeOf(Svr)) = SOCKET_ERROR Then
Begin
CloseSocket(hSocket);
WSACleanup;
MessageDlg('°ó&para;¨&para;&Euml;&iquest;&Uacute; '+IntToStr(LPort)+' &sup3;&ouml;&acute;í!', mtError, [mbYes], 0);
Exit;
end;
new(Cmd);
FillCommand(68, Cmd);
myudpSocket := TudpSocket.Create(false, hSocket, cmd, FrmMain.Handle);//recvfrom thread
end;
function TFrmMain.FillCommand(CmdType: Byte; Cmmd: PCommand):Boolean;//填充要发送的命令包
begin
Case CmdType of
68:
begin
Cmmd^.SockTo.sin_port := htons(RPort);
Cmmd^.SockTo.sin_addr.S_addr := Inet_addr(PChar('192.168.2.1'));
Cmmd^.CmdBuff[0] := 68;//&sup1;&atilde;&sup2;&yen;&Atilde;ü&Aacute;&icirc;
Cmmd^.CmdBuff[1] := 60;
Cmmd^.CmdBuff[2] := 0;
Cmmd^.CmdBuff[3] := 0;
Cmmd^.CmdLen := 4;
Cmmd^.ToLen := sizeof(Cmmd.SockTo);
Cmmd^.Ready := True;
end;
end;
end;
unit udp_Socket;//这是udp数据包接收和发送的线程
interface
uses
Classes, WinSock, messages, Windows, SysUtils, structure, DealWithData;
type
Tudpsocket = class(TThread)
private
{ Private declarations }
j : Integer;
len : Integer;
Cmd : PCommand;
hsocket : TSocket;
Handle: HWND;
socksize : integer;
buffsize : integer;
myUdpData : Array[0..20] of PUDPData;
myDealWithData : Array[0..20] of TDealWithData;
procedure sendmsg(str : string);
protected
procedure Execute; override;
public
constructor Create(RunStart : Boolean; sock : TSocket; Command: PCommand; FrmHandle: HWND);
end;
implementation
constructor Tudpsocket.Create(RunStart: Boolean; sock: TSocket; Command: PCommand; FrmHandle: HWND);
var
i : integer;
begin
j := 0;
hsocket := sock;
Handle := FrmHandle;
socksize := sizeof(myUdpData[0].sockFrom);
buffsize := length(myUdpData[0].buff);
Cmd := Command;
For i := 0 to 20 do
begin
new(myUdpData);
myDealWithData := TDealWithData.Create(True, @myUdpData, Handle);
end;
inherited Create(RunStart);
end;
procedure Tudpsocket.Execute;
var
i : Integer;
begin
{ Place thread code here }
FreeOnTerminate := True;
sendmsg('Run');
while Not Terminated do
begin //接收数据
if Cmd.Ready then//如果有命令已经填充,那么先发送这个命令包,在接收数据
begin
len := SendTo(hsocket, cmd.CmdBuff, Cmd.CmdLen, 0, Cmd.SockTo, Cmd.ToLen);
Cmd.Ready := False;
end;
{
FSockAddrIn.SIn_Addr.S_addr := inet_addr(PChar('192.168.2.1')); ;
FSockAddrIn.SIn_Port := htons(strtoint('1025'));
len := sendto(hsocket, cmd.CmdBuff, Cmd.CmdLen, 0, FSockAddrIn, sizeof(FSockAddrIn));
}
if Not myDealWithData[j].Suspended then
begin
repeat
inc(j);
j := j mod 21;
sendmsg(IntTostr(j));
until myDealWithData[j].Suspended;
end;
myUdpData[j].data_len := recvFrom(hsocket,myUdpData[j].buff,buffsize,0,myUdpData[j].SockFrom,socksize);
if myUdpData[j].data_len > 0 then
begin
myDealWithData[j].Resume; // call DealWithData
sendmsg('myDealWithData['+IntTostr(j)+'].Resume;');
end;
end;
For i := 0 to 20 do
begin
myDealWithData.Terminate;
end;
end;
procedure Tudpsocket.sendmsg(str: string);
begin
SendMessage(Handle, MY_addline, 0, Integer(PChar(str)));
end;
end.
麻烦了。