socket问题(送分喽)(200分)

  • 主题发起人 主题发起人 yhnlzzg
  • 开始时间 开始时间
Y

yhnlzzg

Unregistered / Unconfirmed
GUEST, unregistred user!
大虾,哥们们不好意思,我有个问题要问,有兴趣的快来帮我研究研究.
下面是全部的源代码,问题可能出现在线程上,程序运行后,客户端和
服务端传送数据只能传34次,不知为什么?


unit serverclient;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Memo2: TMemo;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
function ThreadFunc():string;
var
fsock:Tsocket;
sockfd:Tsocket;
addr:Tsockaddr;
addrc:PSockAddr;
addrclen,lens:Integer;
//str:string;
str:array[0..20470]of char;
rb,rl,rlog,i:integer;
wd:wsadata;
getbuf,putbuf:array[0..1024]of char;
begin
i:=0;
wsastartup(2,wd);
fsock:=socket(AF_INET,SOCK_STREAM,0);
try
if fsock>0
then form1.memo1.Lines.Add('服务器建立成功Socket='+inttostr(fsock))
else form1.memo1.Lines.Add('服务器建立失败');
addr.sin_family:=AF_INET;
addr.sin_port:=2223;
addr.sin_addr.S_addr:=htonl(INADDR_ANY);
rb:=bind(fsock,addr,sizeof(addr));
if rb=0
then form1.memo1.Lines.Add('服务器绑定成功')
else form1.memo1.Lines.Add('服务器绑定失败');
rl:=listen(fsock,5);
if rl=0
then form1.memo1.Lines.Add('服务器listen成功')
else form1.memo1.Lines.Add('服务器listen失败');
addrclen:=sizeof(addrc);
while (true) do
begin
//这句阻塞到客户端连接
sockfd:=accept(fsock,addrc,PInteger(addrclen));
if sockfd>=0
then form1.memo1.Lines.Add('服务器接受Socket='+inttostr(sockfd)+'客户端成功')
else form1.memo1.Lines.Add('服务器接受Socket='+inttostr(sockfd)+'客户端失败');
if sockfd>=0
then begin
rlog:=recv(sockfd,getbuf,length(getbuf),0);
if rlog<0 then continue;
form1.memo1.Lines.Add('接受长度='+inttostr(rlog));
form1.memo1.Lines.Add('接受数据='+trim(getbuf));
strcopy(str,PChar('987654321'+inttostr(i)));
lens:=length('987654321'+inttostr(i));
move(str[1],putbuf,lens);
rlog:=send(sockfd,putbuf,lens,0);
form1.memo1.Lines.Add('发送长度='+inttostr(rlog));
form1.memo1.Lines.Add('发送数据='+trim(putbuf));
form1.Memo1.Lines.Add('====================================');
i:=i+1;
end
else begin
form1.memo1.Lines.Add('error!');
break;
end;
end;
except
closesocket(fsock);
end;
end;

function ThreadFunc1():string;
var
csock:Tsocket;
addrc:Tsockaddr;
rcon,len:integer;
strc:string;
Buffer:array[0..20470] of char;
wd:wsadata;
getbuf,putbuf:array[0..20470] of char;
begin
wsastartup(2,wd);
csock:=socket(AF_INET,SOCK_STREAM,0);
form1.memo2.Lines.Add('客户端建立成功Socket='+inttostr(csock));
addrc.sin_family:=AF_INET;
addrc.sin_port:=2223;
addrc.sin_addr.S_addr :=inet_addr('192.168.1.23');
rcon:=connect(csock,addrc,sizeof(addrc));
if rcon=0
then form1.memo2.Lines.Add('客户端连接服务器成功')
else form1.memo2.Lines.Add('客户端连接服务器失败');
// strc:='1234567890';

StrCopy(Buffer, PChar(form1.Edit1.Text));
len:=length(Form1.edit1.text);
move(Buffer,putbuf[0],len);
// move(strc[1],putbuf[0],255);
//发送
rcon:=send(csock,putbuf,len,0);
form1.Memo2.Lines.Add('发送长度:'+inttostr(rcon));
form1.Memo2.Lines.Add('发送数据:'+trim(putbuf));
//接受
rcon:=recv(csock,getbuf,length(getbuf),0);
form1.Memo2.Lines.Add('接受长度:'+inttostr(rcon));
form1.Memo2.Lines.Add('接受数据:'+trim(getbuf));
form1.Memo2.Lines.Add('====================================');



closesocket(csock);
wsacleanup;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
hThread:THandle;
ThreadID:DWord;
begin
hthread:=createThread(nil,
0,
@ThreadFunc,
nil,
0,
ThreadID);
if hthread=0 then
messageBox(Handle,'No Thread',nil,MB_OK);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
hThread:THandle;
ThreadID:DWord;
begin
hthread:=createThread(nil,
0,
@ThreadFunc1,
nil,
0,
ThreadID);
if hthread=0 then
messageBox(Handle,'No Thread',nil,MB_OK);
end;

end.
 
在server中要关闭连接
在i:=i+1;后加一行就OK,给我分吧
closesocket(sockfd);
 
哈哈!我也刚找到,居然回犯这个错误,不过我还是给你分啦!多谢多谢!
 
后退
顶部