Connect C# to ED

All topics on the Atoms in any Enterprise Dynamics Library.
Post Reply
LAC-Bursi
Posts: 41
Joined: Friday 09 November, 2012 - 10:37

Connect C# to ED

Post by LAC-Bursi »

Hi community,

I would like to connect C# to ED. I create a project C# with a Socket Client.

I have to send and receive message between this two application (C# and ED).

When I try to send messages from C# to ED, everything works well. ED receives message from [127.0.0.1],[7122]

However, when ED trys to response to the message something goes wrong:

- If I send message through the port [7122], ED crashes because it receives and responses to the messages endlessly.

- If I try to send the message through another port (checked if the port is open and free), C# returns the following error:

WSAENOTCONN 10057 Socket is not connected.
A request to send or receive data was disallowed because the socket is not connected and
(when sending on a datagram socket using sendto) no address was supplied.
Any other type of operation might also return this error—for example, setsockopt setting
SO_KEEPALIVE if the connection has been reset.


Have you some suggestion to create a stable connection between C# and ED?

Thanks
Fabio
User avatar
Joris
Posts: 1
Joined: Monday 02 May, 2011 - 14:40

Re: Connect C# to ED

Post by Joris »

Hi Fabio,

In order to setup a two-way socket connection between Enterprise Dynamics and a C# application you could use the Socket Listen and Socket Send atoms in the ED library (under TOOLS).

The Socket Listen atom uses the 4ds function SocketStartListen on creation of the atom in the model and the SocketStopListen on destruction of the atom. The OnMessage Event handler is triggered upon reception of a message.

In the GUI of the Socket Listen atom code can be entered (under "Response" 4ds Editfield) which is executed upon reception of the message. By default the message is executed as a string. So the message should be valid 4ds code. For example when you send the string "Trace([Hello World!])", a line "Hello World!" is displayed in the ED tracer window.

The Socket Send atom uses the 4ds function SocketPost. For example you could execute the following 4ds code to send a message "Hello world!$" to the C# application on the localhost at port 11000:

Code: Select all

SocketPost([Hello world!$], [127.0.0.1], [11000])
Note that I used a "$" character as a terminator for the message.

On the C# application you should use two sockets, one for sending:

Code: Select all

        Socket mSocketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPEndPoint mEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7122);

and one for receiving:

Code: Select all

        Socket mSocketRecieve = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPEndPoint mLocalEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);

In you C# code you could use some code to read some bytes from the socket until the entire message is received, i.e. the character "$" is read.

Code: Select all

            byte[] bytes = new Byte[1024];
            mSocketRecieve.Bind(mLocalEndPoint);
            mSocketRecieve.Listen(10);

            Socket handler = mSocketRecieve.Accept();
            data = null;

            // An incoming connection needs to be processed.
            while (true)
            {
                int bytesRec = handler.Receive(bytes);
                data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                if (data.IndexOf("$") > -1)
                {
                    break;
                }
            }

Of course you could design a message protocol in order to be able to determine how many bytes should be read from the socket to receive the whole message.

Note that of course you could write your own atoms for sending and receiving messages using sockets. Please refer to the ED help under communication for more details. The 4ds functions listed below can be used when setting up your own socket atoms:

Code: Select all

ConnectSocket, CreateSocket, DisconnectSocket, FreeSocket, SetSockBufferSize, SetSocketOpt, SocketCloseClient, SocketError, SocketIsConnected, SocketLinkAtom, SocketOpenClient, SocketPost, SocketRecv, SocketRecvFrom, SocketSend, SocketStartListen, SocketStopListen, SocketWriteClient and TranslateSocketError.
helsywarner
Posts: 1
Joined: Thursday 09 April, 2015 - 08:05
Contact:

Re: Connect C# to ED

Post by helsywarner »

Check this one....a basic level Socket Programming sample.

Warner
Karrahahu1
Posts: 1
Joined: Thursday 11 February, 2016 - 14:10

Re: Connect C# to ED

Post by Karrahahu1 »

For example when you send the string "Trace([Hello World!])", a line "Hello World!" is displayed in the ED tracer window.????
Ali
Post Reply