IonicWind Software

IWBasic => General Questions => Topic started by: pistol350 on August 20, 2008, 04:48:15 AM

Title: Help with getting a sample code compiled
Post by: pistol350 on August 20, 2008, 04:48:15 AM
This sample code is about connection to a server and was written by sapero.
I can no longer get it compiled, but i am pretty sure that it compiled fine by the past.

When i use sapero's "windows.inc" header i have only two errors :

QuoteCompiling...
Sapero_Server code.eba
File: D:\Program Files\EBDev\projects\EBDev_projects and demo\Sapero_Server code.eba (74) invalid assignment - different types
File: D:\Program Files\EBDev\projects\EBDev_projects and demo\Sapero_Server code.eba (98) no appropriate conversion exists
Error(s) in compiling "D:\Program Files\EBDev\projects\EBDev_projects and demo\Sapero_Server code.eba"

Any suggestions ?

$include "windows.inc"
$use "ws2_32.lib"

typedef CALLBACK UINT
TYPE SERVERDATA
   UINT ip
   WORD port
   CALLBACK onListen     /*optional*/
   CALLBACK onConnect    /*optional*/
   CALLBACK onListenFail /*optional*/
   UINT     sock         /*socket handle returned from server*/
ENDTYPE
declare cdecl extern __beginthread(function:UINT, stack_size:UINT, argument:POINTER),INT
declare cdecl extern _memset(addr:POINTER, value:INT, count:INT)
'thread function for _beginthread must be cdecl
declare cdecl server(sd:SERVERDATA)
OPENCONSOLE
' create server
SERVERDATA sd
   sd.ip   = inet_addr("0.0.0.0")
   sd.port = 65432
   sd.onConnect = &onConnect
   sd.onListen  = &onListen
   sd.onListenFail = &onListenFail
   __beginthread(&server, 0, &sd)
   ' do here what you want...  waituntil or so
   print "press any key to quit"
   _sleep(1000) /*wait one sec to complete thread creation*/
   if (sd.sock = 0)
      ' _beginthread or listen failed
   endif
   while (inkey$() = "") and (sd.sock <> 0)
      _sleep(50)
   wend
   ' stop server at any time: closesocket(sd.sock)
   if (sd.sock <> 0)
      ' key pressed
   else
      ' \Device\Afd\Endpoint [socket] handle closed
   endif
end
' -------- callbacks ----------
sub onConnect(client_ip:UINT, port:WORD, client_socket:UINT),INT
   print "connected ",inet_ntoa(client_ip), " on port ",port
   return TRUE  /*allow, save socket handle and use it to send/recv/closesocket*/
'  return FALSE /*deny*/
endsub
sub onListenFail(client_ip:UINT, port:WORD)
   print "listen failed: interface ",inet_ntoa(client_ip), " on port ",port
   return
endsub
sub onListen(_interface:UINT, port:WORD)
   print "listening: interface ",inet_ntoa(_interface), ", port ",port
   return
endsub
' --------  server stuff [thread] -----------
declare template(client_ip:UINT, port:WORD),INT
declare template3(client_ip:UINT, port:WORD, client_socket:UINT),INT
sub server(sd:SERVERDATA)
   UINT    callback
   WSADATA wsa
   WSAStartup(0x202, &wsa)
   callback = sd.onListenFail
   sd.sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
   if (SOCKET_ERROR = sd.sock) /*onListenFail*/
      if (callback) then !<template>callback(sd.ip, sd.port)
      goto server_cleanup
   endif
   sockaddr_in sa
   _memset(&sa, 0, len(sockaddr_in))
   sa.sin_family = AF_INET
   sa.sin_port   = htons(sd.port)
   sa.sin_addr   = sd.ip
   if SOCKET_ERROR = bind(sd.sock, &sa, len(sockaddr_in)) /*onListenFail*/
      if (callback) then !<template>callback(sd.ip, sd.port)
      goto server_cleanup
   endif
   if SOCKET_ERROR = listen(sd.sock, 5)
      if (callback) then !<template>callback(sd.ip, sd.port)
      goto server_cleanup
   endif
  ' report listen success
   callback = sd.onListen
   if (callback) then !<template>callback(sd.ip, sd.port)
  ' wait for clients while you can :)
   UINT client_socket
   INT  accepted, sa_size
   sa_size = len(sockaddr_in)
   client_socket = accept(sd.sock, &sa, &sa_size)
   while (SOCKET_ERROR <> client_socket)
      sockaddr_in peer
      _memset(&peer, 0, len(sockaddr_in))
      sa_size = len(sockaddr_in)
      getpeername(client_socket, &peer, &sa_size)
      accepted = FALSE
      callback = sd.onConnect
      if (callback) then accepted = !<template3>callback(peer.sin_addr, htons(peer.sin_port), client_socket)
      if (FALSE = accepted)
         send(client_socket, "I'm busy", 10, 0)
         closesocket(client_socket)
      else
         send(client_socket, "Hello", 7, 0)
      endif
      sa_size = len(sockaddr_in)
      client_socket = accept(sd.sock, &sa, &sa_size)
   wend
   ' if here - an error occured, application terminates or you closed server socket
   LABEL server_cleanup
   closesocket(sd.sock)
   sd.sock = 0
   WSACleanup()
   ' maybe a callback onServerQuit?
   print "quit"
   return
endsub


Title: Re: Help with getting a sample code compiled
Post by: sapero on August 20, 2008, 06:10:58 AM
Replace all sin_addr with sin_addr.S_un.S_addr (match whole word)

Actually the sa.sin_addr and peer.sin_addr represents a structure (in_addr).
Title: Re: Help with getting a sample code compiled
Post by: pistol350 on August 22, 2008, 03:34:13 PM
Thank you!  8)
That did the job.