Click to See Complete Forum and Search --> : Big problem


Ciprian Pavel
April 19th, 2001, 02:05 AM
What is wrong with my code:

The problem is that:
-when I run my program after a connection has been served it crashes but when I run it with F8 it works very fine.



Dim IndexClient as Long
Dim TotalClients as Long
Dim Connection as new ADODB.Connection
Dim Command as new ADODB.Command
Dim Recordset(20) as new ADODB.Recordset

private Sub Form_Load()
Clients.Clear
Connection.Open "DSN=Oracle;UID=userid;PWD=password"
Server(0).Listen
for i = 1 to 20
Load Server(i)
next i
End Sub

private Sub Form_Unload(Cancel as Integer)
If Connection.State <> adStateClosed then
Connection.Close
End If
Clients.Clear
for i = 1 to IndexClient
If Server(i).State <> sckClosed then
Server(i).Close
Unload Server(i)
End If
next i

End Sub

private Sub Server_Close(Index as Integer)
'un client se deconecteaza si trebuie sa-l sterg din lista
Server(Index).Close
Clients.Clear
for i = 1 to 20
If Server(i).State <> sckClosed then
Clients.AddItem Server(i).RemoteHostIP
End If
next i
IndexClient = IndexClient - 1
End Sub

private Sub Server_ConnectionRequest(Index as Integer, byval requestID as Long)
TotalClients = TotalClients + 1
Total = "Total clients: " & TotalClients
'se conecteaza un client
IndexClient = IndexClient + 1
for i = 1 to IndexClient
If Server(i).State = sckClosed then
Server(i).LocalPort = 0
Server(i).Accept requestID
Exit for
End If
next i
'If i = IndexClient then
' Server(IndexClient).LocalPort = 0
' Server(IndexClient).Accept requestID
'End If
Clients.AddItem Server(IndexClient).RemoteHostIP

End Sub

private Sub Server_DataArrival(Index as Integer, byval bytesTotal as Long)
'un client cere date trimitand un string
'analizez stringul si ii trimit datele sau eroare
Dim Data as string
Dim mumu 'nu stiu de ce
Server(Index).GetData Data
If IsNumeric(Data) = true then
'stringul trimis este cod de articol sau cod de bare
mumu = Cod_Articol(Index, Data)
else
'stringul trimis este denumire de articol;ex: coca-cola
mumu = Denumire_Articol(Index, Data)
End If
End Sub

private Function Cod_Articol(byval Index as Integer, byval CodArticol as string)
CodArticol = Trim(CodArticol)
Command.ActiveConnection = Connection

If len(CodArticol) = 8 And mid(CodArticol, 1, 1) = "2" then
CodArticol = mid(CodArticol, 2, 6)
End If

If len(CodArticol) > 8 And mid(CodArticol, 1, 2) = "28" then
Command.CommandText = "select art_no from barcode where barcode like " & mid(CodArticol, 1, 7)
Recordset(Index).Open Command, , adOpenStatic, adLockOptimistic, adCmdText
If Recordset(Index).RecordCount >= 0 then
CodArticol = Recordset(Index).Fields(0)
else
Server(Index).SendData "ERR"
GoTo sfarsit
End If
End If

If len(CodArticol) > 8 then 'este cod de bare normal
Command.CommandText = "select art_no from barcode where barcode like " & CodArticol
Recordset(Index).Open Command, , adOpenStatic, adLockOptimistic, adCmdText
If Recordset(Index).RecordCount >= 0 then
CodArticol = Recordset(Index).Fields(0)
else
Server(Index).SendData "ERR"
GoTo sfarsit
End If
End If

If Recordset(Index).State <> adStateClosed then
Recordset(Index).Close
End If

Command.CommandText = "select * from cioprica_symbol where art_no=" & CodArticol
Recordset(Index).Open Command, , adOpenStatic, adLockOptimistic, adCmdText

If Recordset(Index).RecordCount > 0 then
for i = 0 to 15
Server(Index).SendData Recordset(Index).Fields(i) & "@"
next i
else
Server(Index).SendData "ERR"
End If
sfarsit:
Recordset(Index).Close
End Function

public Function Denumire_Articol(byval Index as Integer, byval DenArticol as string)
Command.ActiveConnection = Connection
DenArticol = UCase(DenArticol)
Command.CommandText = "select art_no,descr from article where descr like '%" & Trim(DenArticol) & "%'"
Recordset(Index).Open Command, , adOpenStatic, adLockOptimistic, adCmdText
If Recordset(Index).RecordCount > 0 And Recordset(Index).RecordCount <= 50 then
Recordset(Index).MoveFirst
for i = 1 to Recordset(Index).RecordCount
Server(Index).SendData Recordset(Index).Fields(0) & "@" & Recordset(Index).Fields(1) & "@"
Recordset(Index).MoveNext
next i
else
If Recordset(Index).RecordCount > 50 then
Server(Index).SendData "SPC"
else
Server(Index).SendData "ERR"
End If
End If
Recordset(Index).Close
End Function






Thanks

Cimperiali
April 23rd, 2001, 09:37 AM
Maybe I am a little tired, and I did not look carefully at all your code: as I saw the following:
Dim Connection as new ADODB.Connection
Dim Command as new ADODB.Command
Dim Recordset(20) as new ADODB.Recordset

I went down fast looking for (but not finding) the following:

set connection = nothing
set command= nothing
set Recordset(i)= nothing
may it be?...


Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.

Ciprian Pavel
April 24th, 2001, 06:19 AM
Thanks a lot.
I resolved the problem.
The error was not in the server it was in the client application.
I had to put an minimal delay (Sleep(10);) in the client application.

Cimperiali
April 24th, 2001, 07:05 AM
It sounds strange...You can make it wait untill connection has established
do while connection.state <> 1
'make a control connection is not ending for other causes
if connection.state > 3 'do not remember exactly: have a look in MSDN
'exit your sub knowing no connection has established
...
end if

loop
'connecttion established...
...
Hope this help

Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.

Ciprian Pavel
April 24th, 2001, 07:38 AM
The client code was the following (in C++):
connect(...);
send(...);
recv(...);
closesocket();
The problem was that when the client began to receive the data the recv(...) function returned and the client began the process for closing the connection.In this time the server was sending data and when it receives the closing message from client the Server_Close(...) function was called and the connection was closed during that the server was still tring to send data using that socket.
In debug mode the Server_Close(...) function was executed after I had finished executing the sending function and that why in debug mode everythig worked fine.
Instead using Sleep(10) in client program I can try setting the buffer size of Windows Sockets to 0.
Thanks again.