NOTE: This page has been translated automatically from Russian to English. Original page.



Simple TCP server event processing on the side of 1C

In my work more and more common problem due 1C and third-party hardware / software industry. In most cases there is some external component to communicate with it all. But this component is worth the money. Plus not everyone want a "black box", which is something that twists in itself, and gives the finished result. This article in the "Pictures" will show how you can implement a TCP server to communicate with than ever "with their own hands."

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

  1. Adding to the ActiveX form
    I prefer to do it programmatically:
    ElementyFormy.DobavitActiveX ( "MSWinsock.Winsock", "WinSock", False);
  2. 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

  1. 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.

  1. DataArrival - availability of data
    WinSocketDataArrival (Element, bytesTotal)
    Where:

i. bytesTotal - number of bytes in the received information

  1. Connect - a successful connection to the server (only occurs on the client!)
    WinSocketConnect (Element)
  2. ConnectionRequest - connection request to the client (server-side occurs)
    WinSocketConnectionRequest (Element, requestID)
    Where:

i. requestID - Customer ID

  1. Close- session closing
    WinsockClose (Element)
  2. SendProgress - Occurs when the progress data
    WinsockSendProgress (Element, bytesSent, bytesRemaining)
    Where:

i. bytesSent - bytes sent

ii. bytesRemaining - bytes left

  1. 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
The default value. Connection closed.

sckOpen

1

Open
Connection is active. The connection is established.

sckListening

2

Listening
"Eavesdropping" mode. Component connection is waiting for the specified port.

sckConnectionPending

3

Connection pending
Waiting for connection

sckResolvingHost

4

Resolving host
Getting the address of the computer (host) name.

sckHostResolved

5

Host resolved
computer address was obtained.

sckConnecting

6

Connecting
connection

sckConnected

7

Connected
connected

sckClosing

8

Peer is closing the connection
Client closed the connection

sckError

9

Error
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.

1C:Enterprise Developer's Community