我的CLIENT端应该是没问题了,因为我用控件做SERVER端的话传输是正常的,但SERVER端用API
写的连接没问题,SEND也成功了,但是RECV就是收不到了。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,Winsock,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
sock,len,msgsock:integer;
//sock,length,,rval,err:integer;
buf:array[0..1023]of char;
wVersionRequested:WORD;
wsaData:TWSADATA;
server:TSockAddrIn;
i,err:integer;
procedure TForm1.Button1Click(Sender: TObject);
begin
wVersionRequested:=MAKEWORD(2,0);
err:=WSAStartup(wVersionRequested,wsaData);
if err=-1 then
Label1.Caption:='初始化失败!'
else begin
sock:=socket(AF_INET,SOCK_STREAM,0);
if sock<0 then
Label1.Caption:='创建套接口失败!'
else begin
server.sin_family:=AF_INET;
server.sin_port:=htons(2000);
server.sin_addr.s_addr:=INADDR_ANY;
bind(sock,server,sizeof(server));
len:=sizeof(server);
getsockname(sock,server,len);
listen(sock,5);
msgsock:=accept(sock,@server,@len);
i:=recv(sock,buf,1024,MSG_OOB);
if i<0 then
Label1.Caption:='接收失败!';
end
end
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Closesocket(msgsock);
WSACleanup();
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Edit1.Text:=buf;
end;
end.
呵呵希望各位高手帮忙了谢谢了!