Winsock Tutorial?
Winsock Tutorial?
Hi.
I want to learn how to use Winsock. Does anybody know where i´m able to get a tutorial? I´ve searched, but i found only c++ tutorials or something i couldn´t unterstand :-(
I want to learn how to use Winsock. Does anybody know where i´m able to get a tutorial? I´ve searched, but i found only c++ tutorials or something i couldn´t unterstand :-(
I'll start working on it, though it's release is pending on when I get my game working. ;-) Actually, I don't know all the ins and outs of sockets, but I've used Winsock controls in VB to make a multi-player game, and I have several tests on my drive right now that all work great. As a beginner's tutorial, I'm sure it would work just fine.
Hmm.. realized the way I'm doing the multiplayer game is with a server and clients, though I believe my theory would work the same for a peer to peer game.
Hmm.. realized the way I'm doing the multiplayer game is with a server and clients, though I believe my theory would work the same for a peer to peer game.
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
This is from FreeBASIC Forum on qbasicnews.com
Member: whitetiger0990 ;-)
all a nice time.
Joshy
Member: whitetiger0990 ;-)
all a nice time.
Joshy
Code: Select all
option escape
'$include: "win/winsock.bi"
declare function wsConnect (s as socket, host as string, port as integer)
declare function wsResolveHost ( hostname as string ) as integer
declare function wsGetData ( s as socket ) as string
declare function wsSendData ( s as socket, sendbuffer as string)
declare function wsInit (s as socket)
declare function wsKill (s as socket)
declare function wsListen (s as socket, port as integer)
declare function wsAccept (s as socket)
const RECVBUFFLEN = 8192
const NEWLINE = chr$(13)+chr$(10)'"\r\n"
function wsAccept(s as socket)
dim acceptsocket as socket
AcceptSocket = SOCKET_ERROR
while ( AcceptSocket = SOCKET_ERROR )
AcceptSocket = accept( s, 0, 0)
wend
end function
function wsListen (s as socket, port as integer)
dim sa as sockaddr_in
sa.sin_port = htons(port)
sa.sin_family = AF_INET
sa.sin_addr.S_addr = inet_addr( "127.0.0.1" )
bind( s, @sa, sizeof(sa) )
listen( s, 1 )
end function
function wsKill(s as socket)
shutdown(s, 2)
closesocket(s)
WSACleanup
end function
function wsInit(s as socket)
dim wsaData as WSAData
WSAStartup( MAKEWORD( 1, 1 ), @wsaData )
s = opensocket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
end function
function wsSendData(s as socket, sendbuffer as string)
send(s, strptr(sendBuffer), len(sendBuffer),0)
end function
function wsGetData(s as socket) as string
dim recvbuffer as zstring * RECVBUFFLEN+1
dim bytes as integer
bytes = recv(s, strptr(recvBuffer), RECVBUFFLEN, 0)
recvbuffer[bytes] = 0
if recvbuffer = "" then
wsgetdata = trim$(str$(bytes))
else
wsgetData = recvbuffer
end if
end function
function wsGetAllData(s as socket) as string
dim recvbuffer as zstring * RECVBUFFLEN+1
dim bytes as integer
while (bytes <> socket_error)
bytes = recv(s, strptr(recvBuffer), RECVBUFFLEN, 0)
if ( bytes = 0 or bytes = WSAECONNRESET ) then
exit while
end if
wend
recvbuffer[bytes] = 0
if recvbuffer = "" then
wsgetalldata = trim$(str$(bytes))
else
wsgetalldata = recvBuffer
end if
end function
function wsResolveHost( hostname as string ) as integer
dim ia as in_addr
dim hostentry as hostent ptr
ia.S_addr = inet_addr( hostname )
if ( ia.S_addr = INADDR_NONE ) then
hostentry = gethostbyname( hostname )
if (hostentry = 0) then exit function
wsresolveHost = **hostentry->h_addr_list
else
wsresolveHost = ia.S_addr
end if
end function
function wsConnect(s as socket, host as string, port as integer)
dim connection as sockaddr_in
connection.sin_port = htons(port)
connection.sin_family = AF_INET
connection.sin_addr.S_addr = wsresolveHost(host)
connect(s, @connection, len(connection))
end function
Code: Select all
'winsockclient.bas
option explicit
option escape
#include "winsockutil.bas"
cls
dim s as socket
print "Initializing..."
wsInit(s)
print "Connecting..."
wsConnect(s,"127.0.0.1", 9090)
print "Recieving..."
dim moo as string
moo = wsGetallData(s)
print moo
print "Killing..."
wsKill(s)
sleep
Code: Select all
'winsockserver.bas
option explicit
option escape
#include "winsockutil.bas"
cls
dim s as socket
print "Initializing..."
wsInit(s)
print "Listening..."
wsListen (s, 9090)
print "Accepting..."
wsAccept (s)
print "Sending..."
wsSendData (s, "moo23wd")
print "Killing..."
wsKill(s)
sleep
Last edited by D.J.Peters on Jun 26, 2005 12:18, edited 2 times in total.
-
- Posts: 92
- Joined: May 27, 2005 12:34
- Location: Universe, Milky Way, Sol III, North-East Hemisphere, North America, USA, ., Chair
- Contact:
My preeeeecious!!!
Hehe.
That's.. hehe. mine
http://forum.qbasicnews.com/viewtopic.p ... 603#109603
So nyeeeh
EDIT: that reminds me. I need to redo that.
Hehe.
That's.. hehe. mine
http://forum.qbasicnews.com/viewtopic.p ... 603#109603
So nyeeeh
EDIT: that reminds me. I need to redo that.
-
- Posts: 92
- Joined: May 27, 2005 12:34
- Location: Universe, Milky Way, Sol III, North-East Hemisphere, North America, USA, ., Chair
- Contact:
-
- Posts: 612
- Joined: Jun 15, 2005 13:22
- Location: Upstate NY
- Contact:
yup, virtual private networking helped in making all connections operate the same way....all windows/linux based PCs and others have an IP address now, and you connect to that IP address through a given port, if both side agree to that, you have a peer to peer connection. :-) hence both sides need to enable the port.
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Yeah, I was just referring to instead of a multiplayer game where it's just two player capable, having a bit of code to where one person can open a socket that will accept connections from many different computers so maybe 1 person could host a game that 8 people can join. You just need to keep track of which socket is connected to who and handle all their input/output appropriately. I really like the idea of using a separate thread for each connection that ....
my friend's cat just about ripped my knuckle off because it fell off my lap while swatting at the mouse pointer.. and dug into my hand with it's baby sharp claws
... It hurts. Anyways, each thread handles its own socket and closes it up when the client program closes or drops link. I think vic recommended this.. or someone to me in another thread.
my friend's cat just about ripped my knuckle off because it fell off my lap while swatting at the mouse pointer.. and dug into my hand with it's baby sharp claws
... It hurts. Anyways, each thread handles its own socket and closes it up when the client program closes or drops link. I think vic recommended this.. or someone to me in another thread.