TCP connection
-
Hi! If i make a TCP server and i try to connect, the firs time it succeed but then i get an error message: "No connection could be made because the target machine actually refuse it". :(
private void AddLine(TextBox tb, string text) { if (tb.InvokeRequired) { AddLineDel d = new AddLineDel(AddLine); this.Invoke(d, new object[] { tb, text }); } else tb.Text += "\r\n" + text; } private void Listen_button_Click(object sender, EventArgs e) { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port); sock.Bind(ipep); sock.Listen(2); sock.BeginAccept(new AsyncCallback(AcceptConn), sock); } private void AcceptConn(IAsyncResult iar) { Socket oldserver = (Socket)iar.AsyncState; client = oldserver.EndAccept(iar); AddLine(textBox1, "Connection from:" + client.RemoteEndPoint.ToString()); Thread receiver = new Thread(new ThreadStart(ReceiveData)); receiver.IsBackground = true; receiver.Start(); } private void ReceiveData() { int recv; string datastr = ""; while (true) { if (client.Connected) { cleardata(); recv = client.Receive(data); datastr = Encoding.ASCII.GetString(data); if (datastr == "exit") break; AddLine(textBox1, datastr); } } client.Close(); AddLine(textBox1, "Connection closed"); return; } }
If anyone knows how to make a TCP connection where the same client can connect and disconnect multiple times. Please help me! Thanks. Regards, Marta Paniti-------------------------------- visit: http://pmartike.deviantart.com/
-
Hi! If i make a TCP server and i try to connect, the firs time it succeed but then i get an error message: "No connection could be made because the target machine actually refuse it". :(
private void AddLine(TextBox tb, string text) { if (tb.InvokeRequired) { AddLineDel d = new AddLineDel(AddLine); this.Invoke(d, new object[] { tb, text }); } else tb.Text += "\r\n" + text; } private void Listen_button_Click(object sender, EventArgs e) { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port); sock.Bind(ipep); sock.Listen(2); sock.BeginAccept(new AsyncCallback(AcceptConn), sock); } private void AcceptConn(IAsyncResult iar) { Socket oldserver = (Socket)iar.AsyncState; client = oldserver.EndAccept(iar); AddLine(textBox1, "Connection from:" + client.RemoteEndPoint.ToString()); Thread receiver = new Thread(new ThreadStart(ReceiveData)); receiver.IsBackground = true; receiver.Start(); } private void ReceiveData() { int recv; string datastr = ""; while (true) { if (client.Connected) { cleardata(); recv = client.Receive(data); datastr = Encoding.ASCII.GetString(data); if (datastr == "exit") break; AddLine(textBox1, datastr); } } client.Close(); AddLine(textBox1, "Connection closed"); return; } }
If anyone knows how to make a TCP connection where the same client can connect and disconnect multiple times. Please help me! Thanks. Regards, Marta Paniti-------------------------------- visit: http://pmartike.deviantart.com/
pmartike wrote:
If anyone knows how to make a TCP connection where the same client can connect and disconnect multiple times.
Sure. In your code above, you're only listening for the first connection. Your
Listen_Button_Click()
method callsBeginReceive()
and passes it the delegate forAcceptConn()
. So far so good, but once that method is called your socket isn't listening anymore. You have to re-wire the callback by invokingBeginReceive()
again to listen for more incoming connections. Share and enjoy. Sean -
pmartike wrote:
If anyone knows how to make a TCP connection where the same client can connect and disconnect multiple times.
Sure. In your code above, you're only listening for the first connection. Your
Listen_Button_Click()
method callsBeginReceive()
and passes it the delegate forAcceptConn()
. So far so good, but once that method is called your socket isn't listening anymore. You have to re-wire the callback by invokingBeginReceive()
again to listen for more incoming connections. Share and enjoy. SeanThanks! Problem solved :)
-------------------------------- visit: http://pmartike.deviantart.com/