D
dazzling
Unregistered / Unconfirmed
GUEST, unregistred user!
最后显示连接主机失败,但那主机是合法的,连接上应立刻显示一段欢迎信息的.
//1.建立SOCKET
skt:=socket(AF_INET,SOCK_STREAM,0);
if(skt=INVALID_SOCKET)then begin
memo1.Lines.Add('建立SOCKET失败!');
exit;
end;
//2.连接主机
zeroMemory(@addr,sizeof(addr));
addr.sin_family:=AF_INET;
addr.sin_addr.S_addr:=inet_Addr(pchar('218.108.26.76'));
addr.sin_port:=21;
re:=connect(skt,addr,sizeof(addr));
if re<>0 then begin
memo1.Lines.Add('连接主机失败!'); //这里总是出现
exit;
end;
//3.发送信息
GetMem(bufSend,1024);
zeroMemory(bufSend,1024);
info:='open'+#10;
strPCopy(bufSend,info);
re:=send(skt,bufSend^,length(bufSend),0);
if(re=SOCKET_ERROR)then begin
memo1.Lines.Add('发送失败!');
exit;
end;
//4.读信息
GetMem(bufRecv,1024);
repeat
zeroMemory(bufRecv,1024);
re:=recv(skt,bufRecv^,1024,0);
info:=strPas(bufRecv);
memo1.Lines.Add(info);
application.ProcessMessages;
until re=0;
memo1.Lines.Add('完成!');
freeMem(bufSend);
freeMem(bufRecv);
//5.关闭SOCKET
closeSocket(skt);
end;
//1.建立SOCKET
skt:=socket(AF_INET,SOCK_STREAM,0);
if(skt=INVALID_SOCKET)then begin
memo1.Lines.Add('建立SOCKET失败!');
exit;
end;
//2.连接主机
zeroMemory(@addr,sizeof(addr));
addr.sin_family:=AF_INET;
addr.sin_addr.S_addr:=inet_Addr(pchar('218.108.26.76'));
addr.sin_port:=21;
re:=connect(skt,addr,sizeof(addr));
if re<>0 then begin
memo1.Lines.Add('连接主机失败!'); //这里总是出现
exit;
end;
//3.发送信息
GetMem(bufSend,1024);
zeroMemory(bufSend,1024);
info:='open'+#10;
strPCopy(bufSend,info);
re:=send(skt,bufSend^,length(bufSend),0);
if(re=SOCKET_ERROR)then begin
memo1.Lines.Add('发送失败!');
exit;
end;
//4.读信息
GetMem(bufRecv,1024);
repeat
zeroMemory(bufRecv,1024);
re:=recv(skt,bufRecv^,1024,0);
info:=strPas(bufRecv);
memo1.Lines.Add(info);
application.ProcessMessages;
until re=0;
memo1.Lines.Add('完成!');
freeMem(bufSend);
freeMem(bufRecv);
//5.关闭SOCKET
closeSocket(skt);
end;