Sockets: Difference between revisions

2,494 bytes added ,  2 months ago
m
→‎{{header|Frink}}: Fixed port number
(→‎{{header|Wren}}: Added note that, like several other entries, this no longer works and added code to show the error message.)
m (→‎{{header|Frink}}: Fixed port number)
 
(3 intermediate revisions by 3 users not shown)
Line 218:
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
 
const char *msg = "hello socket world";
Line 469 ⟶ 470:
 
close-socket</syntaxhighlight>
 
=={{header|FreeBASIC}}==
{{libheader|winsock}}
{{works with|Windows FreeBASIC}}
<syntaxhighlight lang="vbnet">' We import the windows sockets library
#Include Once "windows.bi"
#Include Once "win/winsock.bi"
 
#include "fbgfx.bi"
 
#define NET_BUFLEN 1024
 
'--- SENDER ---
 
Dim As WSADATA wsaData
 
Dim As SOCKET sendSocket
Dim As sockaddr_in recvAddr
Dim As Integer port = 256
Dim As Ubyte sendBuf(NET_BUFLEN-1)
Dim As Integer bufLen = NET_BUFLEN
Dim As Integer iResult
sock.sendall(Dim As String message = "hello socket world")
 
' We copy the message to the buffer
For i As Ubyte = 1 To Len(message)
sendBuf(i-1) = Cbyte(Asc(Mid(message, i, 1)))
Next
 
' We update bufLen to be the length of the message
bufLen = Len(message)
 
' We initialize Winsock
If (WSAStartup(MAKEWORD(2,2), @wsaData) <> 0) Then
Beep: Print "Error: Winsock init"
Sleep
End
End If
 
' We create the socket
sendSocket = socket_(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
If sendSocket = INVALID_SOCKET Then
Beep: Print "Error: Net socket"
WSACleanup()
Sleep
End
End If
 
' We configure the server structure
recvAddr.sin_family = AF_INET
recvAddr.sin_port = htons(256)
recvAddr.sin_addr.s_addr = inet_addr("127.0.0.1")
 
' We send the message
Print "Trying: Net send"
iResult = sendto(sendSocket, @sendBuf(0), bufLen, 0, Cptr(sockaddr Ptr, @recvAddr), Sizeof(recvAddr))
If (iResult = SOCKET_ERROR) Then
Beep: Print "Error: Net send"
closesocket(sendSocket)
WSACleanup()
Sleep
End
Else
Print "number of bytes send:"; iResult
End If
 
iResult = closeSocket(sendSocket)
If (iResult < 0) Then
Beep: Print "Error: Close socket"
End If
 
'closesocket(sock)
sock.closeWSACleanup()</syntaxhighlight>
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">s = newJava["java.net.Socket", ["localhost", 250256]]
w = new Writer[s.getOutputStream[]]
w.println["hello socket world"]
Line 1,088 ⟶ 1,162:
 
=={{header|Python}}==
<syntaxhighlight lang="python">import socket
"""Connect to a socket. Requires Python >= 3.2."""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
import socket
sock.connect(("localhost", 256))
 
sock.sendall("hello socket world")
sock =with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.close()</syntaxhighlight>
sock.connect(("localhost", 256))
sock.sendall(b"hello socket world")
</syntaxhighlight>
 
{{out}}
 
This example output uses netcat (<code>nc</code>) to provide a socket to connect to. Some versions of netcat require the <code>-p</code> (port) flag to be used in combination with <code>-l</code> (listen mode), some treat the combination of <code>-l</code> and <code>-p</code> as an error, and some don't mind either way. On some systems, you can use <code>sudo netstat -tulpn | grep nc</code> to show what port netcat is actually listening on.
 
256 is a "well-known", reserved port number. Binding to well-known ports usually requires elevated permissions. Hence the use of <code>sudo</code> here.
<pre>
$ sudo nc -l -p 256 & python sock.py
[3] 10559
hello socket world[3] Done sudo nc -l -p 256
</pre>
 
=={{header|R}}==
490

edits