Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi i work on a small project which includes socketcommunication.
Specifically a TLS connection to a server. 
The Serverside works so far. But the client seems to block all parallelism tasks.

Does anybody know where to get a sample with Sockets using multiple ports at the client side?
Could it be the problem that TCPClient only supports one port on the clientside?

This code should be the backend of a simple application. 
As you can see i also tried to Queue the data. Simply ignore it. 

here comes my client code:
C#
    public class IMClient
    {
        public IMClient(string ip, int port)
        {
            Server = ip;
            Port = port;
        }
        Task tcpThread;      // Receiver
        bool _conn = false;    // Is connected/connecting?


        private string Server = "";  // Address of server. In this case - local IP address.
        private int Port;

        internal MultithreadedQueue<IDWrapper> CommandInput { get => commandInput; set => commandInput = value; }
        internal MultithreadedQueue<IDWrapper> CommandResult { get => commandResult; set => commandResult = value; }

        private MultithreadedQueue<IDWrapper> commandInput = new MultithreadedQueue<IDWrapper>(100);
        private MultithreadedQueue<IDWrapper> commandResult = new MultithreadedQueue<IDWrapper>(100);

        TcpClient client;
        NetworkStream netStream;
        SslStream ssl;
        BinaryReader br;
        BinaryWriter bw;
        Stream input;
        Stream output;
        
        string s;
        // Start connection thread and login or register.
        public void connect()
        {
          
                _conn = true;
                tcpThread = Task.Run(SetupConn);
         
            
        }
        public void Disconnect()
        {
            if (_conn)
                
                CloseConn();
        }

        private async Task<Task<object>> SetupConn()  // Setup connection and login
        {
            bool retry = false;

            do {
                try
                {
                    client = new TcpClient(Server, Port);  // Connect to the server.
                    client.NoDelay = true;
                    client.SendBufferSize = 1024;
                    client.ReceiveBufferSize = 4096;
                    netStream = client.GetStream();
                    ssl = new SslStream(netStream, false, new RemoteCertificateValidationCallback(ValidateCert));
                    //todo Error Handling
                    ssl.AuthenticateAsClient("InstantMessengerServer");
                    // Now we have encrypted connection.
                    
                    br = new BinaryReader(ssl, Encoding.UTF8);
                    bw = new BinaryWriter(ssl, Encoding.UTF8);
                    retry = false;
                }
                catch (Exception e)
                {
                    retry = true;
                }
            } while (retry);



            IDWrapper idw = commandInput.Dequeue();
            string content = idw.Content;
            string id = idw.ID;
            SendCallback(ref content, ref id);
            CloseConn();
            return Task.FromResult<object>(null);
        }








public async Task<string> SendCallbackUnbuffered(string action,  int recivebuffersize = 4096)
        {
            string res = "";
            try
            {
                    byte[] a = StringToByteArray(action);
                    if(!_conn)
                    {
                         connect();
                    }
                    while (bw == null);
                    ssl.WriteAsync(a, 0, a.Length);

                    byte[] r = new byte[recivebuffersize];
                    ssl.ReadAsync(r, 0, recivebuffersize);
                    CloseConn();

            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
            }
            return res;
        }


What I have tried:

I tried several things which should make the SendCallback Method awaitable.
But it does not work.

Can anyone provide me a example of a nonblocking socketcommunication in c#, or at least a hint how to do it?
Posted
Updated 26-Jan-21 6:34am

1 solution

You need to set the blocking mode on the socket: Socket.Blocking Property (System.Net.Sockets) | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 14530069 26-Jan-21 13:51pm    
yes this option is only available to Socket...
I get this point. But i do not know how to wrap the socket with sslsteam...
Richard MacCutchan 26-Jan-21 14:08pm    
The Socket is a property of the TcpClient object. Spend some time reading the documentation for both classes to get a good understanding of them.
Member 14530069 28-Jan-21 5:06am    
Yes, i already found how to set this property... but i think that the async example of microsoft should also work...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900