Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Winsock tutorial?
#1
I am trying to learn a bit of network programming. I have found tutorials but they are all C or C++.

I am using them, but not very well. Are they any FreeBASIC ones or can someone port one?

http://beej.us/guide/bgnet/output/htmlsingle/bgnet.html

This is the one i am TRYING to use.
url=http://www.sloganizer.net/en/][Image: style4,TheDarkJay.png][/url]
Reply
#2
this is the one that is the best you'll find
quote="NecrosIhsan"]
[Image: yagl1.png]
[/quote]
Reply
#3
k, but are they any FB ones or plans to port ones to FB?
url=http://www.sloganizer.net/en/][Image: style4,TheDarkJay.png][/url]
Reply
#4
Using something someone posted i managed to cut it up and make these. Would they work? I haven't had a chance to test it:

Client:

Code:
dim addy as String
DIM wsad as WSAData
CONST NEWLINE = "\r\n"

common shared inmessage As zString*316, outmessage As zString *316
DIM sa AS sockaddr_in, sb AS sockaddr_in,
DIM sc AS sockaddr_in, sd AS sockaddr_in

DIM inip As Integer
inip = sizeof(sc)
DIM fdset As fd_set

DIM MyName AS String

dim hostname as string * 256
dim hostentry as HOSTENT ptr

if (WSAStartup(&h202, @wsad))= -1 then
      if (WSAStartup(&h101, @wsad))= -1 then errorhandler
end if

Input "Enter remote dot-format IP ADDRESS to connect: ",addy
addy=Trim$(addy)

MyName = "Client"

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

s = opensocket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
if s <= 0  Then errorhandler

PRINT "Loggin In........"

sa.sin_port         = htons( 8501 )
   sa.sin_family      = AF_INET
   sa.sin_addr.S_addr   = inet_addr(addy)

do

'****STEP 3 for CLIENT MODE: Connect the socket to the remote host

ncon = Connect( s, @sa, Len( sa ) )
If ncon <> socket_error then exit Do

'*****************

if inkey$ = chr$(27) then try=31
try = try+1
if try > 30 then exit do
Loop
  
If try > 30 then
closesocket(s)
wsacleanup
Print "Failed to connect. Hit any key to quit."
sleep
END
end if

color 7,0


'***** CLIENT MODE now completed. Two-way connection established and ready.

outmessage = MyName
bsent = send (s, @outmessage,316,0)
if bsent= -1 then closesocket(s) : errorhandler

brec = recv(s,@inmessage,316,0)
If brec <= 0 Then closesocket(s) : errorhandler

PRINT inmessage

DO
key$ = INKEY$
LOOP UNTIL key$ = CHR$(27)

SUB errorhandler

? "There was a Error and the program could not continue."
? "Error trapping has not yet been implemented"
? "so what the error was is unknown"
? ""

? "However for your interest,"
? "Winsock said: ";
? WSAGetLastError

closesocket(s)
wsacleanup

?
?"Hit any key to exit the program..."
color 7,0
Sleep
END
END SUB

Server:

Code:
#include "win\winsock.bi"
option Escape

DECLARE SUB ErrorHandler

DIM wsad as WSAData
CONST NEWLINE = "\r\n"

common shared inmessage As zString*316, outmessage As zString *316
DIM sa AS sockaddr_in, sb AS sockaddr_in,
DIM sc AS sockaddr_in, sd AS sockaddr_in

DIM inip As Integer
inip = sizeof(sc)
DIM fdset As fd_set

DIM MyName AS String

dim hostname as string * 256
dim hostentry as HOSTENT ptr

if (WSAStartup(&h202, @wsad))= -1 then
      if (WSAStartup(&h101, @wsad))= -1 then errorhandler
end if

MyName = "Admin"

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

s = opensocket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
if s <= 0  Then errorhandler

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Print "Entering SERVER Mode..."
Print ""
Print ""
Print "Attempting to determine local IP address, one moment please..."
Print ""

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

GetHostName(@hostname, 256)
hostentry = gethostbyname( hostname )

dim ipparser as ubyte ptr

ipparser = hostentry->h_addr
myip$ = str(ipparser[0])+"."+str(ipparser[1])+"."+str(ipparser[2])+"."+str(ipparser[3])

q = opensocket( AF_INET, SOCK_STREAM, IPPROTO_TCP )

    sd.sin_port         = htons( 80 )
   sd.sin_family      = AF_INET
   sd.sin_addr.S_addr  = inet_addr("63.208.196.105")

if ( connect( q, @sd, len( sd )) = SOCKET_ERROR ) then closesocket( q )

outmessage = "GET /" + " HTTP/1.0" + NEWLINE + "Host: " +  NEWLINE +"Connection: close" + NEWLINE + "User-Agent: GetHTTP 0.0" + NEWLINE + NEWLINE
              
if ( send( q, @outmessage , len(outmessage), 0 )) = SOCKET_ERROR  then closesocket( q )
    
do
       bytes = recv( q, @inmessage , 316, 0 )
       if ( bytes <= 0 ) then exit do
       wholemessage$ = wholemessage$ + inmessage
loop

closesocket(q)

beg=instr(wholemessage$,"Address: ")
en=instr(wholemessage$,"</body>")

altip$=mid$(wholemessage$,beg+9,(en-1)-(beg+8))
if val(altip$) > 0 and ( val(altip$) - int (val(altip$)) ) > 0 and altip$<>myip$ then myip$ = altip$

color 15,0

if val(myip$) > 0 then

print "Your Local IP Address Is: ";
color 10,0 : Print trim(myip$) : color 15,0:Print ""

Print "Remote party should enter that address after selecting ";
color 10,0: Print "Client Mode.":color 15,0:Print ""    

else
color 15,0
Print "Your Local IP Address could not be determined automatically. Please use an"
Print "appropriate Internet Web Site or other service to determine your Local IP."
Print ""

end if
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
   sa.sin_port                 = htons( 8501 )
   sa.sin_family              = AF_INET
   sa.sin_addr.S_addr  = inaddr_any

   nbit = bind( s, @sa, len(sa))
   if nbit=socket_error Then closesocket(s) : ErrorHandler
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
nlis= listen( s, somaxconn)
if nlis = SOCKET_ERROR  Then closesocket( s ): errorhandler
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
do
if inkey$ = chr$(27) then esclause = 1:exit do
fdset.fd_count=1
fdset.fd_array(0)=s

nRet = selectsocket( 3, @fdset, NULL, NULL, @tv )
if nret > 0 then exit do
sleep 1000
loop

if esclause=1 then esclause=0:closesocket (s)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
r = accept( s, null, null )                  
If    r = socket_error Then closesocket(r) : closesocket(s) : errorhandler
swap s,r
Closesocket(r)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



outmessage = myname$
bsent = send (s, @outmessage,316,0)
if bsent= -1 then closesocket(s) : generr

brec = recv(s,@inmessage,316,0)
If brec <= 0 Then closesocket(s) : generr
remotename$ = Trim$(inmessage)

'Get remote IP Address

gpn = getpeername(s,@sc,@inip)
If gpn <> -1 then
ipparser = @sc.sin_addr.s_addr
remoteip$ = str(ipparser[0])+"."+str(ipparser[1])+"."+str(ipparser[2])+"."+str(ipparser[3])
End if

Cls

Print  remotename$ + " ("+ remoteip$ +")"  + " Has Logged In"

outmessage = "You have succesfully logged in the surver."

bsent = send (s, @outmessage,316,0)
if bsent= -1 then closesocket(s) : errorhandler

DO
k$ = INKEY$
If k$ = CHR$(27) THEN
closesocket(s)
wsacleanup
END
LOOP

SUB errorhandler

? "There was a Error and the program could not continue."
? "Error trapping has not yet been implemented"
? "so what the error was is unknown"
? ""

? "However for your interest,"
? "Winsock said: ";
? WSAGetLastError

closesocket(s)
wsacleanup

?
?"Hit any key to exit the program..."
color 7,0
Sleep
END
END SUB
url=http://www.sloganizer.net/en/][Image: style4,TheDarkJay.png][/url]
Reply
#5
Those snipets are from my FreeBASIC Winsock Chat Program, which is designed to in fact be a tutorial for beginners. You can find my newer and better revised version of the code under the post: "Revised FreeBasic Winsock Tutorial/Chat Program" in the FB Projects forum.
Reply
#6
i tested them and they work fine (i had to access my own IP but still...)

Thanks from all wannabe-network programmers
url=http://www.sloganizer.net/en/][Image: style4,TheDarkJay.png][/url]
Reply
#7
You're most welcome... I had to do most of my testing with localhost connections as well until the latter stages, but it works just as well across "real" internet traffic.
Reply
#8
Darn, I need 0.15 of FB for your new version and FreeBASIC.net is DOWN!

But i know that the one i made works (i had to cut off a , or two and stuff but it works)
url=http://www.sloganizer.net/en/][Image: style4,TheDarkJay.png][/url]
Reply
#9
All of the FreeBASIC downloads (including unstable releases) are available at the SourceForge project page: http://www.sourceforge.net/projects/fbc
Reply
#10
Thanks...hmm, i can't compile, i get a no such directory thing.
url=http://www.sloganizer.net/en/][Image: style4,TheDarkJay.png][/url]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)