написал небольшую прожину клиент \ сервер. 1) Клиент устанавливает связь с сервером. 2) Сервер приветствует клиента 3) Клиент читает приветствие и тоже приветствует сервер. 4) Сервер читает ответ клиента и не видит ничего. Вот в этом вся проблема Чувствую, что как-то не так делаю. Подскажите, как правильно читать ответ от сервера? | Код | unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Sockets, ExtCtrls;
type TForm1 = class(TForm) TcpServer1: TTcpServer; Button1: TButton; Memo1: TMemo; Timer1: TTimer; Button2: TButton; Button3: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); procedure TcpServer1Accept(Sender: TObject; ClientSocket: TCustomIpClient); procedure TcpServer1CreateHandle(Sender: TObject); procedure Timer1Timer(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1; Client: TCustomIpClient; implementation
{$R *.dfm}
//*********************************************
function StringToHex(str1,Separator:String):String; var buf:String; i:Integer; begin buf:=''; for i:=1 to Length(str1) do begin buf:=buf+IntToHex(Byte(str1[i]),2)+Separator; end; Result:=buf; end;
function HexToString(Hex:String):String; function SymbolEntersCount(s: string): string; var i: integer; begin Result := ''; for i := 1 to Length(s) do if not(s[i] in [' ',#10,#13]) then Result:=Result+s[i]; end; var buf:String; bt:Byte; i:Integer; begin buf:=''; Hex:=SymbolEntersCount(UpperCase(Hex)); for i:=0 to (Length(Hex) div 2)-1 do begin bt:=0; if (Byte(hex[i*2+1])>$2F)and(Byte(hex[i*2+1])<$3A)then bt:=Byte(hex[i*2+1])-$30 else if (Byte(hex[i*2+1])>$40)and(Byte(hex[i*2+1])<$47)then bt:=Byte(hex[i*2+1])-$37; if (Byte(hex[i*2+2])>$2F)and(Byte(hex[i*2+2])<$3A)then bt:=bt*16+Byte(hex[i*2+2])-$30 else if (Byte(hex[i*2+2])>$40)and(Byte(hex[i*2+2])<$47)then bt:=bt*16+Byte(hex[i*2+2])-$37; buf:=buf+char(bt); end; HexToString:=buf; end;
//*********************************************
procedure TForm1.Button1Click(Sender: TObject); begin TcpServer1.Open ; end;
procedure TForm1.Button2Click(Sender: TObject); begin TcpServer1.Close; Memo1.Lines.Add('Server stoped!'); end;
procedure TForm1.Button3Click(Sender: TObject); var msg: string; i: integer; begin
end;
procedure TForm1.TcpServer1Accept(Sender: TObject; ClientSocket: TCustomIpClient); var str, mess: string; begin { ClientsVal:=ClientsVal+1; Clients[ClientsVal] := ClientSocket;
Memo1.Lines.Add('New Client Accept. Addr: ' + Clients[ClientsVal].LocalHostAddr);
Clients[ClientsVal].Sendln(':NeedAuth: '); Memo1.Lines.Add('Send Auth request to client'); }
Memo1.Lines.Add('New Client Accept'); str := '220 HELLO! '; ClientSocket.Sendln(StringToHex(str,' '));
end;
procedure TForm1.TcpServer1CreateHandle(Sender: TObject); begin Timer1.Enabled := TRUE; Memo1.Lines.Add('Server Started!'); end;
procedure TForm1.Timer1Timer(Sender: TObject); var mess: string; begin mess := TcpServer1.Receiveln();
if mess <> '' then begin SetLength(mess,SizeOf(mess)+6); Memo1.Lines.Add('Recieve from client: ' + mess); end;
end;
end.
|
Еще один вопрос, когда посылаю данные с сервера, то надо ставить 7 пробелов ибо последние 7 символов обрезаются. почему так? | Код | str := '220 HELLO! ';
|
И на всякий случай, код клиента: | Код | unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Sockets, ExtCtrls;
type TForm1 = class(TForm) TcpClient1: TTcpClient; Button1: TButton; Memo1: TMemo; Label1: TLabel; Edit1: TEdit; Timer1: TTimer; Label2: TLabel; Edit2: TEdit; Label3: TLabel; Edit3: TEdit; procedure Button1Click(Sender: TObject); procedure TcpClient1Connect(Sender: TObject); procedure TcpClient1Error(Sender: TObject; SocketError: Integer); procedure Timer1Timer(Sender: TObject); procedure TcpClient1Disconnect(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } public { Public declarations } end;
var Form1: TForm1; LastMsg: string; auth, AuthRequest: integer; implementation
{$R *.dfm}
//***************************************
function HexToString(Hex:String):String; function SymbolEntersCount(s: string): string; var i: integer; begin Result := ''; for i := 1 to Length(s) do if not(s[i] in [' ',#10,#13]) then Result:=Result+s[i]; end; var buf:String; bt:Byte; i:Integer; begin buf:=''; Hex:=SymbolEntersCount(UpperCase(Hex)); for i:=0 to (Length(Hex) div 2)-1 do begin bt:=0; if (Byte(hex[i*2+1])>$2F)and(Byte(hex[i*2+1])<$3A)then bt:=Byte(hex[i*2+1])-$30 else if (Byte(hex[i*2+1])>$40)and(Byte(hex[i*2+1])<$47)then bt:=Byte(hex[i*2+1])-$37; if (Byte(hex[i*2+2])>$2F)and(Byte(hex[i*2+2])<$3A)then bt:=bt*16+Byte(hex[i*2+2])-$30 else if (Byte(hex[i*2+2])>$40)and(Byte(hex[i*2+2])<$47)then bt:=bt*16+Byte(hex[i*2+2])-$37; buf:=buf+char(bt); end; HexToString:=buf; end;
//***************************************
procedure RunCommand(Command: String); begin Command := HexToString(Command); if Command = '220' then begin if Form1.TcpClient1.Connected then begin Form1.TcpClient1.Sendln('HELLO!'); Form1.Memo1.Lines.Add('Send hello'); Form1.Timer1.Enabled := FALSE; end; end else begin Form1.Memo1.Lines.Add('Command: ' + Command); end;
end;
//***************************************
procedure TForm1.Button1Click(Sender: TObject); var size: word; pck: string; begin TcpClient1.RemoteHost := Edit1.Text; TcpClient1.Open; end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin Timer1.Enabled := FALSE; TcpClient1.Close; end;
procedure TForm1.TcpClient1Connect(Sender: TObject); begin Memo1.Lines.Add('Conected OK!'); LastMsg := '0'; auth := 0; AuthRequest := 0; Timer1.Enabled := TRUE; end;
procedure TForm1.TcpClient1Disconnect(Sender: TObject); begin Memo1.Lines.Add('Connection to server is closed'); end;
procedure TForm1.TcpClient1Error(Sender: TObject; SocketError: Integer); begin Memo1.Lines.Add('Connection error. Err code ' + inttostr(SocketError)); end;
procedure TForm1.Timer1Timer(Sender: TObject); var mess: string; SendAuthRequest: boolean; begin mess := Form1.TcpClient1.Receiveln();
if (mess <> '') AND (LastMsg <> mess) then begin LastMsg := mess; SetLength(mess,SizeOf(mess)+6); Memo1.Lines.Add('Recieve from server: ' + mess); LastMsg := mess;
RunCommand(mess);
end; { if auth = 0 then begin if AuthRequest = 0 then begin TcpClient1.Sendln('220 HELLO!'); Memo1.Lines.Add('Send hello'); AuthRequest := 1; end;
end; }
end;
end.
|
Сервер - TCPServer, клиент соответственно TCPClient. Буду очень благодарен за подсказку! Это сообщение отредактировал(а) Agares - 24.11.2010, 20:04
|