用WinSock居然连不上了.(100)

  • 主题发起人 主题发起人 dai_wei
  • 开始时间 开始时间
D

dai_wei

Unregistered / Unconfirmed
GUEST, unregistred user!
我用服务端与客户端进行连接,服务端和客户端都进行WSAStartup($0101,wsadata);但服务端启动等待客户端连接的时候,都没等到客户端的连接,下面是我写的代码.请高手参考一下.帮我指出问题.谢谢服务端是这样写:procedure TForm1.Button1Click(Sender: TObject);var ca,ra : SockAddr_IN; rcSock : TSocket; ralen : Integer;begin Server := Socket(PF_INET,SOCK_STREAM,IPPROTO_IP); if Server = INVALID_Socket then begin exit; end; ca.sin_family := PF_INET; ca.sin_port := htons(5678); ca.sin_addr.S_addr := INADDR_ANY; if bind(Server,ca,sizeof(ca)) = SOCKET_ERROR then begin exit; end; Listen(Server,5); //等客户端连接 ralen := sizeof(ra); rcSock := accept(Server,@ra,@ralen);end;=======================================================客户端是这样写:procedure TForm1.Button1Click(Sender: TObject);var ra : Sockaddr_IN; len : u_long;begin m_Client := Socket(PF_INET,SOCK_STREAM,IPPROTO_IP); if m_Client =INVALID_SOCKET then begin ra.sin_family := PF_INET; ra.sin_port := htons(5678); ra.sin_addr.S_addr := Inet_addr(Pchar('192.168.1.22')); len := sizeof(ra); if Connect(m_Client,ra,len) <>0 then begin showmessage(' 连接失败'); end else begin showmessage('连接成功'); end; end;end;
 
procedure TForm1.Button1Click(Sender: TObject);var ca,ra : SockAddr_IN; rcSock : TSocket; ralen : Integer;begin Server := Socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if Server = INVALID_SOCKET then begin exit; end; ca.sin_family := AF_INET; ca.sin_port := htons(5678); ca.sin_addr.S_addr := INADDR_ANY; if bind(Server,ca,sizeof(ca)) = SOCKET_ERROR then begin CloseSocket(Server); exit; end; if listen(Server,5)=SOCKET_ERROR then begin CloseSocket(Server); exit; end; //等客户端连接 ralen := sizeof(ra); rcSock := accept(Server,@ra,@ralen);end;procedure TForm1.Button1Click(Sender: TObject);var ra : Sockaddr_IN; len : u_long;begin m_Client := Socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if m_Client <> INVALID_SOCKET then begin ra.sin_family := AF_INET; ra.sin_port := 0; ra.sin_addr.S_addr := INADDR_ANY; if bind(m_Client ,ra,sizeof(ra)) = SOCKET_ERROR then begin CloseSocket(m_Client); exit; end; ra.sin_family := AF_INET; ra.sin_port := 0; ra.sin_addr.S_addr := Inet_addr(Pchar('192.168.1.22')); len := sizeof(ra); if Connect(m_Client,ra,len) <>0 then begin CloseSocket(m_Client); exit; showmessage(' 连接失败'); end else begin showmessage('连接成功'); end; end;end;
 
后退
顶部