Sockets: Difference between revisions

m (Somehow a copy of the Neko socket example was placed inside the one for Smalltalk)
Line 814:
 
=={{header|Pascal}}==
Tested using <code>nc -l 256</code>, on both macOS Sierra and Windows 7, compiled using Free Pascal in the default FPC mode.
Very similar to the [https://rosettacode.org/wiki/Sockets#C example in C].
<lang pascal>Program Sockets_Example;
 
See also [https://rosettacode.org/wiki/Sockets#Delphi Delphi].
<lang pascal>Program Sockets_ExampleSockets_ExampleA;
 
Uses
Line 824 ⟶ 827:
TCP_Sock: integer;
Remote_Addr: TSockAddr;
 
Message: string;
PMessage: Pchar;
Message_Len: integer;
 
 
Line 833 ⟶ 839:
Sin_family := AF_INET;
Sin_addr := StrToNetAddr('127.0.0.1');
Sin_port := HtoNs(256); { Converts short integer to network byte order }
end;
 
Line 839 ⟶ 845:
TCP_Sock := fpSocket(AF_INET, SOCK_STREAM, IPPROTO_IP);
 
{ fpSocketMost returnsroutines in this unit return -1 on failure }
If TCP_Sock = -1 then
begin
Line 855 ⟶ 861:
 
{ Finally, send the message to the server and disconnect }
Message := 'Hello socket world';
PMessage := @Message;
fpSend(TCP_Sock, @Message, SizeOf(Message), 0);
Message_Len := StrLen(PMessage);
 
If fpSend(TCP_Sock, PMessage, Message_Len, 0) <> Message_Len then
begin
WriteLn('An error occurred while sending data to the server');
Halt(1);
end;
 
CloseSocket(TCP_Sock);
End.
</lang>
 
Variant without superfluous additions:
<lang pascal>Program Sockets_ExampleB;
 
Uses
sockets;
 
Var
TCP_Sock: integer;
Remote_Addr: TSockAddr;
 
Message: string;
PMessage: Pchar;
Message_Len: integer;
 
Begin
Remote_Addr.Sin_family := AF_INET;
Remote_Addr.Sin_addr := StrToNetAddr('127.0.0.1');
Remote_Addr.Sin_port := HtoNs(256);
 
TCP_Sock := fpSocket(AF_INET, SOCK_STREAM, IPPROTO_IP);
 
fpConnect(TCP_Sock, @Remote_Addr, SizeOf(Remote_Addr));
 
Message := 'Hello socket world';
PMessage := @Message;
Message_Len := StrLen(PMessage);
 
fpSend(TCP_Sock, @MessagePMessage, SizeOf(Message)Message_Len, 0);
End.
</lang>