"No connection could be made because the target machine actively refused it"
-
I get this error when trying to send something between two apps over TCP. The weird part is that I can send one time without any trouble, but whenever I try a second time, I get this error. I use this code for the sending app:
private void B_Send_Click(object sender, EventArgs e)
{
string send = "";
send = TB_Quotes.Text;TcpClient tcp = new TcpClient(TB\_Host.Text, (int)UD\_Port.Value); NetworkStream ns = tcp.GetStream(); StreamWriter writer = new StreamWriter(ns); writer.WriteLine(send); writer.Flush(); ns.Close(); writer.Close(); tcp.Close(); }
And this code for the receiving app:
public Form1()
{
InitializeComponent();
Thread treceive = new Thread(new ThreadStart(Listen));
treceive.Start();
}public void Listen() { int port = 2112; string result = ""; IPAddress localaddress = IPAddress.Parse("127.0.0.1"); TcpListener listener = new TcpListener(localaddress, port); listener.Start(); TcpClient client = listener.AcceptTcpClient(); NetworkStream ns = client.GetStream(); StreamReader reader = new StreamReader(ns); result = reader.ReadToEnd(); Invoke(new UpdateDisplayDelegate(UpdateDisplay), new object\[\] {result}); } public void UpdateDisplay(string text) { TB\_Quotes.Text = text; }
I'm making sure the port is 2112 (if not, the first time would not be successful), but I don't understand why the 2nd time won't work. Thanks.
-
I get this error when trying to send something between two apps over TCP. The weird part is that I can send one time without any trouble, but whenever I try a second time, I get this error. I use this code for the sending app:
private void B_Send_Click(object sender, EventArgs e)
{
string send = "";
send = TB_Quotes.Text;TcpClient tcp = new TcpClient(TB\_Host.Text, (int)UD\_Port.Value); NetworkStream ns = tcp.GetStream(); StreamWriter writer = new StreamWriter(ns); writer.WriteLine(send); writer.Flush(); ns.Close(); writer.Close(); tcp.Close(); }
And this code for the receiving app:
public Form1()
{
InitializeComponent();
Thread treceive = new Thread(new ThreadStart(Listen));
treceive.Start();
}public void Listen() { int port = 2112; string result = ""; IPAddress localaddress = IPAddress.Parse("127.0.0.1"); TcpListener listener = new TcpListener(localaddress, port); listener.Start(); TcpClient client = listener.AcceptTcpClient(); NetworkStream ns = client.GetStream(); StreamReader reader = new StreamReader(ns); result = reader.ReadToEnd(); Invoke(new UpdateDisplayDelegate(UpdateDisplay), new object\[\] {result}); } public void UpdateDisplay(string text) { TB\_Quotes.Text = text; }
I'm making sure the port is 2112 (if not, the first time would not be successful), but I don't understand why the 2nd time won't work. Thanks.
The Listen method ends after the Invoke bit at the end (also terminating the started thread). Thus it can not accept new connections (client = listener.AcceptTcpClient(). You'd have to have some kind of loop where the server continually accepts connections. From within the loop you could start threads that do the handling for each individual connection. Cheers Manfred
-
The Listen method ends after the Invoke bit at the end (also terminating the started thread). Thus it can not accept new connections (client = listener.AcceptTcpClient(). You'd have to have some kind of loop where the server continually accepts connections. From within the loop you could start threads that do the handling for each individual connection. Cheers Manfred
Thanks. I added a loop and now it works just fine.
-
Thanks. I added a loop and now it works just fine.
If the answer was helpful, I'd really appreciate a vote.
-
I get this error when trying to send something between two apps over TCP. The weird part is that I can send one time without any trouble, but whenever I try a second time, I get this error. I use this code for the sending app:
private void B_Send_Click(object sender, EventArgs e)
{
string send = "";
send = TB_Quotes.Text;TcpClient tcp = new TcpClient(TB\_Host.Text, (int)UD\_Port.Value); NetworkStream ns = tcp.GetStream(); StreamWriter writer = new StreamWriter(ns); writer.WriteLine(send); writer.Flush(); ns.Close(); writer.Close(); tcp.Close(); }
And this code for the receiving app:
public Form1()
{
InitializeComponent();
Thread treceive = new Thread(new ThreadStart(Listen));
treceive.Start();
}public void Listen() { int port = 2112; string result = ""; IPAddress localaddress = IPAddress.Parse("127.0.0.1"); TcpListener listener = new TcpListener(localaddress, port); listener.Start(); TcpClient client = listener.AcceptTcpClient(); NetworkStream ns = client.GetStream(); StreamReader reader = new StreamReader(ns); result = reader.ReadToEnd(); Invoke(new UpdateDisplayDelegate(UpdateDisplay), new object\[\] {result}); } public void UpdateDisplay(string text) { TB\_Quotes.Text = text; }
I'm making sure the port is 2112 (if not, the first time would not be successful), but I don't understand why the 2nd time won't work. Thanks.
Megidolaon wrote:
writer.Flush();
one remark: you oinly need to flush when you don't close right away; here you want to close, so don't flush, there is no need to. However, you should close nested things in reverse order: since you opened ns and then writer, you should close writer, then ns. That way, no flush is required. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.