Starting out with winsock

For issues with communication ports, protocols, etc.
Post Reply
Stonemonkey
Posts: 649
Joined: Jun 09, 2005 0:08

Starting out with winsock

Post by Stonemonkey »

I'm trying out some network code, udp using winsock. Currently I am just sending a few bytes between two programs.

I'm using recvfrom(socket,buffer.......) to recieve the data but it waits for data to be sent, is there a way to check if there's any data to be recieved that doesn't wait if there's no data?
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Starting out with winsock

Post by TJF »

Stonemonkey wrote:... is there a way to check if there's any data to be recieved that doesn't wait if there's no data?
Check TSNE (search forum).
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Starting out with winsock

Post by badidea »

Yes, with socket select or using non-blocking with fcntl.

I'll post an example of socket select in a minute. --> see: http://www.freebasic.net/forum/viewtopi ... 6&start=14

edit: Or use threads. So the the thread waits for a packet and not the main loop.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Starting out with winsock

Post by D.J.Peters »

Yes socket select is the key. (of course on linux too)

None blocking sockets are a bad idea.

Joshy
Stonemonkey
Posts: 649
Joined: Jun 09, 2005 0:08

Re: Starting out with winsock

Post by Stonemonkey »

Thanks for that. I'm not really wanting to go the route of threads at the moment badidea.
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Re: Starting out with winsock

Post by Gonzo »

D.J.Peters wrote:Yes socket select is the key. (of course on linux too)

None blocking sockets are a bad idea.

Joshy
why? i'd say blocking is a bad idea for anything non-basic
other than that the sockets could die at any time, for any reason, it's just way better design :)
i wish they never made blocking sockets to begin with
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Starting out with winsock

Post by vdecampo »

You can just put your blocking socket function in a thread and kill it if it times out.

-Vince
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Re: Starting out with winsock

Post by Gonzo »

i know, i'm just not a big fan of spamming threads.. but it\s what everyone else does
it''s annoying, and cheap programming, but for personal purposes its quite ok =)

http://fbcraft.fwsnet.net/threads.jpg
one example: tortoiseSVN which does absolutely nothing, has 30 threads
in contrast, FBedit uses 2 threads
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Starting out with winsock

Post by D.J.Peters »

Hello Gonzo
you can do all your network needs with sleep and threads. (what a nice rhyme)
but ten beears sorry years later come back and proof it again ;-)

Joshy
stevie1401
Posts: 134
Joined: Apr 19, 2007 16:20
Contact:

Re: Starting out with winsock

Post by stevie1401 »

I need a servercode for my programm. At time, my server is not very good working with wsaasyncselect.
Is this code realy working?

Code: Select all

    #include "network.bi"
    #include "fbgfx.bi"

    #DEFINE SERVER_PORT 27015
    #DEFINE CLIENT_IP "127.0.0.1"

    '--- SERVER ---

    dim as SOCKET socketId
    dim as sockaddr_in clientAddr
    dim as sockaddr_in serverAddr
    dim as integer serverAddrSize = sizeof(serverAddr)
    dim as integer clientAddrSize = sizeof(clientAddr)
    dim as integer iResult, quit, sendData, sendCount
    dim as string key

    dim as net_data netDataIn
    dim as net_data netDataOut

    dim as fd_set readfds, readfds0
    dim as fd_set writefds, writefds0
    dim as timeval timeout

    iResult = init_socket_API()
    if (iResult <> 0) then
      print "Winsock init: Error"
      sleep
      end
    end if

    socketId = socket_(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
    if (socketId < 0) then
      print "Socket: Error"
      close_socket_API()
      end
    end if

    serverAddr.sin_family = AF_INET
    serverAddr.sin_port = htons(SERVER_PORT)
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY)

    iResult = bind(socketId, cptr(sockaddr ptr, @serverAddr), serverAddrSize)
    if (iResult < 0) then
      print "Bind: Error"
      close_socket_API()
      sleep
      end
    end if

    clientAddr.sin_family = AF_INET
    clientAddr.sin_port = htons(SERVER_PORT)
    clientAddr.sin_addr.s_addr = inet_addr(CLIENT_IP)

    FD_ZERO(@readfds0)
    FD_ZERO(@writefds0)
    FD_SET_(socketId, @readfds0)
    FD_SET_(SocketId, @writefds0)

    print "q = quit"
    print "s = send"

    while (quit = 0)
     
      quit = 0: sendData = 0
      key = lcase(inkey())
      if (key = "q") then quit = 1
      if (key = "s") then sendData = 1

    'Keep a backup because SelectSocket will change the data
      readfds = readfds0
      writefds = writefds0
      timeout = type(0, 0)

      iResult = selectSocket(FD_SETSIZE, @readfds, @writefds, NULL, @timeout)
      if (iResult = 0) then
        print "SelectSocket: Timeout"
      elseif (iResult < 0) then
        print "SelectSocket: Error"
      else
        'print "SelectSocket: Sockets ready: "; iResult
      end if
     
      if(sendData = 1) then
        netDataOut.seqNr = sendCount
        netDataOut.text = "Server says: No!"
        if(FD_ISSET(socketId, @writefds)) then
          print "Send: Trying..."
          iResult = sendto(socketId, cptr(ubyte ptr, @netDataOut), _
            sizeof(net_data), 0, cptr(sockaddr ptr, @clientAddr), clientAddrSize)
          if (iResult < 0) then
            print "Send: Error"
            closeSocket(socketId)
            close_socket_API()
            sleep
            end
          else
            print "Bytes send:"; iResult;
            print " to: "; inet_addr_rev(clientAddr.sin_addr)
          end if
        end if
        sendCount += 1
      end if
     
      if(FD_ISSET(socketId, @readfds)) then
        print "Receive: Trying..."
        iResult = recvfrom(socketId, cptr(ubyte ptr, @netDataIn), _
          sizeof(net_data), 0, cptr(sockaddr ptr, @clientAddr), @clientAddrSize)
        if (iResult > 0) then
          print "Bytes received:"; iResult;
          print " from: "; inet_addr_rev(clientAddr.sin_addr)
          print "SeqNr: "; netDataIn.seqNr;
          print " text: "; netDataIn.text
        elseif (iResult = 0) then
          print "Receive: Connection closed"
          closesocket(socketId)
          close_socket_API()
          sleep
          end
        else
          print "Receive: Error"
          closesocket(socketId)
          close_socket_API()
          sleep
          end
        end if
      end if
     
      sleep 1, 1
     
    wend

    iResult = closesocket(socketId)
    if (iResult < 0) then
      print "CloseSocket: Error"
    end if
    close_socket_API()

    print "End."
    sleep


My socketserver works without threads, because I work with shared variables.
To look what my server ist doing, you can look here:
http://www.doko-lounge.de (In german)
My server is written in GFA Basic and works not very vell.
So I need a better Server code.
Is the freebasic-code better?
@D.J.Peters in german: Kannst du mir evtl. beim Socketserver helfen? Mein GFA Server läuft mit GFA Basic mit WSAAsyncselect und läuft nicht wirklich "rund". Ich kann dir gerne meinen Code senden.
Ich weiss nicht wie man hier PMs schreibt, deshalb schreibe ich es so.
My email: hilfe@doko-lounge.de

Thank you :-)
Stevie
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Starting out with winsock

Post by D.J.Peters »

@Stevie
I can't help ATM but here you will get it self (i'm sure)
in german: http://www.zotteljedi.de/socket-tipps/s ... genau.html

all other socket topics: http://www.zotteljedi.de/socket-tipps/

Joshy
Post Reply