Interop call C# WinForm by TCP Server 

Sunday, November 14, 2010 4:58:27 PM

As my privious section mentioned, if the VB6 and WinForm application are not located in one machine, the WinMsg can't work.

 

So there are some remote ways to use:

TCP communication

Tcp communication is based on socket communication, but it takes care of all the handshaking and data process for the user. It takes in a stream as input and output a stream as well.

.Net remoting

.Net remoting is similar to web services but it does not have the over head of web service.

 

Both TCP and .Net remoting are used to pass information between computers accorss network.

 

Finally we choose to use TCP to handle the work.

 

The process is:

In the Interop.cs we add a new method named notifyTCPServer, in the method we create a TCPSender

and put the message to the TCPSender.

In the TCPSender.cs, it will create a socket to communicate with the remote WinForm machine.

In the remote WinForm machine, we create a TCPReceiver.cs to listen the connection and if the connection is created, it can receive message from the TCPServer. After receive the message, it

will send WinMsg to WinForm similar with privious section

 

The Interop.cs

        public void notifyTCPServer()
        {
            StringBuilder sbMsg = new StringBuilder();
            if (this.GenInfo != null)
            {
                for (int i = 0; i < GenInfo.Length; i++)
                {

                    if (i == (GenInfo.Length - 1))
                        sbMsg.Append(GenInfo[i]);
                    else
                        sbMsg.Append(GenInfo[i] + "|");
                }
                _logger.WriteEntry("GenInfo:" + sbMsg);
            }
            else
            {
                sbMsg.Append(Flag.ToString());
            }

            //send msg through TCP server

            TCPSender tcpSender = new TCPSender();
            tcpSender.sendMsg(sbMsg.ToString());
        }

 

The TCPSener.cs
    public class TCPSender
    {

       public int port = 9050;       
        public void sendMsg(String msg)
        {

            //the Remote machine IP

            string ip= "192.168.1.100";
            sendToClient(ip, msg);
         }

        private void sendToClient(string ip, string msg)
        {

            TcpClient client = null;
            NetworkStream strm = null;
            try
            {
                client = new TcpClient();
                IPAddress address = IPAddress.Parse(ip);
                IAsyncResult MyResult = client.BeginConnect(ip, port, null, null);
                MyResult.AsyncWaitHandle.WaitOne(5000, true);   //set time out   
                if (!MyResult.IsCompleted)
                {
                    client.Close();
                }
                else
                {

                    // the formatter that will serialize my object on my stream 
                    IFormatter formatter = new BinaryFormatter();                    

                    strm = client.GetStream(); // the stream 
                    formatter.Serialize(strm, msg); // the serialization process 
                }

            }
            catch (Exception e)
            {
            }
            finally
            {
                if (strm != null)
                {
                    strm.Close();
                }
                if (client != null)
                {
                    client.Close();
                }
            }
        }

    }

 

The Remothe TCPReceiver.cs

    public class TCPReceiver
    {

        public static string IP = "";
        public int listenPort = 9050;
        public void listen()
        {
            TcpListener listener = new TcpListener(listenPort);
            listener.Start();
            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();
                IPEndPoint ip = (IPEndPoint)client.Client.RemoteEndPoint;

                IP = ip.Address.ToString();//get the IP from client
               

                NetworkStream strm = client.GetStream();
                IFormatter formatter = new BinaryFormatter();

                String msg = (String)formatter.Deserialize(strm);

                
                Interop interop = new Interop ();

               
                interop.notifyCSharpFormByTCP(msg);

                strm.Close();
                client.Close();
            }
        }

    }

You must sign in to this site to post comments.

Site Map | Privacy | Printable View | © 2008 - 2012 Pacific Ingenium, Inc.