473,466 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Operation not allowed on non-connected sockets

1 New Member
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 6707

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:...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.