Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. TCP connection

TCP connection

Scheduled Pinned Locked Moved C#
helpcomsysadmintutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pmartike
    wrote on last edited by
    #1

    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/

    S 1 Reply Last reply
    0
    • P pmartike

      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/

      S Offline
      S Offline
      Sean Michael Murphy
      wrote on last edited by
      #2

      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 calls BeginReceive() and passes it the delegate for AcceptConn(). So far so good, but once that method is called your socket isn't listening anymore. You have to re-wire the callback by invoking BeginReceive() again to listen for more incoming connections. Share and enjoy. Sean

      P 1 Reply Last reply
      0
      • S Sean Michael Murphy

        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 calls BeginReceive() and passes it the delegate for AcceptConn(). So far so good, but once that method is called your socket isn't listening anymore. You have to re-wire the callback by invoking BeginReceive() again to listen for more incoming connections. Share and enjoy. Sean

        P Offline
        P Offline
        pmartike
        wrote on last edited by
        #3

        Thanks! Problem solved :)

        -------------------------------- visit: http://pmartike.deviantart.com/

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups