Ejemplo n.º 1
0
        public void StartClient(string url)
        {
            // Connect to a remote device.
            try
            {
                // Establish the remote endpoint for the socket.
                // The name of the
                IPHostEntry ipClientInfo    = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress   ipClientAddress = ipClientInfo.AddressList[0];

                IPHostEntry ipHostInfo = Dns.GetHostEntry(url);
                IPAddress   ipAddress  = ipHostInfo.AddressList[0];
                IPEndPoint  remoteEP   = new IPEndPoint(ipAddress, port);

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

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

                string[] IPParts = ipClientAddress.ToString().Split('%');
                // Send test data to the remote device.
                Send(client, IPParts[0] + "<IPIF>");

                InformationContainer.selfIP = IPParts[0];

                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);

                InformationContainer.PushIPAddressesIntoLoop(url);
                SentConfirm(true);


                // Release the socket.
                client.Shutdown(SocketShutdown.Both);
                client.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                SentConfirm(false);
            }
        }
Ejemplo n.º 2
0
        public void ReadCallback(IAsyncResult ar)
        {
            string content = String.Empty;

            StateReader stateReader = (StateReader)ar.AsyncState;
            Socket      handler     = stateReader.workSocket;

            int bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                stateReader.sb.Append(Encoding.ASCII.GetString(stateReader.buffer, 0, bytesRead));
                content = stateReader.sb.ToString();

                if (content.IndexOf("<EOF>") > -1 || content.IndexOf("<IPIF>") > -1)
                {
                    Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
                                      content.Length, content);
                    //richTextBox.Text=content;
                    richTextBox.Invoke((MethodInvoker) delegate
                    {
                        if (content.IndexOf("<IPIF>") == -1)
                        {
                            richTextBox.Text += content.Replace("<EOF>", "\n");
                        }
                        else
                        {
                            InformationContainer.PushIPAddressesIntoLoop(content.Replace("<IPIF>", ""));
                        }
                    });
                    // Echo the data back to the client.
                    Send(handler, content);
                }
                else
                {
                    handler.BeginReceive(stateReader.buffer, 0, StateReader.BufferSize, 0,
                                         new AsyncCallback(ReadCallback), stateReader);
                }
            }
        }