Tutorial for using WinSock
-
- Posts: 1
- Joined: Oct 25, 2011 21:00
Tutorial for using WinSock
Okay I have been using Free Basic for a while but I would like to incorporate WinSock into my programs. I want to use a client to server connection and the few tutorials I found for this were incredibly old and when I ran them gave me errors. So if anyone could link me to a tutorial or explain in the topic the different functions and show me some source code it would be greatly appreciated. TIA :)
Search for FBNET or Chisock. You can find a thread here http://www.freebasic.net/forum/viewtopic.php?t=3367
Walter
Walter
Ok. Let's assume that I'm a complete novice at FreeBasic Winsock, that I know nothing about what it is, or how it works.
That's me. (I am a professional network engineer though so I understand the framework that the winsock has to operate in/relate to.)
Like Glorfindel22, I can do some things in FB, (create arrays, fill them with data, graph the data to the screen and save it to disk), maybe not very prettily, efficiently, or elegantly, but I can get it to do what I want it to.
I've followed the link below, searched and read as many of the other Forum links as I can find, and I'm beginning to feel like I'm a moron.
(I also can't find the Chisock library which everyone keeps mentioning and seems fairly essential.)
I think I'm missing some VERY basic knowledge, a plateau if you like, to get me to the level where I can start to understand all the website links, and "play" in a meaningful self-educating manner.
Could someone please point me at some resource, (Winsock 101, FB Winsock for Dummies, etc), or help me in those first steps to understanding it all?
Pretty please?
Thanks in anticipation,
Andy.
That's me. (I am a professional network engineer though so I understand the framework that the winsock has to operate in/relate to.)
Like Glorfindel22, I can do some things in FB, (create arrays, fill them with data, graph the data to the screen and save it to disk), maybe not very prettily, efficiently, or elegantly, but I can get it to do what I want it to.
I've followed the link below, searched and read as many of the other Forum links as I can find, and I'm beginning to feel like I'm a moron.
(I also can't find the Chisock library which everyone keeps mentioning and seems fairly essential.)
I think I'm missing some VERY basic knowledge, a plateau if you like, to get me to the level where I can start to understand all the website links, and "play" in a meaningful self-educating manner.
Could someone please point me at some resource, (Winsock 101, FB Winsock for Dummies, etc), or help me in those first steps to understanding it all?
Pretty please?
Thanks in anticipation,
Andy.
I recommend TSNE. Have a look at this thread: http://www.freebasic.net/forum/viewtopic.php?t=18684
Hi MOD, thanks for the prompt reply.
I have to agree with darkhog sentiments:
I'm having huge difficulty in finding any info that tells me what I have to do to understand how FB "gets it all together". It's no use being shown a load of examples if I don't understand what I have to include and why.
As a network engineer, I know what the network is expecting, but how I get FB to interface to those needs is beyond me.
Why do I need chisock, or GIO, or TSNE?
There is some promising help here:
http://www.freebasic.net/forum/viewtopic.php?t=3367 but it doesn't explain if/what I need (chisock, or GIO, or TSNE, something else), and what I have to do about making it available.
Sorry if I sound massively thick, but I need to understand some very basic-level info.
Apologies,
Andy.
I have to agree with darkhog sentiments:
I don't read/speak German either.I never seen any easy to use for people with little or no network programming knowledge wrapper. I don't care what it will have "under the hood", it can be sdl_net, GIO or any other lib (though GIO will be problem for windows (l)users). I just want some functions with self-explanatory names like in sample code I presented in the last post.
I don't need to understand network programming. I just need to be able to use it.
I'm having huge difficulty in finding any info that tells me what I have to do to understand how FB "gets it all together". It's no use being shown a load of examples if I don't understand what I have to include and why.
As a network engineer, I know what the network is expecting, but how I get FB to interface to those needs is beyond me.
Why do I need chisock, or GIO, or TSNE?
There is some promising help here:
http://www.freebasic.net/forum/viewtopic.php?t=3367 but it doesn't explain if/what I need (chisock, or GIO, or TSNE, something else), and what I have to do about making it available.
Sorry if I sound massively thick, but I need to understand some very basic-level info.
Apologies,
Andy.
I did some things with winsock and socket select, maybe usefull, see:
http://www.freebasic.net/forum/viewtopi ... c&start=14
http://www.freebasic.net/forum/viewtopi ... c&start=14
Thanks for that badidea. I think I'm slowly starting to understand how to ask about what I don't understand (does that make sense?).
I think the problem I'm having is understanding which file? library? to include together with which commands they'll give me to use for network communication. Does this make sense?
For instance, what's the difference between the FB-supplied winsock, chisock (if you can get hold of it anywhere), TSNE and GIO, and why is one better than another? Surely there's a limited number of ways these can do the same job?
I've also only just yesterday discovered the difference between blocking and non-blocking sockets.
I really do need a socket wetnurse <sigh>.
I think the problem I'm having is understanding which file? library? to include together with which commands they'll give me to use for network communication. Does this make sense?
For instance, what's the difference between the FB-supplied winsock, chisock (if you can get hold of it anywhere), TSNE and GIO, and why is one better than another? Surely there's a limited number of ways these can do the same job?
I've also only just yesterday discovered the difference between blocking and non-blocking sockets.
I really do need a socket wetnurse <sigh>.
Hi fixit9660,
I am no expert on this, but this is my understanding:
The winsock functions are part of the Windows operating system. (see: http://msdn.microsoft.com/en-us/library ... 85%29.aspx) To use these functions from FreeBASIC, you need to include "win/winsock2.bi" in your code. This file should be somewhere in your FreeBASIC program folder. Something like /freebasic/inc/win. For linux there is no winsock (obviously), but by including some other files in your code, the network function calls are almost the same (as you can see in my example).
With the winsock functions you can do anything you like to do, but code you be a bit complicated. Therefore, others have made a code layer on top of the winsock functions to make things easier (e.g.: chisock, TSNE). I preferred to use the socket functions directly too get better knowledge of the underlying structure.
I hope this helps. I'll try to post a more simple example here in a few minutes.
edit: And yes, when I learned about blocking, I decided to go for the select functions. No need to block then.
edit: Best to run the example above: run "server" and "client" and from the command line. Each in its own command line terminal. On the same PC . Start "server first". If you get a bind error, then you already started the server or something else is wrong.
I am no expert on this, but this is my understanding:
The winsock functions are part of the Windows operating system. (see: http://msdn.microsoft.com/en-us/library ... 85%29.aspx) To use these functions from FreeBASIC, you need to include "win/winsock2.bi" in your code. This file should be somewhere in your FreeBASIC program folder. Something like /freebasic/inc/win. For linux there is no winsock (obviously), but by including some other files in your code, the network function calls are almost the same (as you can see in my example).
With the winsock functions you can do anything you like to do, but code you be a bit complicated. Therefore, others have made a code layer on top of the winsock functions to make things easier (e.g.: chisock, TSNE). I preferred to use the socket functions directly too get better knowledge of the underlying structure.
I hope this helps. I'll try to post a more simple example here in a few minutes.
edit: And yes, when I learned about blocking, I decided to go for the select functions. No need to block then.
edit: Best to run the example above: run "server" and "client" and from the command line. Each in its own command line terminal. On the same PC . Start "server first". If you get a bind error, then you already started the server or something else is wrong.
More simple example:
- Berkeley sockets (basis for winsock)
- UDP (no TCP/IP)
- Only one way messaging
- Only one message
- Runs on WindowsXP and Unbuntu 10.04 64-bit
- Only tested locally.
receiver.bas
sender.bas
Running:
- Compile both programs first
- Start "receiver(.exe)"
- Start "senser(.exe)"
- Show is over, 1024 useless bytes transmitted
- Berkeley sockets (basis for winsock)
- UDP (no TCP/IP)
- Only one way messaging
- Only one message
- Runs on WindowsXP and Unbuntu 10.04 64-bit
- Only tested locally.
receiver.bas
Code: Select all
#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
#include "fbgfx.bi"
#define NET_BUFLEN 1024
'--- RECEIVER ---
#If Defined(__FB_WIN32__)
dim as WSADATA wsaData
#EndIf
dim as SOCKET recvSocket
dim as sockaddr_in recvAddr
dim as integer port = 27015
dim as ubyte recvBuf(NET_BUFLEN-1)
dim as integer bufLen = NET_BUFLEN
dim as sockaddr_in senderAddr
dim as integer senderAddrSize = sizeof(senderAddr)
dim as integer iResult, quit = 0
#If Defined(__FB_WIN32__)
iResult = WSAStartup(MAKEWORD(2,2), @wsaData)
if (iResult <> 0) then
beep: print "Error: Winsock init"
sleep
end
end if
#EndIf
'UDP/IP -> No listen, No accept
recvSocket = socket_(AF_INET, SOCK_DGRAM, IPPROTO_UDP) 'IPv4, UDP
if (recvSocket < 0) then
beep: print "Error: Net socket"
#If Defined(__FB_WIN32__)
WSACleanup()
#EndIf
end
end if
recvAddr.sin_family = AF_INET
recvAddr.sin_port = htons(port)
recvAddr.sin_addr.s_addr = htonl(INADDR_ANY)
iResult = bind(recvSocket, cptr(sockaddr ptr, @recvAddr), sizeof(recvAddr))
if (iResult < 0) then
beep: print "Error: Net bind"
#If Defined(__FB_WIN32__)
WSACleanup()
#EndIf
sleep
end
end if
print "Trying: Net recv"
iResult = recvfrom(recvSocket, @recvBuf(0), bufLen, 0, cptr(sockaddr ptr, @senderAddr), @senderAddrSize)
print "number of bytes recived:"; iResult
iResult = closesocket(recvSocket)
if (iResult < 0) then
beep: print "Error: Close socket"
end if
#If Defined(__FB_WIN32__)
WSACleanup()
#EndIf
print "End."
sleep
Code: Select all
#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
#include "fbgfx.bi"
#define NET_BUFLEN 1024
'--- SENDER ---
#If Defined(__FB_WIN32__)
dim as WSADATA wsaData
#EndIf
dim as SOCKET sendSocket
dim as sockaddr_in recvAddr
dim as integer port = 27015
dim as ubyte sendBuf(NET_BUFLEN-1)
dim as integer bufLen = NET_BUFLEN
dim as integer iResult, quit = 0
#If Defined(__FB_WIN32__)
iResult = WSAStartup(MAKEWORD(2,2), @wsaData)
if (iResult <> 0) then
beep: print "Error: Winsock init"
sleep
end
end if
#EndIf
sendSocket = socket_(AF_INET, SOCK_DGRAM, IPPROTO_UDP) 'IPv4, UDP
if (sendSocket < 0) then
beep: print "Error: Net socket"
#If Defined(__FB_WIN32__)
WSACleanup()
#EndIf
end
end if
recvAddr.sin_family = AF_INET
recvAddr.sin_port = htons(port)
recvAddr.sin_addr.s_addr = inet_addr("127.0.0.1")
print "Trying: Net send"
iResult = sendto(sendSocket, @sendBuf(0), bufLen, 0, cptr(sockaddr ptr, @recvAddr), sizeof(recvAddr))
if (iResult < 0) then
beep: print "Error: Net send"
closesocket(sendSocket)
#If Defined(__FB_WIN32__)
WSACleanup()
#EndIf
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
#If Defined(__FB_WIN32__)
WSACleanup()
#EndIf
print "End."
sleep
- Compile both programs first
- Start "receiver(.exe)"
- Start "senser(.exe)"
- Show is over, 1024 useless bytes transmitted
Read the thread, I translated the example and it almost can't be done easier.I don't read/speak German either.
You don't need chisock or TSNE but it's better because you don't have to use the low level API for network connections. I prefer TSNE, but you can use what you want. TSNE is easy to use, proven in many projects and works without any changes on Windows and Linux.