Re: requesting some hints and libraries-Csharp

@kianoosh, multiple things:
1. I noticed your byte arrays. They are ridiculously large for a networking application. You should never accept packets in 20000-byte chunks. That leaves so many vulnerabilities open to your system its absolutely unimaginable. The most efficient byte array size for your server is exactly the amount of data you know will be sent between the client and server; however, since this can never be anticipated, 1024 is the recommended byte array size for 99 percent of all networking applications.
2. You have no error handling here, and don't use half the namespaces you import. If your going to write a networking application, you first need to use error handling *everywhere*. Because an error may occur, any time, anywhere, and you certainly won't be expecting it.
3. You use threads and no mutexes. In this kind of application, a thread is entirely pointless. You won't acomplish anything with that, and it may explain your communications problem. What your doing here is joining both threads together and executing them at the same time, but they're pretty much being given access to the same data at the same time, causing a deadlock. And this is the first trip that most programmers, myself included, make when using threads: accessing the same data at the same time among two or more threads. This causes a deadlock; that is, both threads just sit there, waiting for one to release the resource they're trying to access, and neither ever does. On the other hand, non-use of mutexes can cause data races and many other problems you'd best deal with before they can ever arise.
Now, my recommendations:
1. Get rid of all those unused imports. You don't use them; it doesn't make sense to import them.
2. Decrease your byte array size to 1024 bytes, maximum. Never set your byte array size below 512 bytes. In some cases it is fine to have a buffer size of 2048 or even 4096 bytes each, but keep in mind that this array is constantly being allocated and released, and the higher the array size, the more memory usage your server will use.
3. Fix the error handling with generic try/catch blocks around everything, including your main() function, send(), server() and client(). And I don't mean wrap small little statements in try/catch, either. I mean wrap the entire function in them.
4. Either get rid of threads entirely, as, like I said, they're pointless here, since you can split them into separate apps entirely, or use mutexes and careful locking. Below is a sample of how to use a mutex:
private Mutex mutex = new Mutex();
//... in the receive method, wait for safe access...
            while(true)
            {
mutex.WaitOne();
                NetworkStream net = client.GetStream();
                byte[] bfrom = new byte[1024];
                net.Read(bfrom, 0, bfrom.Length);
                string event_message = Encoding.ASCII.GetString(bfrom);
                if(event_message=="pat")
                {
                    send(net, "stuff");
                    net.Flush();
                }
mutex.ReleaseMutex();
}
}
// Local function? End the server func.
            void send(NetworkStream net, string data)
            {
// We've already acquired a mutex by this point, so don't try reacquiring it (causes an indefinite block).
                byte[] outst = Encoding.ASCII.GetBytes(data);
                net.Write(outst, 0, outst.Length);
                            }
// ... same with the server client.
private        static void client()
        {
            TcpClient client = new TcpClient();
            client.Connect("127.0.0.1", 11232);
            while(true)
            {
  mutex.WaitOne();
                NetworkStream net = client.GetStream();
                byte[] bfrom = new byte[1024];
                string event_message = Encoding.ASCII.GetString(bfrom);
                bool b = false;
                if (!b) // No need to use b==false here, redundant
                {
                    string dat = "pat";
                    byte[] outst = Encoding.ASCII.GetBytes(dat);
                    net.Write(outst, 0, outst.Length);
                    net.Flush();
b=true;
                    break; // If we don't break, the condition won't be invalidated
                }
                if (event_message=="stuff")
                    {
                    Console.WriteLine("you are stuff");
                }
mutex.ReleaseMutex();
            }
        }
// Now we can use the threads together.
You can read more on Mutexes at https://msdn.microsoft.com/en-us/librar … 10%29.aspx

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : kianoosh via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Munawar via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : kianoosh via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : kianoosh via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Xsense via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Xsense via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Xsense via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : defender via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Xsense via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : defender via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : defender via Audiogames-reflector

Reply via email to