NOTE: This page has been translated automatically from Russian to English. Original page.
Simple TCP server event processing on the side of 1C
Greetings!
Few lyrics. At one point I came to associate the task 1C and some etikirovochnuyu car. How- not known. It seems that somewhere there are people who know something, but where these people - no one knows. And by laceration supplier could shake manual in English, which was described by the interaction of the format of a TCP port. And in this regard we had to write a kind of happiness.
Well, from words to deeds. I decided to use WinSock darling melkomyagkih technology (for communication over the TCP http://ru.wikipedia.org/wiki/Winsock ). On the basis of it can be written as part of the server and the client. All actions are divided into several stages:
a common part
1) Go to Google and search for dear winsock.ocx
2) We register it in the system (regsvr32)
3) prescribes the registration in the register of branches (otherwise ActiveX is not licensed). Without this we will not be available the necessary methods.
I think that the problem with these steps will not have anybody. If there were difficulties - in google huge number of articles on these issues.
4) Create a process in 1C
- Adding to the ActiveX form
I prefer to do it programmatically:
ElementyFormy.DobavitActiveX ( "MSWinsock.Winsock", "WinSock", False); - Form - Insert ActiveX - Microsoft WinSock Control, version 6.0
Only in this variant it is necessary to remove the appearance of the element, since it lacks a graphical display.
5) Determine appropriate methods of an object
- Error-if there was any error either.
WinSocketError (Element, Number, Description, Scode, Source, HelpFile, HelpContext, CancelDisplay)
Where:
i. Number - an error code
ii. Description - Description of the error,
iii. Scode - error code again, but in a different type (LONG)
iv. Source - The source of error,
v. HelpFile - link to the FAQ
vi. HelpContext - Context Help
vii. CancelDisplay - flag cancel display of error standard window. Default value - the truth. The window does not appear.
- DataArrival - availability of data
WinSocketDataArrival (Element, bytesTotal)
Where:
i. bytesTotal - number of bytes in the received information
- Connect - a successful connection to the server (only occurs on the client!)
WinSocketConnect (Element) - ConnectionRequest - connection request to the client (server-side occurs)
WinSocketConnectionRequest (Element, requestID)
Where:
i. requestID - Customer ID
- Close- session closing
WinsockClose (Element) - SendProgress - Occurs when the progress data
WinsockSendProgress (Element, bytesSent, bytesRemaining)
Where:
i. bytesSent - bytes sent
ii. bytesRemaining - bytes left
- SendComplete - completion of sending data
The server part
1) Run the server itself:
WinSock = ElementyFormy.WinSock; // Our ActiveX
WinSock.LocalPort = Port; // Port on which it will work
WinSock.Bind (Port, "127.0.0.1"); // IP which will listen
WinSock.listen (); // Actually the run on server wiretap
2) In the procedure ConnectionRequest specify:
If WinSocket.State <> 0 then // If we have an active socket, before the adoption of the new current must be closed
WinSocket.Close (); // Close the socket
ENDIF;
WinSocket.Accept (requestID); // Accept the new request
3) In the procedure DataArrival:
TekstSoobscheniya = "";
WinSocket.GetData (TekstSoobscheniya); // Accept message from the server
WinSocket.SendData ( "Otvet server"); // A certain response from the server for information
That's all. The simplest server is ready. You can knock on it and telnet to test. Immediately say, that code page settings (display Cyrillic) depends precisely on the client used. 1C all sends in Cp1251.
The client side
1) Initialize the connection:
WinSocket.RemoteHost = SokrLP (IP); // The address to which connectivity
WinSocket.RemotePort = SokrLP (Port); // Port on which the connectivity
WinSocket.Connect (); // Connect to Team
Attention! Status in the same procedure WinSocket not change! Therefore:
2) Connect procedure:
Report (WinSocket.State) // Here we get the current status
Status Table:
condition | The numerical value | Description |
sckClosed | 0 | Default. Closed |
sckOpen | 1 | Open |
sckListening | 2 | Listening |
sckConnectionPending | 3 | Connection pending |
sckResolvingHost | 4 | Resolving host |
sckHostResolved | 5 | Host resolved |
sckConnecting | 6 | Connecting |
sckConnected | 7 | Connected |
sckClosing | 8 | Peer is closing the connection |
sckError | 9 | Error |
3) The procedure for sending data:
If WinSocket1.State = 7 then // send data only when the status of "Connected"
WinSocket1.SendData (SokrLP (TekstSoobscheniya));
ENDIF;
That's the simplest client is ready.
And then it all depends on your imagination.
Naturally I am not a pioneer in this section, but I hope that my little article will help someone.