"Traxgeek" <guest@news.pcsoft.fr> a écrit dans le message de news:
70702009051018500367.27213@news.pcsoft.fr...
>
> Hi people,
> Wondering if someone can throw some libgt on a newbie problem with sockets
> / threads please ?
> I am trying to send data to our web-server over a TCP/IP link from a PC
> here in the office. Both machines are running Windows XP (SP2 / Server).
> I have written a small app. (based upon the‘Using Sockets’
> example). The idea is that myoffice PC will, from time-to-time, send the
> server some data and the server will do something with it !
> The server is supposed to wait for a connection, grant the request (port
> 5010) and display the received data – that’s it (for the
> moment) !!
> Currently I open a socket on the server (WinDev12 app) and it will wait
> for a connection request from my office PC (Windows VB6 / C app.).
> Currently, the WindDev12 server app. does grant the request and opens a
> new thread to handle the incoming data…
> The new thread should simply display the data…
> What I get though is : the connection is accepted by my server WinDev app.
> My Office VB6 app then begins to send some data (we can see it being
> transmitted) BUT I can’t extract it from the socket instance on the
> server…it doesn't appear to exist there. Am I mis-calling the socket
> perhaps ?...
> What am I doing wrong please ? There are two new principles for me here :
> [1] threads
> [2] the socket instance and its requirements to function correctly.
> I have attached the app. code for convenience.
> I would be extremely grateful should someone be able to point out my
> errors – many thanks.
> I should also point out that I can force a disconnect from my Office PC by
> clicking ‘Stop’ and that often if may take more than one
> request by the office PC for a data connection to be established BUT a
> connection will always be made and data will always be sent (even if only
> after the 2nd request…
> I hope the above makes some sense. Again, many thanks.
>
> GLOBAL & CONSTANT VAIABLES
> gnRTPort is numeric
> sConnectionName is string
>
> gsRxData is string
> csSocketFromRaceTrax = "RT_SOCKET_RX" // Name of
> listening socket
> csThreadFromRaceTrax = "RT_THREAD_RX" // Name of
> listening thread
>
> CLICK BTN_Start
> Socket.Option = SocketNoReuseAddress
> gnRTPort = EDT_RxPort
>
> SocketCreate(csSocketFromRT,gnRTPort)
> IF ErrorOccurred THEN
> Beep()
> ELSE
> ThreadExecute(csThreadFromRT,threadNormal,RxSocketListen)
> END
>
> // called by csThreadFromRaceTrax from BTN_RxStart
> PROCEDURE RxSocketListen()
> sdtg is string = DateToString(DateSys())+" -
> "+TimeToString(TimeSys(),"HH:MM
S")
> sRT_IP is string
> LOOP
> IF NOT ThreadState(sConnectionName) = threadRunning THEN
> IF SocketWaitForConnection(csSocketFromRT) THEN
> sConnectionName = SocketAccept(csSocketFromRT)
> ThreadExecute(sConnectionName,threadNormal,"ReceiveData",sConnectionName)
> sRT_IP =
> SocketClientInfo(sConnectionName,SocketAddress)
> UpdateSocketTable(sdtg,"O",sRT_IP,sConnectionName)
> ELSE
> TRACE(“No socket connection: ”+
> csSocketFromRT)
> BREAK
> END
> END
> END
>
> PROCEDURE ReceiveData(sName)
> LOOP
> // NO DATA EVER APPEARS IN OUR gsRxData VARIABLE – WHY ? ? ?
> gsRxData = RTFToText(SocketRead(sName,False,1000))
> // NO DATA EVER APPEARS IN OUR gsRxData VARIABLE – WHY ?
> ? ?
> SWITCH gsRxData
> CASE =""
> //break
> Multitask(-1)
> CASE "BYE"
> TRACE(“Connection
> closed (BYE): ”+ gsRxData)
> RETURN
> OTHER CASE
> TRACE(“Received data : “ + gsRxData)
> // HandleData() // Routine to handle any received
> data…
> END
> END
> CLICK BTN_Stop
> ThreadStop(csThreadFromRT)
> SocketClose(csSocketFromRT)
>
>
Hi,
Just the basics :
1) Threads : a thread is a process which run in parrellel of the main
process (same as if you had several program running but the thread can
access to the main process ressources) Event which stop the main process
(user interraction) don't stop the thread. You must be careful when your
thread access data.
2) socket : As you did, a socket is a connexion between programs which allow
you to send data. If the communication must be both way, each program must
be server and customer.
After reading your code sample. It seem to me that your second thread ( for
ReceiveData) is not necessary. When you receive data, you analyse it ... you
don't need to launch a new thread to do the job
Here is what i do :
IF SocketConnect("Mysocket",port,IpAdress,300) THEN
ThreadExécute("THREAD_Socket",threadNormal,fThreadClient,"MySocket")
END
Procedure fThredWaitData(pSocket)
sData is string
LOOP
IF SocketExist(pSocket) THEN
sData = SocketRead(pSocket)
IF sData <> "" THEN
SWITCH sLecture
case "BYE"
Other case
// Job
ReceiveData(sData)
end
end
end
end
There is only one thread to wait for the data.
Albert P.
Nota : This is sample code ... i just wrote it into the mail ... could be
some error in it :-)
Message forwarded from pcsoft.us.windev