一个简单到极点的c/s程序(50分)

  • 主题发起人 主题发起人 fu_xiang_yu
  • 开始时间 开始时间
F

fu_xiang_yu

Unregistered / Unconfirmed
GUEST, unregistred user!
目的:客户端按button1时,服务端出现对话框,内容是'hello'

客户端:
在button1click事件:
reqcodestr:='hello';
strpcopy(reqcode,reqcodestr);
clientsocket1.address:='127.0.0.1';
clientsocket1.port:=1111;
clientsocket1.active:=true;
clientsocket1.Socket.SendBuf(reqcode,30);

服务器端:(监听端口1111,本地运行)
在clientread事件中,
serversocket1.Socket.ReceiveBuf(reqcode,30);
reqcodestr:=reqcode;
showmessage(reqcodestr);//但这句根本没有反应,没有对话框出现

后来远程运行了一下也不行,请问是怎么回事?
 
delphi目录下有一个自带的 Chat的例子看 一下就懂了
 
看了

可我还没发现我的程序的问题

我真是太#@$@#%了
 
改成这样试试

服务器端:(监听端口1111,本地运行)
在clientread事件中,
Socket.ReceiveBuf(reqcode,30);
reqcodestr:=reqcode;
showmessage(reqcodestr);
 
>>zhhc:
还是不行

怎么办???!!!
 
Client端

unit Unit1;
interface

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

type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
clientsocket1.address:='127.0.0.1';
clientsocket1.port:=1111;
clientsocket1.active:=true;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
reqcode:pchar;
reqcodestr:string;
begin
getmem(reqcode,30);
reqcodestr:='hello';
strpcopy(reqcode,reqcodestr);
clientsocket1.Socket.SendBuf(reqcode^,30);
end;
end.


Server端

unit Unit1;

interface

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

type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
reqcode:pchar;
reqcodestr:string;
begin
getmem(reqcode,30);
Socket.ReceiveBuf(reqcode^,30);
reqcodestr:=reqcode;
showmessage(reqcodestr);
freemem(reqcode);
end;
end.

Client端,在connect后不能马上send,此时send很可能会失败,
所以我用了两个按钮,一个connect,一个send

再就是SendBuf和ReceiveBuf中的Buf,必须用reqcode^,应为reqcode是一个地址,Buf是一个
变量型参数,它在调用时会再取一次参数的地址,所以必须在reqcode后加^
 
给你几个建议:
(1)设置好ClientSocket1的参数后,使用ClientSocket1.Open建立连接(不是Active:=True)
(2)直接使用ClientSocket1.Socket.SendText('hello'+#10)发送字符
(3)同样,在ClientRead事件中使用Socket.ReceiveText接收,如:ShowMessage(Socket.ReceiveText)
 
同意zhhc,Client端在connect后不能马上send!
建议用一个按钮控制连接,或者在设计期间就将Active设为True!
 
我居然忘了三路握手需要一定时间,真是太白痴了

谢谢你们!

另:小弟也有个很好的建议,不如把你们的oicp号给我,
以后我就可以很方便地想你们请教了(其实主要目的是为了节约分,呀,说漏嘴了...)
玩笑啦^_^

不过真的很希望
 
多人接受答案了。
 
后退
顶部