75 lines
1.9 KiB
Plaintext
75 lines
1.9 KiB
Plaintext
#If Defined(__FB_WIN32__)
|
|
#Include Once "win/winsock2.bi"
|
|
#ElseIf Defined(__FB_LINUX__)
|
|
#Include Once "crt/netdb.bi"
|
|
#Include Once "crt/sys/socket.bi"
|
|
#Include Once "crt/netinet/in.bi"
|
|
#Include Once "crt/arpa/inet.bi"
|
|
#Include Once "crt/unistd.bi"
|
|
#Include Once "crt/sys/select.bi"
|
|
#Else
|
|
#Error Platform Not supported
|
|
#EndIf
|
|
|
|
Type SOCKET As Ulongint
|
|
|
|
Const NET_BUFLEN = 1024
|
|
Const PORT = 256
|
|
Const HOST = "127.0.0.1"
|
|
|
|
' Initialize variables
|
|
#If Defined(__FB_WIN32__)
|
|
Dim As WSADATA wsaData
|
|
#EndIf
|
|
Dim As SOCKET sendSocket
|
|
Dim As sockaddr_in recvAddr
|
|
Dim As String message = "hello socket world"
|
|
Dim As Ubyte sendBuf(NET_BUFLEN-1)
|
|
Dim As Integer bufLen = Len(message)
|
|
|
|
' Copy message to buffer
|
|
For i As Integer = 1 To bufLen
|
|
sendBuf(i-1) = Cbyte(Asc(Mid(message, i, 1)))
|
|
Next
|
|
|
|
' Initialize Winsock
|
|
If WSAStartup(MAKEWORD(2,2), @wsaData) = 0 Then
|
|
' Create socket
|
|
sendSocket = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0)
|
|
|
|
If sendSocket <> INVALID_SOCKET Then
|
|
' Configure server address
|
|
With recvAddr
|
|
.sin_family = AF_INET
|
|
.sin_port = htons(PORT)
|
|
.sin_addr.s_addr = inet_addr(HOST)
|
|
End With
|
|
|
|
' Send message
|
|
Print "Sending message..."
|
|
Dim As Integer bytesSent = sendto(sendSocket, @sendBuf(0), bufLen, 0, Cast(sockaddr Ptr, @recvAddr), Sizeof(sockaddr_in))
|
|
|
|
If bytesSent <> SOCKET_ERROR Then
|
|
Print "Bytes sent: "; bytesSent
|
|
Else
|
|
Print "Error sending message"
|
|
End If
|
|
|
|
' Close socket
|
|
#If Defined(__fb_win32__)
|
|
closesocket(sendSocket)
|
|
#Else
|
|
Close(sendSocket)
|
|
#EndIf
|
|
Else
|
|
Print "Error creating socket"
|
|
End If
|
|
|
|
' Cleanup Winsock
|
|
#If Defined(__fb_win32__)
|
|
WSACleanup()
|
|
#EndIf
|
|
Else
|
|
Print "Error initializing Winsock"
|
|
End If
|