How to stop C# from buffering the data it sends through socket?

Viewed 23

I have been trying to send data through C# to Python using socket. I tried to program the sender using Python to make sure everything is set up properly using the Python code be

import socket
import sys


HEADERSIZE = 10
raw_msg = "a"
print(f"{len(raw_msg):<{HEADERSIZE}}"+raw_msg)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((socket.gethostname(), 1235))
s.listen(5)
clientsocket, address = s.accept()
print(f"Connection from {address} has been established.")

while True:


    raw_msg = input("input: ")

    msg = f"{len(raw_msg):<{HEADERSIZE}}"+raw_msg
    clientsocket.send(bytes(msg,"utf-8"))

    if raw_msg == "bye":
        break
    s.detach()
    s.close()

When the code is executed, the packages sent look like:

b'39        readEx'
b'cel|x,test_Purch'
b'ase_Forecast.xls'
b'x'
b'45        printD'
b'ataFrames|x,test'
b'_Purchase_Foreca'
b'st.xlsx'

So, I did it using C# using the code below:

 class Connection
    {
        int ServerPortNum;
        Socket connectionSocket;
        Socket welcomingSocket;

        public Connection() {
            ServerPortNum = 1235;
            IPEndPoint serverEndPoint = new System.Net.IPEndPoint(IPAddress.Loopback,ServerPortNum);
            welcomingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            welcomingSocket.NoDelay = true;

            Console.WriteLine("Starting");
            welcomingSocket.Bind(serverEndPoint);
            welcomingSocket.Listen(5);
        }

        public void Accept() {
            connectionSocket = welcomingSocket.Accept();
        }

        public void send(string raw_msg_string) 
        {   
            char[] lengthCharArray = raw_msg_string.Length.ToString().ToCharArray();
            string whiteSpace = String.Concat(Enumerable.Repeat(" ", 10-lengthCharArray.Length));
            string header = new string(lengthCharArray) + whiteSpace;

            Console.WriteLine(header + raw_msg_string);

            byte[] msg = Encoding.ASCII.GetBytes(header + raw_msg_string);
            connectionSocket.Send(msg); 
        }

        public void ShutDown() {
            connectionSocket.LingerState = new LingerOption(false, 0);
            connectionSocket.Shutdown(SocketShutdown.Both);
            connectionSocket.Disconnect(false);
            connectionSocket.Close();
        }
    }

However the problem is C# is buffering the data it sends which is causing problems for my application. So, the send code for the same input looks like this:

b'39        readEx'
b'cel|x,test_Purch'
b'ase_Forecast.xls'
b'x45        print'
b'DataFrames|x,tes'
b't_Purchase_Forec'
b'ast.xlsx'
b''

So my question is, how can I prevent C# from buffering the data and cause it to send the package if the message is over instead of buffering it so that the packages look like the ones in python?

0 Answers
Related