December 06, 2022, 09:50:00 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Open Server, Close and reopen does not work

Started by r.budras, October 25, 2017, 05:10:46 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

r.budras

Hello, its me again

I open a Server, works fine says the return value
I close the Server, works fine says the return value
and then I reopen the same server again and I get error 10048, Socket address already in use.
My Open and close subs see below.
"plog" is my "Print in the Log window" subroutine.

Please help !

Reinhard


int BBQ_Listen_Socket             = -1
'=========================================================================
sub BBQ_server_Start(),int
   int ncsStatus
   ncsStatus = IWNCSOpenForClients("",6666,1)
   if ncsStatus < 0
      plog("BBQ_server_Start","ERROR IWNCSOpenForClients, Errorcode :"+str$(IWNCSGetLastError())+", "+IWNCSGetErrorText())
   else
      BBQ_Listen_Socket = ncsStatus
      plog("BBQ_server_Start","O.K. IWNCSOpenForClients, BBQ_Listen_Socket = "+STR$(BBQ_Listen_Socket))

      plog("BBQ_server_Start","IWNCSOnConnectMessage")
      ncsStatus = IWNCSOnConnectMessage(w_bbq.hwnd,BBQ_Listen_Socket,BBQ_ID_OnConnectMessage)
      if ncsStatus < 0
         plog("BBQ_server_Start","ERROR IWNCSOnConnectMessage, Errorcode :"+str$(IWNCSGetLastError())+", "+IWNCSGetErrorText())
      else
         plog("BBQ_server_Start","O.K. IWNCSOnConnectMessage Server started and waiting for connections")
      endif
   endif

return 0
endsub
'=========================================================================
sub BBQ_server_Stop(),int
   int ncsStatus
   ncsStatus = IWNCSCloseSocket(BBQ_Listen_Socket)
   if ncsStatus < 0
      plog("BBQ_server_Stop","ERROR IWNCSCloseSocket, Errorcode :"+str$(IWNCSGetLastError())+", "+IWNCSGetErrorText())
   else
      plog("BBQ_server_Stop","O.K. IWNCSCloseSocket, BBQ_Listen_Socket: "+str$(BBQ_Listen_Socket))
   endif
   BBQ_Listen_Socket = -1

return 0
endsub
'=========================================================================
SUB plog(pw:string,pi:string),int
   ' int time_delta
   string timestring, txt ', time_delta_string
   getclientsize w_log,woid,woid,w_log_wdh,w_log_hgh
   SETFONT w_log, "Courier", 10, 400

   log_old_tm = log_tm
   GetLocalTime(log_tm)
   if left$(pi,1) = "$" 'indicator for "print in the same line as before to avoid masses of entries in loops"
      txt = mid$(pi,2)
   else
      txt = pi
      printpos = printpos + 15
   endif

   IF (printpos > w_log_hgh - 30) then printpos = 20
   RECT w_log,0,printpos,2000, 60,RGB(255,255,255),RGB(255,255,255)
   timestring = using("0##:0##:0##:0###",log_tm.wHou,log_tm.wMin,log_tm.wSec,log_tm.wmse)
   'time_delta = (log_tm.wmse - log_old_tm.wmse) + (log_tm.wsec - log_old_tm.wsec)*1000  + (log_tm.wmin - log_old_tm.wmin) * 60000 + (log_tm.wHou - log_old_tm.wHou) * 3600000
   'time_delta_string = using("0####",time_delta)
   mopri(w_log, 10, printpos, timestring + " " + pw)      '" dT:" + time_delta_string +
   mopri(w_log, 250, printpos," :"+ txt)

return printpos
ENDSUB
'=========================================================================

'=========================================================================

Egil

I'm not familiar with the IW NCS Library, but sounds like the socket never closed properly. Maybe calling Wait before proceeding after the close command will help you.


Good luck!
Egil
Support Amateur Radio  -  Have a ham  for dinner!

ckoehn

QuoteI'm not familiar with the IW NCS Library, but sounds like the socket never closed properly.

Or maybe it didn't open properly.  Although you would think you would get an error code with that.

The help file says
QuoteIt is possible to still have data pending after a socket has been closed so be prepared for one or more window messages as the buffer is emptied.

I'm sure you included the IWNCSInit although it isn't listed in your code.

INT = IWNCSInit(datasockets as int,listensockets as int)

If I can find some time I'll look into it.

Later,
Clint

Egil

Quote from: ckoehn on October 26, 2017, 06:00:04 AM
Or maybe it didn't open properly.  Although you would think you would get an error code with that.

That was my first thought, but since he did not mention it, I guess he receives data before trying to close the socket. But then, it could as well be a buffer that need to be purged.
When I started experimenting with UDP routines, I sometimes got a similar error code which dissapeared after inserting a wait command.
But after finding these routines: http://www.ionicwind.com/forums/index.php?topic=6044.msg44549#msg44549, where the receiving  routines run in a thread, all errors vanished...


Egil
Support Amateur Radio  -  Have a ham  for dinner!

r.budras

Thanx Clint
Thanx Egil

I have stripped down my program to server open and close functions, see file attached
In full version my program runs stable and fast, even with 4.500 bytes to send and read at once.
Just opening and closing servers is strange.
Please, if anyone finds the time, try :
1. open all four servers, will be O.K.
2. close all four servers, will be O.K
3. reopen any server, will produce error

Reinhard

Brian

I get an undefined function error at Line 106:

i = IWNCSInit(8,8)

Brian

Egil

Been scratching my head for hours here , but have not yet been able to figure out what is causing the error messages.

There is one thing though, I noticed that when closing the program without starting any server, a list off errors are listed just before the window are closed.
This happens so fast  that I had to do numerous shutdowns before I managed to make a screendump, and still I'm not sure if I managed to catch all errors. See attached picture.


Egil
Support Amateur Radio  -  Have a ham  for dinner!

r.budras

This is O.K.
In the closing sub is :
   if socket < 0
      plog(name+" server_stop","ERR. - CloseSocket, Socket was not open")
   else
      ncsStatus = IWNCSCloseSocket(socket)....

I will change this from "ERR. ..." to "INFO ..."
That sounds better, because this is not an error-

Egil, it is great that You spent so much time on my problem.
If You have another idea, please let me know.

Is there something I can do for You ?

Reinhard

Egil

QuoteIn the closing sub is :
   if socket < 0
      plog(name+" server_stop","ERR. - CloseSocket, Socket was not open")
   else
      ncsStatus = IWNCSCloseSocket(socket)...

I somehow missed that lines... sorry.


QuoteIf You have another idea, please let me know.

Not at the moment, but your problem still bugs me as it is very similar to something I experienced myself when I started out using EB (the predecessor of IWB).  That particular problem was solved by redifining a STRING variable to ISTRING and dimension it to give the contents a little more "room".

I'll look into your problem again tonight. If I come up with something, I'll let you know.



Egil
Support Amateur Radio  -  Have a ham  for dinner!