Socket阻塞问题? (100分)

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

hnzgf

Unregistered / Unconfirmed
GUEST, unregistred user!
ServerSocket和ClientSocket都用阻塞方式,但是接收的数据总是乱码,请解决!
代码如下:
客户端
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp, ComCtrls;

type
TMyClientThread = Class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;

TForm1 = class(TForm)
CS: TClientSocket;
Btn_Connect: TButton;
StatusBar1: TStatusBar;
Edit1: TEdit;
Memo1: TMemo;
btn_Send: TButton;
Edit2: TEdit;
Button1: TButton;
procedure Btn_ConnectClick(Sender: TObject);
procedure btn_SendClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TMyClientThread.Execute;
var
MyStream : TWinsocketStream;
buffer : string;
i : integer;
begin
buffer := Form1.Edit2.Text + #13#10;
MyStream := TWinsocketStream.Create(Form1.CS.Socket,5000);
try
While (Not Terminated) and (Form1.CS.Active) do begin
try
//MyStream.WaitForData(5000);
i := MyStream.Write(buffer,Length(buffer)+1);
Form1.Memo1.Lines.Add(inttostr(i));
Terminate;
except
end;
end;
finally
MyStream.Free;
end;
end;

procedure TForm1.Btn_ConnectClick(Sender: TObject);
begin
if CS.Active then CS.Close;
CS.Port := 2500;
CS.ClientType := ctBlocking;
CS.Host := Edit1.Text;
try
CS.Open;
StatusBar1.SimpleText := 'Connected to ' + CS.Host;
except
StatusBar1.SimpleText := 'Error Connected to ' + CS.Host;
Exit;
end;
end;

procedure TForm1.btn_SendClick(Sender: TObject);
begin
TMyClientThread.Create(False);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
CS.Close;
end;

end.

服务器端
unit Unit1;

interface

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

type
TMyServerThread = Class(TServerClientThread)
public
procedure ClientExecute;override;
end;

TForm1 = class(TForm)
SS: TServerSocket;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
ListBox1: TListBox;
procedure SSClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
procedure SSThreadStart(Sender: TObject; Thread: TServerClientThread);
procedure SSGetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
procedure SSThreadEnd(Sender: TObject; Thread: TServerClientThread);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TMyServerThread.ClientExecute;
Var
Data: Array[0..1023] Of char;
RecText: String;
SocketStream: TWinSocketStream;
Begin
While Not Terminated And ClientSocket.Connected Do
Try
SocketStream := TWinSocketStream.Create(ClientSocket, 5000);
Try
FillChar(Data, SizeOf(Data), #0);
SocketStream.WaitForData(5000);
If SocketStream.Read(Data, SizeOf(Data)) = 0 Then
Begin
ClientSocket.SendText('Timeout on Server');
sleep(1);
ClientSocket.Close;
Terminate;
End;
RecText := Data;
If Length(RecText) > 2 Then
Delete(RecText, Pos(#13#10, RecText), 2); // Delete #13#10
If ClientSocket.Connected Then
Begin
ClientSocket.SendText(RecText);
SendMessage(Form1.Listbox1.Handle, LB_ADDSTRING, 0, Integer(PChar(RecText)));
//PostMessage(Form1.Handle, CM_INCCOUNT, 0, 0);
End;
Finally
SocketStream.Free;
End;
Except
HandleException;
End;
End;


procedure TForm1.SSClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('Client Will to Write');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
SS.Open;
Memo1.Lines.Add('Listen..');
end;

procedure TForm1.SSThreadStart(Sender: TObject;
Thread: TServerClientThread);
begin
Memo1.Lines.Add('Thread Start');
Memo1.Lines.Add(Thread.ClassName);
end;

procedure TForm1.SSGetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
TMyServerThread.Create(False,ClientSocket);
end;

procedure TForm1.SSThreadEnd(Sender: TObject; Thread: TServerClientThread);
begin
Memo1.Lines.Add('Thread End');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
SS.Close;
end;

end.
 
知道为什么吗?因为你发送和接受得不一致.

程序发过来,我帮你调试

gdcqs14@21cn.com
 
你把procedure TMyClientThread.Execute;中的buffer的定义改成字符数组
string类型的数据中的第一个字节是整个string的长度,你收到的当然是乱码了
 
请gdcqs14调试一下,谢谢!
 
我已经收到了,
 
收发顺序也要自己考虑?!
 
把程序发给我一份吧,我学习学习!
谢啦!
comhacker@sohu.com
 
[:)]把程序发给我一份吧,我学习学习!
多谢啦!
 
MyStream.Write(buffer,Length(buffer)+1);//发送buffer:是那里的值?
//ClientSocket.SendText(RecText);你把RecText打印出来看看!
建议采用sendbuffer;
例子:http://delphi.mychangshu.com/dispdoc.asp?id=531
 
如果可以的话,能发我一份吗?
kevin-csg@163.com
 
多人接受答案了。
 
后退
顶部