P
pniker
Unregistered / Unconfirmed
GUEST, unregistred user!
我想用winsock写一个客户/服务的模式程序,服务端监听端口后用telnet能正确的显示数据在cmd下,可是用我写的客户端却总是返回-1的结果到我的edit控件中,想了好久不知道问题出哪了,小菜我刚进富翁不久,分不多,但是恳请大家不佞赐教。
我的客户端代码:
const
s_port=803;
var
ws:WSADATA;
sock_addr:sockaddr_in;
sockfd,size1:integer;
v:byte;
begin
WSASTARTUP(MAKEWORD(2,2),ws);
sockfd:=socket(AF_INET,SOCK_STREAM,0);
sock_addr.sin_family:=AF_INET;
sock_addr.sin_port:=htons(s_port);
sock_addr.sin_addr.S_addr:=inet_addr('127.0.0.1');
Connect(sockfd,sock_addr,sizeof(sock_addr));
size1:=recv(sockfd,v,sizeof(v),0);
edit1.Text:=inttostr(size1);
Closesocket(sockfd);
end;
服务端代码:
const
s_port=830;
BackLog=10;
var
ws:wsadata;
sock_addr:sockaddr_in;
sockfd,newfd,size1:integer;
size2:char;
begin
WSASTARTUP(Makeword(2,2),ws);
sockfd:=socket(AF_INET,SOCK_STREAM,0);
sock_addr.sin_family:=AF_INET;
sock_addr.sin_port:=htons(s_port);
sock_addr.sin_addr.S_addr:=INADDR_ANY;
bind(Sockfd,sock_addr,sizeof(sock_addr));
listen(Sockfd,BackLog);
size1:=sizeof(sock_addr);
Newfd:=Accept(Sockfd,@sock_addr,@size1);
size2:='w';
Send(newfd,size2,sizeof(size2),0);
closesocket(sockfd);
closesocket(newfd);
end;
我的客户端代码:
const
s_port=803;
var
ws:WSADATA;
sock_addr:sockaddr_in;
sockfd,size1:integer;
v:byte;
begin
WSASTARTUP(MAKEWORD(2,2),ws);
sockfd:=socket(AF_INET,SOCK_STREAM,0);
sock_addr.sin_family:=AF_INET;
sock_addr.sin_port:=htons(s_port);
sock_addr.sin_addr.S_addr:=inet_addr('127.0.0.1');
Connect(sockfd,sock_addr,sizeof(sock_addr));
size1:=recv(sockfd,v,sizeof(v),0);
edit1.Text:=inttostr(size1);
Closesocket(sockfd);
end;
服务端代码:
const
s_port=830;
BackLog=10;
var
ws:wsadata;
sock_addr:sockaddr_in;
sockfd,newfd,size1:integer;
size2:char;
begin
WSASTARTUP(Makeword(2,2),ws);
sockfd:=socket(AF_INET,SOCK_STREAM,0);
sock_addr.sin_family:=AF_INET;
sock_addr.sin_port:=htons(s_port);
sock_addr.sin_addr.S_addr:=INADDR_ANY;
bind(Sockfd,sock_addr,sizeof(sock_addr));
listen(Sockfd,BackLog);
size1:=sizeof(sock_addr);
Newfd:=Accept(Sockfd,@sock_addr,@size1);
size2:='w';
Send(newfd,size2,sizeof(size2),0);
closesocket(sockfd);
closesocket(newfd);
end;