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. .NET Sockets Problem [modified]

.NET Sockets Problem [modified]

Scheduled Pinned Locked Moved C#
csharphelpwinformstutorial
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.
  • M Offline
    M Offline
    Mike Bentzen
    wrote on last edited by
    #1

    Hello Everyone. I am currently having a lot of trouble with .NET sockets. I am trying to create a client with a windows forms interface. I have attempted to create a communication class which basically contains some functions for connecting and managing the connection. public class connection { private TCPClient _client = new TCPClient(); public bool Connect(string hostname, int port) { try { _client.Connect(hostname, port); return true; } catch { return false; } } public bool IsConnected() { try { if (_client.Connected) { return true; } else { return false; } } catch { return false; } } } Is a really basic connection class. I have multiple forms which I would like them to be able to control the connection and receive and display information on them. I understand that the usual way for all form instances to access the same things, is to declare those things as static. I have tried to make this class a static class but the TCPClient throws an error telling me that you can't have a static instance. I don't want multiple instances of this class. The client only needs to handle one connection at a time. I have gone through all C# tutorials and articles on code project, and they are really helpful if you are only using one form (with delegates) and custom event handlers. I want to be able to receive information from the socket, deserialize the data and use that data to populate classes, and form controls (for example: list box - of connected clients.) I would really appreciate it if you could explain why I can't have a static instance and maybe possibly links to tutorials or code samples of something similar to what I am describing above so I can try to understand how it works. Kindest Regards, Mike

    modified on Friday, April 25, 2008 11:20 PM

    D 1 Reply Last reply
    0
    • M Mike Bentzen

      Hello Everyone. I am currently having a lot of trouble with .NET sockets. I am trying to create a client with a windows forms interface. I have attempted to create a communication class which basically contains some functions for connecting and managing the connection. public class connection { private TCPClient _client = new TCPClient(); public bool Connect(string hostname, int port) { try { _client.Connect(hostname, port); return true; } catch { return false; } } public bool IsConnected() { try { if (_client.Connected) { return true; } else { return false; } } catch { return false; } } } Is a really basic connection class. I have multiple forms which I would like them to be able to control the connection and receive and display information on them. I understand that the usual way for all form instances to access the same things, is to declare those things as static. I have tried to make this class a static class but the TCPClient throws an error telling me that you can't have a static instance. I don't want multiple instances of this class. The client only needs to handle one connection at a time. I have gone through all C# tutorials and articles on code project, and they are really helpful if you are only using one form (with delegates) and custom event handlers. I want to be able to receive information from the socket, deserialize the data and use that data to populate classes, and form controls (for example: list box - of connected clients.) I would really appreciate it if you could explain why I can't have a static instance and maybe possibly links to tutorials or code samples of something similar to what I am describing above so I can try to understand how it works. Kindest Regards, Mike

      modified on Friday, April 25, 2008 11:20 PM

      D Offline
      D Offline
      Derek Bartram
      wrote on last edited by
      #2

      It sounds a little suspect the way you are trying to do it.... but the way to do what you are trying is;

      public abstract class connection {
      private static TCPClient _client = new TCPClient();

       public static void Connect(String hostname, int port) {
            try {
                 \_client.Connect(hostname, port);
                 return true;
            }
            catch (Exception e) {
                 return false;
            }
       }
      
       public static bool IsConnected {
            get {
                 return \_client.Connected;
            }
       }
      

      }

      You would the access via;

      connection.Connect("192.168.0.1", "50");
      Console.WriteLine("Is it connected? " + connection.IsConnected.ToString());

      Someone else would be better able to answer this, but this seams very insecure and there is no protection of the TCPClient which potentially is sending user data?

      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