Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem reconnecting using Socket class

Problem reconnecting using Socket class

Scheduled Pinned Locked Moved C#
csharphtmldatabasedotnetsysadmin
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    quacks_a_lot
    wrote on last edited by
    #1

    I am trying write a TCP client that connects to a server and requests HTTP packets using the Socket class. The problem I'm having is when I receive a HTTP return code of 400 (Bad Request), the server disconnects me and I have to close my connection as well. Once I close my connection, the Socket class won't let me start another TCP handshake, let alone send any more packets. Here is my code for the connection:

    ;
    Socket socket;
    IPEndPoint hostEndPoint;
    string packet;

    public void run()
    {
    TCPConnect();

    packet = SendReceive("/signin.aspx", "GET");
    if (GetReturnCode(packet) == "400")
    TCPReset();

    packet = SendReceive("/index.html", "GET");

    TCPClose();
    }

    private void TCPConnect()
    {
    // Make sure there is a connection
    if (socket == null)
    {
    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    hostEndPoint = new IPEndPoint(ipaddr, port);
    }

    socket.Connect(hostEndPoint);
    }

    // My problem is getting this function working properly
    private void TCPReset()
    {
    socket.Disconnect(true); // Close the socket connection and allow reuse of the socket
    socket.Connect(hostEndPoint); // This is ignored according to Ethereal,
    // nothing is sent even using socket.Send()
    }

    private void TCPClose()
    {
    socket.Shutdown(SocketShutdown.Both);
    socket.Close();
    }

    private string SendReceive(string strURI, strMethod)
    {
    string pcAppend = " HTTP/1.1\r\nConnection: Keep-Alive\r\n\Content-Length: 0\r\n" +
    "User-Agent: Mozilla/4.0 (Compatible; MSIE 7.0; Windows NT 5.2; .NET CLR " +
    "2.0.50727; .NET CLR 3.0.04506.30; InfoPath.1; .NET CLR 3.5.21022)\r\n" +
    "Content-Type: application/x-www-form-urlencoded\r\nHost: " + strIPAddress + "\r\n\r\n";
    byte[] pcSend = System.Text.ASCIIEncoding.ASCII.GetBytes(strMethod + strURI + pcAppend);
    byte[] bfBuffer = new byte[10240];

    socket.Send(pcSend, pcSend.Length, 0);
    socket.Receive(bfBuffer, bfBuffer.Length, SocketFlags.None);

    return System.Text.ASCIIEncoding.ASCII.GetString(bfBuffer);
    }

    ; I've been monitoring the commuications with Ethereal. .Net is closing the connection just fine, but it ignores any socket.Connect() calls after the initial connection. Any ideas?

    C N 2 Replies Last reply
    0
    • Q quacks_a_lot

      I am trying write a TCP client that connects to a server and requests HTTP packets using the Socket class. The problem I'm having is when I receive a HTTP return code of 400 (Bad Request), the server disconnects me and I have to close my connection as well. Once I close my connection, the Socket class won't let me start another TCP handshake, let alone send any more packets. Here is my code for the connection:

      ;
      Socket socket;
      IPEndPoint hostEndPoint;
      string packet;

      public void run()
      {
      TCPConnect();

      packet = SendReceive("/signin.aspx", "GET");
      if (GetReturnCode(packet) == "400")
      TCPReset();

      packet = SendReceive("/index.html", "GET");

      TCPClose();
      }

      private void TCPConnect()
      {
      // Make sure there is a connection
      if (socket == null)
      {
      socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      hostEndPoint = new IPEndPoint(ipaddr, port);
      }

      socket.Connect(hostEndPoint);
      }

      // My problem is getting this function working properly
      private void TCPReset()
      {
      socket.Disconnect(true); // Close the socket connection and allow reuse of the socket
      socket.Connect(hostEndPoint); // This is ignored according to Ethereal,
      // nothing is sent even using socket.Send()
      }

      private void TCPClose()
      {
      socket.Shutdown(SocketShutdown.Both);
      socket.Close();
      }

      private string SendReceive(string strURI, strMethod)
      {
      string pcAppend = " HTTP/1.1\r\nConnection: Keep-Alive\r\n\Content-Length: 0\r\n" +
      "User-Agent: Mozilla/4.0 (Compatible; MSIE 7.0; Windows NT 5.2; .NET CLR " +
      "2.0.50727; .NET CLR 3.0.04506.30; InfoPath.1; .NET CLR 3.5.21022)\r\n" +
      "Content-Type: application/x-www-form-urlencoded\r\nHost: " + strIPAddress + "\r\n\r\n";
      byte[] pcSend = System.Text.ASCIIEncoding.ASCII.GetBytes(strMethod + strURI + pcAppend);
      byte[] bfBuffer = new byte[10240];

      socket.Send(pcSend, pcSend.Length, 0);
      socket.Receive(bfBuffer, bfBuffer.Length, SocketFlags.None);

      return System.Text.ASCIIEncoding.ASCII.GetString(bfBuffer);
      }

      ; I've been monitoring the commuications with Ethereal. .Net is closing the connection just fine, but it ignores any socket.Connect() calls after the initial connection. Any ideas?

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      It has been a while since I've used sockets (I usually go for the HttpWebRequest/HttpWebResponse classes these days) so this is a bit of a guess based on some hazy memories. It's worth a try, at least. When you open the socket for the second time you are openning a connection on the existing socket that was shut down. You may need to open a brand new socket in that case. In which case when you shutdown the old socket you might want to set socket=null; to force a new socket to be created when a new connection is requested.

      *Developer Day Scotland - Free community conference Delegate Registration Open

      Q 1 Reply Last reply
      0
      • C Colin Angus Mackay

        It has been a while since I've used sockets (I usually go for the HttpWebRequest/HttpWebResponse classes these days) so this is a bit of a guess based on some hazy memories. It's worth a try, at least. When you open the socket for the second time you are openning a connection on the existing socket that was shut down. You may need to open a brand new socket in that case. In which case when you shutdown the old socket you might want to set socket=null; to force a new socket to be created when a new connection is requested.

        *Developer Day Scotland - Free community conference Delegate Registration Open

        Q Offline
        Q Offline
        quacks_a_lot
        wrote on last edited by
        #3

        That worked! Thanks a million

        1 Reply Last reply
        0
        • Q quacks_a_lot

          I am trying write a TCP client that connects to a server and requests HTTP packets using the Socket class. The problem I'm having is when I receive a HTTP return code of 400 (Bad Request), the server disconnects me and I have to close my connection as well. Once I close my connection, the Socket class won't let me start another TCP handshake, let alone send any more packets. Here is my code for the connection:

          ;
          Socket socket;
          IPEndPoint hostEndPoint;
          string packet;

          public void run()
          {
          TCPConnect();

          packet = SendReceive("/signin.aspx", "GET");
          if (GetReturnCode(packet) == "400")
          TCPReset();

          packet = SendReceive("/index.html", "GET");

          TCPClose();
          }

          private void TCPConnect()
          {
          // Make sure there is a connection
          if (socket == null)
          {
          socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
          hostEndPoint = new IPEndPoint(ipaddr, port);
          }

          socket.Connect(hostEndPoint);
          }

          // My problem is getting this function working properly
          private void TCPReset()
          {
          socket.Disconnect(true); // Close the socket connection and allow reuse of the socket
          socket.Connect(hostEndPoint); // This is ignored according to Ethereal,
          // nothing is sent even using socket.Send()
          }

          private void TCPClose()
          {
          socket.Shutdown(SocketShutdown.Both);
          socket.Close();
          }

          private string SendReceive(string strURI, strMethod)
          {
          string pcAppend = " HTTP/1.1\r\nConnection: Keep-Alive\r\n\Content-Length: 0\r\n" +
          "User-Agent: Mozilla/4.0 (Compatible; MSIE 7.0; Windows NT 5.2; .NET CLR " +
          "2.0.50727; .NET CLR 3.0.04506.30; InfoPath.1; .NET CLR 3.5.21022)\r\n" +
          "Content-Type: application/x-www-form-urlencoded\r\nHost: " + strIPAddress + "\r\n\r\n";
          byte[] pcSend = System.Text.ASCIIEncoding.ASCII.GetBytes(strMethod + strURI + pcAppend);
          byte[] bfBuffer = new byte[10240];

          socket.Send(pcSend, pcSend.Length, 0);
          socket.Receive(bfBuffer, bfBuffer.Length, SocketFlags.None);

          return System.Text.ASCIIEncoding.ASCII.GetString(bfBuffer);
          }

          ; I've been monitoring the commuications with Ethereal. .Net is closing the connection just fine, but it ignores any socket.Connect() calls after the initial connection. Any ideas?

          N Offline
          N Offline
          Natza Mitzi
          wrote on last edited by
          #4

          socket.Disconnect(true); works only on windows XP. You should try to dispose the socket every time and then create a new one.

          Natza Mitzi

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups