473,287 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Operation not allowed on non-connected sockets

Hi I'm trying to create a tcp-server and running into issues getting the message "The operation is not allowed on non-connected sockets"

The project is an AvaloniaUI project...when I create my MainWindow I start up a TcpListener and run it on a background thread


Expand|Select|Wrap|Line Numbers
  1. private TcpListener _listener;
  2.         public MainWindow()
  3.         {
  4.             InitializeComponent();
  5.             DataContext = _vm;
  6.  
  7.  
  8.             _listener = new TcpListener(IPAddress.Any, 1025);
  9.             _listener.Start();
  10.             Thread smtpThread = new Thread(StartSmtpLocal);
  11.             smtpThread.IsBackground = true;
  12.             smtpThread.Start();
  13.  
  14.         }
StartSmtpLocal looks like

Expand|Select|Wrap|Line Numbers
  1. void StartSmtpLocal()
  2.         {
  3.                 while (true)
  4.                 {
  5.                     Console.WriteLine("Waiting for connection...");
  6.                     TcpClient client = _listener.AcceptTcpClient();
  7.                     SMTPServer handler = new SMTPServer();
  8.  
  9.                     handlers.Add(handler); 
  10.                     handler.Initialize(client, SmtpMessageComplete);
  11.                     Thread thread = new Thread(handler.Run);
  12.                     // thread.IsBackground = true;
  13.                     thread.Start();
  14.                 } 
  15.         }
SmtpServer -

Expand|Select|Wrap|Line Numbers
  1. public class SMTPServer
  2.     {
  3.         private TcpClient _client;
  4.         private Action<MimeMessage> _finishAction;
  5.         private MimeMessage _messageRead;
  6.         public void Initialize(TcpClient client, Action<MimeMessage> finishAction)
  7.         {
  8.             _client = client;
  9.             _finishAction = finishAction;
  10.         }
  11.  
  12.         public void Run()
  13.         {
  14.             Write("220 localhost -- Fake proxy server");
  15.             // string message = String.Empty;
  16.             // MimeMessage message;
  17.             string message;
  18.  
  19.             while (true)
  20.             {
  21.                 message = Read();
  22.                 // message = Read();
  23.  
  24.                 if (message.Length > 0)
  25.                 {
  26.                     if (message.StartsWith("QUIT"))
  27.                     {
  28.                         Console.WriteLine("Closing TCP Client");
  29.                         _client.Close();
  30.                         break;
  31.                     }
  32.                     if (message.StartsWith("EHLO"))
  33.                     {
  34.                         Write("250 OK");
  35.                     }
  36.  
  37.                     if (message.StartsWith("RCPT TO"))
  38.                     {
  39.                         Write("250 OK");
  40.                     }
  41.  
  42.                     if (message.StartsWith("MAIL FROM"))
  43.                     {
  44.  
  45.                         Write("250 OK");
  46.                     }
  47.  
  48.                     if (message.StartsWith("DATA"))
  49.                     {
  50.                         Write("354 Start mail input; end with");
  51.                         string msg = Read();
  52.                         MemoryStream stream = new MemoryStream();
  53.                         using (StreamWriter writer = new StreamWriter(stream))
  54.                         {
  55.                             writer.Write(msg);
  56.                             writer.Flush();
  57.                             stream.Position = 0;
  58.  
  59.                             // https://github.com/jstedfast/MimeKit
  60.                             _messageRead = MimeMessage.Load(stream);
  61.                             Console.WriteLine();
  62.                         }
  63.  
  64.                         stream.Close();
  65.  
  66.                         Console.WriteLine();
  67.                         Console.WriteLine($"Message from client {message}");
  68.                         Write("250 OK");
  69.                     }
  70.                 }
  71.             }
  72.             Console.WriteLine("Invoking finish action");
  73.             _finishAction.Invoke(_messageRead);
  74.         }
  75.  
  76.         private void Write(string message)
  77.         {
  78.             using (NetworkStream clientStream = _client.GetStream())
  79.             {
  80.                 ASCIIEncoding encoder = new ASCIIEncoding();
  81.                 byte[] buffer = encoder.GetBytes(message + "\r\n");
  82.  
  83.                 clientStream.Write(buffer, 0, buffer.Length);
  84.                 clientStream.Flush();
  85.             }
  86.             // NetworkStream clientStream = _client.GetStream();
  87.         }
  88.  
  89.         private string Read()
  90.         {
  91.             byte[] messageBytes = new byte[8192];
  92.             int bytesRead = 0;
  93.             NetworkStream clientStream = _client.GetStream();
  94.             ASCIIEncoding encoder = new ASCIIEncoding();
  95.             bytesRead = clientStream.Read(messageBytes, 0, 8192);
  96.             string strMessage = encoder.GetString(messageBytes, 0, bytesRead);
  97.             return strMessage;
  98.         }
  99.  
Line 93 in SmtpServer is where the error is happening

I have no idea what is causing the Client to disconnect so quickly after it's accepted. Any guidance in the right direction is greatly appreciated!
May 15 '22 #1
0 6528

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up...
3
by: David | last post by:
Hi, Ive been trying to work this out for the past 2 days now and im not getting anywhere fast. The problem i have is that i am using Asynchronous sockets to create a Socket Client library....
6
by: Ted | last post by:
Hello all, Please could somebody with a little time on their hands help me with my net sockets program? The aim is to send multiple transactions via C# network stream and read them back. The...
15
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up...
1
by: Jim | last post by:
Hi Guys, i've searched through different n-groups but couldn't find any answers to my problem. If my TCPserver keeps running after a few hours my client couldnt connect anymore in my TCPserver,...
3
by: Curious | last post by:
Hi, I am using the following code to transfer messages between client and server. Upon running that code I am getting the following error message in method ReceiveFromClient on line Stream s...
1
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
4
by: keithseah | last post by:
Hi all, i've been having this problem and its kiiling me! i'm a newbie at this so i hope someone would be able to help me. picture link:...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.