The examples are ancient history, back to a time where they implemented socket-to-file functions, which are now deprecated. Now, they just pass-through to the underlying API socket calls, so those won't compile anymore without recompiling the sockets unit with the legacy symbol defined.
I've had issues with the FP sockets unit being incomplete/wrong in the past. I ended up using my own instead.
If you just want to do it with the sockets unit, no fancy textfile-handling (which is annoying anyway), this should come closer to what you are looking for:
Code:
Program Server;
Uses Sockets;
Var
Buffer : String[255];
Count : LongInt;
ClientSocket : Longint;
ListenSocket : Longint;
ServerAddr : TInetSockAddr;
ClientAddr : TInetSockAddr;
ClientAddrSize : LongInt;
Procedure PrintError (Const Msg : String);
Begin
Writeln (Msg,SocketError);
Halt(100);
End;
Begin
ListenSocket := fpSocket (AF_INET,SOCK_STREAM,0);
If ListenSocket = SOCKET_ERROR Then
PrintError ('Server : Socket : ');
ServerAddr.sin_family := AF_INET;
{ port 50000 in network order }
ServerAddr.sin_port := htons(50000);
ServerAddr.sin_addr.s_addr := htonl($7F000001);
If fpBind(ListenSocket,@ServerAddr,sizeof(ServerAddr)) = SOCKET_ERROR Then
PrintError ('Server : Bind : ');
If fpListen (ListenSocket,1) = SOCKET_ERROR Then
PrintError ('Server : Listen : ');
Writeln('Waiting for Connect from Client, run now sock_cli in an other tty');
ClientAddrSize := sizeof(ClientAddr);
ClientSocket := fpaccept(ListenSocket,@ClientAddr,@ClientAddrSize);
If ClientSocket = SOCKET_ERROR Then
PrintError('Server : Accept : ');
Buffer := 'Message From Server';
Count := Length(Buffer);
If (fpsend(ClientSocket,@Buffer[1],Count,0) = Count) Then
Begin
Repeat
Count := fprecv(ClientSocket,@Buffer[1],255,0);
If (Count <> SOCKET_ERROR) And (Count > 0) Then
Begin
SetLength(Buffer,Count);
Writeln('Server : read : ',Buffer);
End;
Until (Count = SOCKET_ERROR) Or (Count = 0);
End;
CloseSocket(ClientSocket);
CloseSocket(ListenSocket);
End.
Code:
Program Client;
Uses Sockets;
Procedure PrintError(Const Msg : String);
Begin
Writeln(Msg,SocketError);
Halt(100);
End;
Var
ServerAddr : TInetSockAddr;
Buffer : String[255];
ServerSocket : Longint;
Count : Longint;
I : Integer;
Begin
ServerSocket := fpSocket(AF_INET,SOCK_STREAM,0);
If ServerSocket = SOCKET_ERROR Then
PrintError('Client : Socket : ');
ServerAddr.sin_family := AF_INET;
{ port 50000 in network order }
ServerAddr.sin_port := htons(50000);
{ localhost : 127.0.0.1 in network order }
ServerAddr.sin_addr.s_addr :=htonl($7F000001);
If fpconnect(ServerSocket,@ServerAddr,Sizeof(ServerAddr)) = SOCKET_ERROR Then
PrintError('Client : Connect : ');
Buffer := 'This is a textstring sent by the Client.';
For I := 1 To 10 Do
Count := fpsend(ServerSocket,@Buffer[1],Length(Buffer),0);
Count := fprecv(ServerSocket,@Buffer[1],255,0);
if Count <> SOCKET_ERROR Then
Begin
SetLength(Buffer,Count);
Writeln('Server sent: ',Buffer);
End;
CloseSocket(ServerSocket);
End.
Bookmarks