Search This Blog

Thursday, March 22, 2012

Socket Programming


private void btnRead_Click(object sender, EventArgs e)
        {

           string Query = "POST /test/service.asmx/HelloWorld HTTP/1.1\r\n" +
            "Host: 192.168.1.25\r\n" +
            "Content-Type: application/x-www-form-urlencoded\r\n" +
           "Content-Length:0 \r\n";

            byte[] byArrsend = System.Text.ASCIIEncoding.ASCII.GetBytes(Query);
            try
            {
                IPAddress ipAddress = IPAddress.Parse("110.234.132.130");
                IPEndPoint ipEnd = new IPEndPoint(ipAddress, 80);
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(ipEnd);
                if (socket.Connected)
                {
                    socket.Send(byArrsend);
                    socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), socket);
                    //MessageBox.Show(responceBuilder.ToString());
                    while (reading)
                        System.Threading.Thread.Sleep(200);
                    textBox1.Text = Responce;
                    StreamWriter sw = new StreamWriter("\\My Documents\\Socket1.xml");
                    sw.Write(Convert.ToString(responceBuilder));
                }
                else
                {
                    MessageBox.Show("Not Connected");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        int bytesRead;
        string Responce;
        bool reading = true;
        byte[] buffer = new byte[1024];
        StringBuilder responceBuilder = new StringBuilder();

        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                //while (reading == true)
                //{
                    Socket socket = (Socket)ar.AsyncState;
                    bytesRead = socket.EndReceive(ar);
                    if (bytesRead > 0)
                    {
                        Responce = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                        //_Receive = true;
                    }
                    if (bytesRead > 0)
                    {
                        if (responceBuilder.Length == 0)
                        {
                            string tempString = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                            //if (replace) ;
                            //{
                                 // tempString = tempString.Remove(0, tempString.IndexOf("<?xml version="));
                            //}
                            responceBuilder.Append(tempString);
                        }
                        else
                        {
                            responceBuilder.Append(Encoding.ASCII.GetString(buffer, 0, bytesRead));
                        }
                        socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), socket);
                        reading = true;
                    }
                    else
                    {
                        reading = false;
                    }
                //}
                Responce = responceBuilder.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            //responceBuilder = responceBuilder.Remove(0, 0);
            //StreamWriter sw=new StreamWriter("\\My Documents\\Socket1.xml");
            //sw.Write(Convert.ToString(responceBuilder));
        }

No comments:

Post a Comment