Sockets: Difference between revisions
Content deleted Content added
No edit summary |
|||
Line 814: | Line 814: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Tested using <code>nc -l 256</code>, on both macOS Sierra and Windows 7, compiled using Free Pascal. |
|||
See [[Sockets#Delphi | Delphi]] |
|||
<lang pascal>Program Sockets_Example; |
|||
Uses |
|||
{ Free Pascal RTL sockets unit } |
|||
sockets; |
|||
Var |
|||
TCP_Sock: integer; |
|||
Remote_Addr: TSockAddr; |
|||
Message: string; |
|||
Begin |
|||
{ Fill the record (struct) with the server's address information } |
|||
With Remote_Addr do |
|||
begin |
|||
Sin_family := AF_INET; |
|||
Sin_addr := StrToNetAddr('127.0.0.1'); |
|||
Sin_port := HtoNs(256); { Converts short integer to network byte order } |
|||
end; |
|||
{ Returns an IPv4 TCP socket descriptor } |
|||
TCP_Sock := fpSocket(AF_INET, SOCK_STREAM, IPPROTO_IP); |
|||
{ fpSocket returns -1 on failure } |
|||
If TCP_Sock = -1 then |
|||
begin |
|||
WriteLn('Failed to create new socket descriptor'); |
|||
Halt(1); |
|||
end; |
|||
{ Attempt to connect to the address supplied above } |
|||
If fpConnect(TCP_Sock, @Remote_Addr, SizeOf(Remote_Addr)) = -1 then |
|||
begin |
|||
{ Specifc error codes can be retrieved by calling the SocketError function } |
|||
WriteLn('Failed to contact server'); |
|||
Halt(1); |
|||
end; |
|||
{ Finally, send the message to the server and disconnect } |
|||
Message := 'Hello socket world'; |
|||
fpSend(TCP_Sock, @Message, SizeOf(Message), 0); |
|||
CloseSocket(TCP_Sock); |
|||
End. |
|||
</lang> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |