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. Socket Connections

Socket Connections

Scheduled Pinned Locked Moved C#
sysadminhelpquestion
2 Posts 2 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.
  • R Offline
    R Offline
    Rick van Woudenberg
    wrote on last edited by
    #1

    Dear all, I'm trying to get my computer to listen on a port and pass on the received data ( after some modification ) over Multicast. I'm receiving data on the socket every second. I wrote the code below and it works perfect. The problem that I require some assistance with is pretty simply. Everytime a string of data is received, the socket is closed and new socket is opened to wait for the next string. When the next string of data comes in, it gets processed and the socket is closed again. This means that after a period of time, lots of socket connections are made. One of them is active and the others are in a TIME_WAIT state ( and that list keeps growing ) Is there a way around this ?

    Int32 port = 31008;

                IPAddress localAddr = IPAddress.Any;
                server = new TcpListener(localAddr, port);
                server.Start();
    
                // Buffer for reading data
                Byte\[\] bytes = new Byte\[256\];
                String data = null;
    
                while (true)
                {
                    client = server.AcceptTcpClient();
                    data = null;
                    NetworkStream stream = client.GetStream();
    
                    int i;
    
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        data = data.ToUpper();
                        byte\[\] msg = System.Text.Encoding.ASCII.GetBytes(data);
                        
                        SendMulticast(MultiCastIP, MultiCastPort, msg);
                    }
                    client.Close();
                }
    

    kind regards, Rick

    M 1 Reply Last reply
    0
    • R Rick van Woudenberg

      Dear all, I'm trying to get my computer to listen on a port and pass on the received data ( after some modification ) over Multicast. I'm receiving data on the socket every second. I wrote the code below and it works perfect. The problem that I require some assistance with is pretty simply. Everytime a string of data is received, the socket is closed and new socket is opened to wait for the next string. When the next string of data comes in, it gets processed and the socket is closed again. This means that after a period of time, lots of socket connections are made. One of them is active and the others are in a TIME_WAIT state ( and that list keeps growing ) Is there a way around this ?

      Int32 port = 31008;

                  IPAddress localAddr = IPAddress.Any;
                  server = new TcpListener(localAddr, port);
                  server.Start();
      
                  // Buffer for reading data
                  Byte\[\] bytes = new Byte\[256\];
                  String data = null;
      
                  while (true)
                  {
                      client = server.AcceptTcpClient();
                      data = null;
                      NetworkStream stream = client.GetStream();
      
                      int i;
      
                      while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                      {
                          data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                          data = data.ToUpper();
                          byte\[\] msg = System.Text.Encoding.ASCII.GetBytes(data);
                          
                          SendMulticast(MultiCastIP, MultiCastPort, msg);
                      }
                      client.Close();
                  }
      

      kind regards, Rick

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      The TIME_WAIT state for a period of time is normal/default socket behavior. If you're absolutely sure all data is sent, then you may be able to set the LingerState to on/0sec:

      LingerOption lingerOption = new LingerOption (true, 0);
      client.LingerState = lingerOption;

      You also should call Close() on your NetworkStream :) Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      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