Sockets: Difference between revisions
Content deleted Content added
Frink |
m →{{header|Frink}}: Fixed port number |
||
(6 intermediate revisions by 4 users not shown) | |||
Line 26:
tcpip_connect(i, o, "127.0.0.1", 256, 0);
i.text("hello socket world");</syntaxhighlight>
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">socket: connect.to:"localhost" 256
send socket "Hello Socket World"
unplug socket</syntaxhighlight>
=={{header|AutoHotkey}}==
Line 213 ⟶ 218:
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
const char *msg = "hello socket world";
Line 464 ⟶ 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
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)
WSACleanup()</syntaxhighlight>
=={{header|Frink}}==
<syntaxhighlight lang="frink">s = newJava["java.net.Socket", ["localhost",
w = new Writer[s.getOutputStream[]]
w.println["hello socket world"]
Line 1,083 ⟶ 1,162:
=={{header|Python}}==
<syntaxhighlight lang="python">
"""Connect to a socket. Requires Python >= 3.2."""
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
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}}==
Line 1,393 ⟶ 1,486:
{{trans|C}}
An embedded program so we can ask the C host to call the relevant library functions for us.
Although this worked when originally posted, the connection is now refused. The same applies to the C, Go, Phix, Python and Ruby entries and probably others.
<syntaxhighlight lang="wren">/* Sockets.wren */
var AF_UNSPEC = 0
Line 1,458 ⟶ 1,553:
pm = pm[slen..-1]
}
} else if (stat == -1) {
System.print("Connection refused.")
}
var status = Socket.close(sock)
Line 1,466 ⟶ 1,563:
<br>
Now embed this script in the following C program, compile and run it.
<syntaxhighlight lang="c">
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 1,666 ⟶ 1,765:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 1,685 ⟶ 1,784:
{{out}}
<pre>
$ sudo nc -l 256 & ./Sockets
[23] 6074
Connection refused.
</pre>
|