08-28-2005, 08:27 AM
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?
Thanks for any help...
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...