Qbasicnews.com

Full Version: Request help with BASIC-Winsock programming
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Currently I'm trying to program a very, very simple test program that will just open a socket, listen on a port, and accept a connection. I'm learning the BASIC-Winsock syntax completely by trial and error, since there appears to be no documentation on it anywhere on the net. C yes, Basic no.

My server program is below, and seems to open the socket and listen just fine, but when I try to tell it to accept, it always returns an invalid socket. I'm sure my syntax is way off since I'm only making wild guesses at it. Can someone tell me what I'm doing wrong?



Code:
#include "win\winsock.bi"
#include "win\kernel32.bi"
option escape

const wsver1 = &H101
const wsver2 = &H202
dim wsastartupversion as integer
dim wsad as WSAData

wsastartupversion = wsver2
WSAStartup(wsastartupversion, @wsad)


dim s as SOCKET
    s = opensocket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
    if( s = 0 ) then
      print "Error:"; WSAGetLastError; " Calling: socket()"
      end 1
   end if
  
  
dim sa as sockaddr_in
  
   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
    print "Error:"; WSAGetLastError; " Calling: connect()"
   closesocket( s )
end 2
end if

nlis= listen( s, somaxconn)
      if nlis = SOCKET_ERROR  then
        print "Error:"; WSAGetLastError; " Calling: connect()"
      closesocket( s )
        end 3
   end if

print "Accepting... (press any key to exit)"
      
do while ( len( inkey$ ) = 0 )
         nacc = accept( s, @sa, len( sa ) )          
         if ( nacc = INVALID_SOCKET ) then
            print "Error: accept()"; WSAGetLastError
            nsel = closesocket( s )
            end 4
            elseif nacc > -1 then
                input"connected I think...",ci$
            end if  
        loop
  


   WSACleanup

Thanks for any help...
http://msdn.microsoft.com/library/defaul...page_2.asp

or your could use SDLNet and look at all the pretty docs they have for that =P (I can't test you're code because I'm running linux.)
Thanks for your reply. That site has good information about Winsock commands in general, but not the BASIC syntax for using them. Currently I am stuck at the point where I need to create a continuous loop to check for incoming connections. I don't know how to do that, as in which winsock command I should use to "check".

"Accept" always returns a socket error, and "listen" doesn't seem to be enough, so I'm missing a command somewhere in between to "check" for a connection request.
Quote: That site has good information about Winsock commands in general, but not the BASIC syntax for using them.
xD
I almost died laughing.
That "site" is Microsoft.com.

You probably won't find winsock stuff for BASIC... I mean.. why would you? what is BASIC used for that makes it worthy for winsock docs?


Learn to at least "read" C. It's not that hard.
I do understand the general notation of C, and how it's using the winsock calls to deal with sockets. But, I am missing a link in understanding how the "middle step" is done. After opening the socket, binding it, and making it listen successfully, which I can do, the next step as described on "that site" is to create a continuous loop to check for incoming connections. The C code they use is this:

Code:
SOCKET AcceptSocket;


printf( "Waiting for a client to connect...\n" );
while (1) {
    AcceptSocket = SOCKET_ERROR;
    while ( AcceptSocket == SOCKET_ERROR ) {
        AcceptSocket = accept( m_socket, NULL, NULL );
    }

So how does that translate into basic? I don't get it. If I just DIM Acceptsocket as socket, it will be a socket, sure, but why would an incoming connection attempt be detected on THAT one, since the one I bound my specified incoming connection port to is a different socket altogether. How is this temporary socket, created on the fly, going to be the one that detects the connection attempt? It has no port attached to it.

If I just use the original socket that the port is bound to, and try to use ACCEPT, it returns a socket_error (-1) all the time, always. That's what I don't know how to do. Get it to "check" a socket to see if there's an incoming connection queued and waiting first, before trying to futilely use ACCEPT.

Presumably the server's got to be up and running already before the client tries to connect, so what's the point of trying to call ACCEPT upon startup, when no incoming connection has even been received yet and it's just going to produce a socket error?
Code:
dim AcceptSocket as socket
print "Waiting for a client to connect..."
do
    AcceptSocket = SOCKET_ERROR
    do while ( AcceptSocket = SOCKET_ERROR )
        AcceptSocket = accept( m_socket, NULL, NULL )
    loop
loop
I believe (from memory)
according to that code you already have a socket named "m_socket" which is bond to a port and listening.

edit: small mistake in code
Thank you again. That code above is pretty much what I tried in Basic. Could you explain to me what the mathematical relational operator "==" (double equal sign) means? Is it the same as NOT EQUAL (<>) ?

If so, why would AcceptSocket ever NOT EQUAL a socket error, if there is never anything connected to it?

Also why are we using NULL, NULL instead of the sockaddr_in we want to store the info in and the LEN (of that sockaddr_in) ?

Sorry I'm such a newbie at this.
"==" is comparing. In basic it's the same as "=". if you do a "=" in C it sets the varible.

about the NULLs, I dunno.
Ah okay, thanks. So how will that code ever break out of the larger DO LOOP? Even that the point where AcceptSocket suddenly doesn't equal a socket_error anymore, and it breaks out of the small do loop, what gets it out of the larger DO LOOP?

On another note, let me see if I understand this in plain English. The program open a socket, binds it, then makes it listen..

then it just keeps checking over and over again to see whether calling ACCEPT on that bound socket produces an error. As long as it keeps getting an error, it just keeps checking over and over again. At the point where it suddenly doesn't get an error anymore, it gets out of the loop and moves on with the program.

Correct..?
No that program
1. Opens a socket
2. Checks on a socket (m_socket) which is already binded and listening (this is NOT in that code. This code by itself doesn't work)
3. Accepts any connections on that socket and sets that connection to AcceptSocket


Doesn't do anything but accept sockets.
Pages: 1 2