Note that there are some explanatory texts on larger screens.

plurals
  1. POSocket connection issues
    primarykey
    data
    text
    <hr> <p>My answer:</p> <p>After getting annoyed, I have found a solution. The problem was indeed C# either C#'s garbage collector or C#'s multithreading, it probably thought the object was no longer needed within THAT thread, and deleted it. The solution was found as follows:</p> <p>I implemented the ClientThread into the Server class, passing the Client object as a parameters, this minor change made it work. Thank you for all your responses, if anyone in the future has this problem maybe it wasn't C#'s garbage collector. But C# mutithreading OR networking must be done within the same class. I kept my client class and just made the thread object run the function within the Server class.</p> <p>If anyone can figure out what my problem was, feel free to comment so I can expand my little knowledge of C#'s memory management.</p> <p>Thanks again to all the people who attempted to help me in this thread.</p> <hr> <p>Original Question</p> <p>I'm a C++ programmer so I'm used to managing memory myself, and I'm really not sure how to solve this problem.</p> <p>For instance in C++:</p> <pre><code>while(true) { void* ptr = new char[1000]; } </code></pre> <p>This would be an obvious memory leaking program, so I need to go ahead and clean it up with:</p> <pre><code>delete ptr; </code></pre> <p>But there are cases when I want to create memory for use in a different thread and I DO NOT WANT IT DELETED AFTER THE LOOP.</p> <pre><code>while(true) { socket.Accept(new Client()); } //////////Client Constructor//////////// Client() { clientThread.Start(); } </code></pre> <p>This snippet is basically what I want to do in C#, but my client connects then disconnects immediately, I'm assuming this is because at the end of the <code>while</code> loop my <code>new Client()</code> is being deleted by our favorite Garbage Collector.</p> <p>So my question is, how do I get around this and make it NOT delete my object.</p> <p>Many have replied saying various things about having other links to it in the code. I forgot to mention that I also save the new client in a list of clients located globally</p> <pre><code>List&lt;Client&gt; clients; //inside loop clients.Add(new Client(socket.Accept())); </code></pre> <hr> <p>Ok because I'm unsure if I'm missing more information here is the ACTUAL code snippet</p> <pre><code>// Server class internal Socket socket { get; set; } internal Thread thread { get; set; } internal List&lt;Client&gt; clients { get; set; } internal void Init() { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); thread = new Thread(AcceptThread); } internal void Start(int port,List&lt;Client&gt; clients) { var ipep = new IPEndPoint(IPAddress.Any, port); this.socket.Bind(ipep); this.socket.Listen(10); this.clients = clients; this.thread.Start(); } internal void End() { socket.Close(); thread.Abort(); } internal void AcceptThread() { int ids = 0; while (true) { Client client = new Client(); client.Init(socket.Accept()); client.clientid = ids++; client.Start(); clients.Add(client); } } // Client class public class Client { ..... #region Base Code internal void Init(Socket socket) { this.socket = socket; this.status = new SocketStatus(); this.thread = new Thread(ClientThread); this.stream = new Stream(); } internal void Start() { thread.Start(); } internal void Close() { socket.Close(); status = SocketStatus.Null; thread.Abort(); } internal void ClientThread() { try { while (true) { byte[] data = new byte[1]; int rec = socket.Receive(data); if (rec == 0) throw new Exception(); else stream.write(data[0]); } } catch(Exception e) { Close(); } } #endregion } </code></pre> <p>I thank you for all your replies.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload