.NET Sockets Problem [modified]
-
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, Mikemodified on Friday, April 25, 2008 11:20 PM
-
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, Mikemodified on Friday, April 25, 2008 11:20 PM
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?