Skip to main content
gamedev.net
🔒 Locked

Socket question

Started by vivendi 1 reply 800+ views
Original Post
vivendi
vivendi
Hey, we are currently programming a async socket connection with C#. I have a quick question. The socket doesn't always seem to be sending the data right away. So my question is, how do i have to initialise the socket to make sure that whenever i want to send a string, no matter how small, that it sends it right away. This is currently how we do it: Small socket class:

        public class SocketPacket
        {
            // Constructor neemt een socket en een client nummer
            public SocketPacket(System.Net.Sockets.Socket socket, int clientNumber)
            {
                m_currentSocket = socket;
                m_clientNumber = clientNumber;
            }
            public System.Net.Sockets.Socket m_currentSocket;
            public int m_clientNumber;

            // Buffer om de data op te slaan die gestuurd wordt door de client.
            public byte[] dataBuffer = new byte[1024];
        }
And this is how we initialise the socket:

m_mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
And this is how i currently send it:

            // Convert de data naar een byte array
            byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg);

            Socket workerSocket = (Socket)m_workerSocketList[clientNumber - 1];
            workerSocket.Send(byData);
This is the server part. Which is continously sending data. But i'm not sure if it's building up data first before sending, or if it's sending data right away. And that is what we want. It should send strings right away.
Antheus
Antheus
Disable Nagle.

Despite that, there is no option to send *right now*. It's still up to network stack to decide when to send, but that is usually good enough, especially if you're not exceeding bandwidth capacity. But that's a problem of limited resources.

Generally, UDP is preferred for this type of tasks. As long as you're using TCP, if any of packets get lost, receiver will be waiting for resends, so regardless of how fast one sends, there will be a minimum of round-trip delay when receiving.
vivendi
vivendi
whoa, thanks a million! That was exactly what i needed! :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.