L
lsh998
Unregistered / Unconfirmed
GUEST, unregistred user!
请教:
是这样的,
我有服务器端和客户端,
用 serversocket 和 clientsocket 来通信,
客户端不知道服务器的 IP 地址, 服务器知道客户端的 IP 地址,
假设,先运行客户端:
客户端程序如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
procedure FormCreate(Sender: TObject);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
IP:string;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
ClientSocket1.Open;
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode:=0;
end;
end.
服务器:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSocket1.Socket.RemoteAddress:='192.168.0.118';
ServerSocket1.Socket.SendText('dfdf');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ServerSocket1.Socket.RemoteAddress:='192.168.0.119';
ServerSocket1.Socket.SendText('dfdf');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ServerSocket1.Open;
end;
end.
服务器端编译有错误:
ServerSocket1.Socket.RemoteAddress:='192.168.0.118';
cannot assign to a read-only property
各位大哥,那我怎么使 服务器和客户端建立连接
不客户端不进行任何操作
谢谢!
是这样的,
我有服务器端和客户端,
用 serversocket 和 clientsocket 来通信,
客户端不知道服务器的 IP 地址, 服务器知道客户端的 IP 地址,
假设,先运行客户端:
客户端程序如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
procedure FormCreate(Sender: TObject);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
IP:string;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
ClientSocket1.Open;
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode:=0;
end;
end.
服务器:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSocket1.Socket.RemoteAddress:='192.168.0.118';
ServerSocket1.Socket.SendText('dfdf');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ServerSocket1.Socket.RemoteAddress:='192.168.0.119';
ServerSocket1.Socket.SendText('dfdf');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ServerSocket1.Open;
end;
end.
服务器端编译有错误:
ServerSocket1.Socket.RemoteAddress:='192.168.0.118';
cannot assign to a read-only property
各位大哥,那我怎么使 服务器和客户端建立连接
不客户端不进行任何操作
谢谢!