有哪位大侠能够提供监听端口11000的源码吗???小弟正在试用期中,搞不出来就完了,急!!!(100分)

  • 主题发起人 主题发起人 happyproblem
  • 开始时间 开始时间
H

happyproblem

Unregistered / Unconfirmed
GUEST, unregistred user!
公司要我做个demo程序,将其他软件间通信的数据读出来,我只知道他们之间通信的端口号为11000,有哪位大哥可以帮帮小弟吗?写个demo程序将流进端口11000的数据读出来???[:(!][:(!]
 
我自己写了个,但是从端口读不出来数据,请大家帮帮我这个菜鸟
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Sockets;

type
TForm1 = class(TForm)
Memo1: TMemo;
TcpServer1: TTcpServer;
TcpClient1: TTcpClient;
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
procedure TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
procedure Button1Click(Sender: TObject);



private
{ Private declarations }
public
{ Public declarations }
end;
TClientDataThread = class(TThread)
private
public
ListBuffer :TStringList;
TargetList :TStrings;

end;


var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);

var
s: string;
DataThread: TClientDataThread;
begin
// create thread
DataThread:= TClientDataThread.Create(true);
// set the TagetList to the gui list that you
// with to synch with.
DataThread.TargetList := memo1.lines;

// Load the Threads ListBuffer
DataThread.ListBuffer.Add('*** Connection Accepted ***');
DataThread.ListBuffer.Add('Remote Host: ' + ClientSocket.LookupHostName(ClientSocket.RemoteHost) +
' (' + ClientSocket.RemoteHost + ')');
DataThread.ListBuffer.Add('===== Begin message =====');
s := ClientSocket.Receiveln;
while s <> '' do
begin
DataThread.ListBuffer.Add(s);
s := ClientSocket.Receiveln;
end;
DataThread.ListBuffer.Add('===== End of message =====');

// Call Resume which will execute and synch the
// ListBuffer with the TargetList
DataThread.Resume;

end;



procedure TForm1.Button1Click(Sender: TObject);
begin
TcpClient1.RemoteHost := EDIT2.Text;
TcpClient1.RemotePort := EDIT1.Text;
TcpServer1.LocalPort := Edit1.Text;
TcpServer1.Active := True;
end;

end.
 
&quot;TcpServer1.LocalPort := Edit1.Text;&quot;

你这段代码能通过编译吗?
 
下面的代码是监听一个端口,通过消息来读信息:
uses Winsock;

connect
WM_SOCK = WM_USER + 11; //自定义端口消息
GETUDPPORT = 6601;

function StartNetServer:boolean;
var
TempWSAData: TWSAData;
begin
result:=true;
// 初始化SOCKET
if WSAStartup(2, TempWSAData)<>0 then
msg('SOCKET初始化错误',merr);
s := Socket(AF_INET, SOCK_DGRAM, 0);
if (s = INVALID_SOCKET) then //Socket创建失败
begin
msg('Socket创建失败',merr);
result:=false;
CloseSocket(s);
exit;
end;
//发送方SockAddr绑定
addr.sin_family := AF_INET;
addr.sin_addr.S_addr := INADDR_ANY;
addr.sin_port := htons(GETUDPPORT);
if Bind(s, addr, sizeof(addr)) <> 0 then
begin
msg('监听端口 '+inttostr(GETUDPPORT)+' 失败。',merr);
result:=false;
CloseSocket(s);
exit;
end;
WSAAsyncSelect(s, frm_window.Handle , WM_SOCK, FD_READ);
end;

//------------------------------------------

Tfrm_window = class(TForm)
private
{ Private declarations }
procedure ReadData(var Message: TMessage);message WM_SOCK;//接受到的端口信息处理
public
end;


procedure Tfrm_window.ReadData(var Message: TMessage);
var
buffer: Array [1..4096] of char;
len: integer;
flen: integer;
value: string;
begin
flen:=sizeof(FSockAddrIn);
if WSAGetSelectEvent(Message.LParam) = FD_READ then
begin
len := recvfrom(s, buffer, sizeof(buffer), 0, FSockAddrIn, flen);
value := copy(buffer, 1, len);
//value是你接受到的东西,自己根据内容进行处理
end;
end;
 
后退
顶部