通过Winsock发送字符串失败

时间:2019-06-22 19:49:53

标签: winsock lazarus

我想将一些文本从客户端发送到服务器,但接收器只是挂起,什么也没有通过。

客户

class maze:

    # [...]

    def movePlayerTest(self):
        newPositions = self.findPlayer()

        while not self.gameOver:

            self.drawMap()
            self.blitPlayer(self.findPlayer()[0], self.findPlayer()[1])
            pygame.display.update()

            for event in pygame.event.get():
                # [...]

现在应该发送数据的发送方可以正常工作,它只是接收方。看起来像这样

服务器

program client;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes , SysUtils, Windows, Winsock
  { you can add units after this };

procedure GetInformation();
var
  _wsdata: WSAData;
  result:Integer;
  serverSocket, S: TSocket;
  _addrIn, _addr: sockaddr_in;
  addrSize: Integer;
  tid: Cardinal;
  SendBuf:Array[0..31] of AnsiChar;
  data : Integer;
begin
  //result := false;
  if WSAStartup(MakeWord(2, 2), _wsdata) <> 0 then
    Exit;
  serverSocket := socket(AF_INET, SOCK_STREAM, 0);
  if serverSocket = INVALID_SOCKET then
    Exit;
  _addrIn.sin_family := AF_INET;
  _addrIn.sin_addr.S_addr := INADDR_ANY;
  _addrIn.sin_port := htons(8080);

  if bind(serverSocket, _addrIn, SizeOf(_addrIn)) = SOCKET_ERROR then
    Exit;

  if listen(serverSocket, SOMAXCONN) = SOCKET_ERROR then
    Exit;

  addrSize := SizeOf(_addrIn);
  getsockname(serverSocket, _addrIn, addrSize);

  Writeln(Format('Listening on port %d' + #13#10, [ntohs(_addrIn.sin_port)]));

  while True do
  begin
    S := accept(serverSocket, @_addr, @addrSize);
    data := recv(s,SendBuf,Length(SendBuf),0);
    Writeln('Data Received: ',data);
  end;

  // result := True;
end;

begin
  GetInformation();
  ReadLn;
end.

烦人的是,它只是不显示是否发送,也不显示是否已接收。

我只是将此作为终点

FinalImage

0 个答案:

没有答案