Spinach.AsynchronousClient.SendSingleClient C# (CSharp) Method

SendSingleClient() public method

public SendSingleClient ( ) : void
return void
        public void SendSingleClient()
        {
            // Connect to a remote device.
            try
            {
                // Establish the remote endpoint for the socket.
                // The name of the 
                // remote device is "host.contoso.com".
                IPAddress ipAddress = (Dns.GetHostAddresses(targetip))[0];
                IPEndPoint remoteEP = new IPEndPoint(ipAddress,Int32.Parse(targetport));

                //Socket client = new Socket(AddressFamily.InterNetwork,
                //        SocketType.Stream, ProtocolType.Tcp);

                {
                    // Create a TCP/IP socket.
                    Socket client = new Socket(AddressFamily.InterNetwork,
                        SocketType.Stream, ProtocolType.Tcp);

                    // Connect to the remote endpoint.
                    client.BeginConnect(remoteEP,
                        new AsyncCallback(ConnectCallback), client);
                    connectDone.WaitOne();

                    // Send test data to the remote device.
                    string m = SingleMsg + "<EOF>";
                    Send(client, m);
                    sendDone.WaitOne();

                    // Receive the response from the remote device.
                    //Receive(client);
                    //receiveDone.WaitOne();

                    // Write the response to the console.
                    //Console.WriteLine("Response received : {0}", response);

                    // Release the socket.
                    client.Shutdown(SocketShutdown.Both);
                    client.Close();
                }

            }
            catch (Exception e)
            {
               //System.Windows.Forms.MessageBox.Show(e.Message, "Failed to send to single client.");
               // Console.WriteLine(e.Message);
                Console.WriteLine("Please check the IP " + e.Message);
                int ErrorCode = 10;
                string Error = "Please Check the IP";
                if (ErrorExcep != null)
                    ErrorExcep(ErrorCode, Error);
            }
        }

Usage Example

Example #1
0
        //*************************************
        public bool Join_Swarm(string DstIP, string DstPort, string SrcIP, string SrcPort, string Username)
        {
            Thread t1 = null;
            Thread t2 = null;
            try
            {
                string cpu = "1";
                mSocket.SetIP(SrcIP);
                mSocket.SetPort(SrcPort);
                mSocket.SetName(Username);
                mSocket.SetCPU(cpu);

                listenerThread = new Thread(new ThreadStart(mSocket.StartListening));
                listenerThread.IsBackground = true;
                listenerThread.Start();

                t1 = new Thread(new ThreadStart(mSocket.StarHeartBeat));
                t1.IsBackground = true;
                t1.Start();

                MessageGenerator msg = new MessageGenerator();
                string mMsg = msg.msgConnectionRequest
                    (mSocket.GetIP(), mSocket.GetPort(), mSocket.GetName(), mSocket.GetCPU());
                AsynchronousClient client = new AsynchronousClient();
                client.ErrorExcep += new AsynchronousClient.EventHandlerExcep(Error_Changed);
                client.SetSingleMsg(DstIP, DstPort, mMsg);
                // t2 = new Thread(new ThreadStart(client.SendSingleClient));
                client.SendSingleClient();
                // t2.Start();
                // t2.IsBackground = true;
                Thread.Sleep(2000);
                if (mSocket.GetIPtoPeer().Count > 0)
                    return true;
                else
                    return false;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Failed to join swarm.");
                return false;
            }

        }