Winsock Tutorial?

Windows specific questions.
Progger_X
Posts: 35
Joined: Jun 22, 2005 11:28
Contact:

Winsock Tutorial?

Postby Progger_X » Jun 22, 2005 12:21

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 :-(
mjs
Site Admin
Posts: 842
Joined: Jun 16, 2005 19:50
Location: Germany

Postby mjs » Jun 22, 2005 20:19

Hmm ... the Platform SDK from Microsoft is a good source. But you must know C.

I haven't developed any networking applications with FB yet, sorry.

Regards,
Mark
Ryan
Posts: 695
Joined: Jun 10, 2005 2:13
Location: Louisville, KY
Contact:

Postby Ryan » Jun 25, 2005 2:43

I could write one. Anything specific about Winsock you want to know?
marzec
Posts: 225
Joined: Jun 06, 2005 23:17

Postby marzec » Jun 25, 2005 14:32

i would suggest to write something on sockets in general ( that is berkley sockets ) as the interface is 1:1 to the winsock interface. the only difference between winsocks and berkley sockets are the WSA functions. by doing so the code can be used in win and linux
aetherFox
Posts: 100
Joined: Jun 23, 2005 16:48

Postby aetherFox » Jun 25, 2005 15:17

I could use this. How about a tutorial from the ground up? Cover everything about the basics, but gear it towards creating multiplayer games?
Ryan
Posts: 695
Joined: Jun 10, 2005 2:13
Location: Louisville, KY
Contact:

Postby Ryan » Jun 25, 2005 20:28

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.
D.J.Peters
Posts: 8125
Joined: May 28, 2005 3:28
Contact:

Postby D.J.Peters » Jun 25, 2005 23:36

This is from FreeBASIC Forum on qbasicnews.com
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.
whitetiger0990
Posts: 92
Joined: May 27, 2005 12:34
Location: Universe, Milky Way, Sol III, North-East Hemisphere, North America, USA, ., Chair
Contact:

Postby whitetiger0990 » Jun 26, 2005 0:21

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.
aetherFox
Posts: 100
Joined: Jun 23, 2005 16:48

Postby aetherFox » Jun 26, 2005 10:15

How do you make a peer-to-peer connection as opposed to a server-client connection?
whitetiger0990
Posts: 92
Joined: May 27, 2005 12:34
Location: Universe, Milky Way, Sol III, North-East Hemisphere, North America, USA, ., Chair
Contact:

Postby whitetiger0990 » Jun 26, 2005 13:54

aetherFox wrote:How do you make a peer-to-peer connection as opposed to a server-client connection?

Basically the same thing ^^;;

the connection is the same. I beleive.
MystikShadows
Posts: 612
Joined: Jun 15, 2005 13:22
Location: Upstate NY
Contact:

Postby MystikShadows » Jun 26, 2005 14:35

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.
D.J.Peters
Posts: 8125
Joined: May 28, 2005 3:28
Contact:

Postby D.J.Peters » Jun 26, 2005 15:03

(sorry bad english)
Hi aetherFox,
if you have understood Server/Client connections than you can write Peer to Peer too.
Your program is than an server and client too.

Joshy
peer to peer all can akt as an server too.
Image
aetherFox
Posts: 100
Joined: Jun 23, 2005 16:48

Postby aetherFox » Jun 26, 2005 17:25

Hehe the diagram said uber :D

I don't really know Winsock too well anyway...I tried to figure it out in VB but found better things to waste my time programming.
Ryan
Posts: 695
Joined: Jun 10, 2005 2:13
Location: Louisville, KY
Contact:

Postby Ryan » Jun 27, 2005 3:28

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.

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 2 guests