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. TCPClient and TCPListener

TCPClient and TCPListener

Scheduled Pinned Locked Moved C#
databasehelpcsharpwcfsysadmin
5 Posts 3 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.
  • M Offline
    M Offline
    michaelgr1
    wrote on last edited by
    #1

    Hello, I am writing a project which sends SQL query to server, it runs it and returns back to client the results using streaming. The flow is sending query,clientIP and port the client is going to listen to using WCF. before sending the WCF, the client opens a listener on the chosen port, and waits for a networkStream from the server. this done in a seperate thread. meanwhile the client sends the WCF request (with query,client ip, client port) to server and it returns the results to the waiting client on the right port. All this done in seperate thread so i can run several queries simultanesly from the same client. After i received the results i am trying to close the TcpListener and TcpClient opened in the client side (in the opened thread). But when the other thread running it throws exception: Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCal. If i don't close the listener and client it works fine. So how can i close the TcpListener and TcpClient in the thread without causing problem to the second thread . TcpListener and TcpClient are not static. Code: private void OpenListenerTask(int port,AutoResetEvent resetEvent) { try { NetworkStream getStream = StartListenerGetStream(port, resetEvent); string localIp = Dns.GetHostAddresses(Dns.GetHostName())[0].ToString(); var networkReader = new Reader(getStream, localIp, _sendPort.Value, _query); networkReader.Start(); } catch (Exception e) { Console.WriteLine(e.GetBaseException().Message); } //close TcpClient & TcpListener after data/error received _client.Close(); _listener.Stop(); } private TcpClient _client; private TcpListener _listener; private NetworkStream StartListenerGetStream(int port,EventWaitHandle resetEvent) { var tcpListener = new TcpListener(port); tcpListener.Start(); _listener = tcpListener; //tcpListener.Server.Blocking = false; resetEvent.Set(); var tcpClient = tcpListener.AcceptTcpClient(); _client = tcpClient; return tcpClient.GetStream(); }

    J 1 Reply Last reply
    0
    • M michaelgr1

      Hello, I am writing a project which sends SQL query to server, it runs it and returns back to client the results using streaming. The flow is sending query,clientIP and port the client is going to listen to using WCF. before sending the WCF, the client opens a listener on the chosen port, and waits for a networkStream from the server. this done in a seperate thread. meanwhile the client sends the WCF request (with query,client ip, client port) to server and it returns the results to the waiting client on the right port. All this done in seperate thread so i can run several queries simultanesly from the same client. After i received the results i am trying to close the TcpListener and TcpClient opened in the client side (in the opened thread). But when the other thread running it throws exception: Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCal. If i don't close the listener and client it works fine. So how can i close the TcpListener and TcpClient in the thread without causing problem to the second thread . TcpListener and TcpClient are not static. Code: private void OpenListenerTask(int port,AutoResetEvent resetEvent) { try { NetworkStream getStream = StartListenerGetStream(port, resetEvent); string localIp = Dns.GetHostAddresses(Dns.GetHostName())[0].ToString(); var networkReader = new Reader(getStream, localIp, _sendPort.Value, _query); networkReader.Start(); } catch (Exception e) { Console.WriteLine(e.GetBaseException().Message); } //close TcpClient & TcpListener after data/error received _client.Close(); _listener.Stop(); } private TcpClient _client; private TcpListener _listener; private NetworkStream StartListenerGetStream(int port,EventWaitHandle resetEvent) { var tcpListener = new TcpListener(port); tcpListener.Start(); _listener = tcpListener; //tcpListener.Server.Blocking = false; resetEvent.Set(); var tcpClient = tcpListener.AcceptTcpClient(); _client = tcpClient; return tcpClient.GetStream(); }

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      michaelgr1 wrote:

      i am trying to close the TcpListener and TcpClient opened in the client side

      You can't close a TcpListener in the client. The client doesn't own nor have access to that.

      M 1 Reply Last reply
      0
      • J jschell

        michaelgr1 wrote:

        i am trying to close the TcpListener and TcpClient opened in the client side

        You can't close a TcpListener in the client. The client doesn't own nor have access to that.

        M Offline
        M Offline
        michaelgr1
        wrote on last edited by
        #3

        yes, but in my solution, probably i didn't explain it. The server sending the data and the client receives it. So the listener is opened in the client side

        A J 2 Replies Last reply
        0
        • M michaelgr1

          yes, but in my solution, probably i didn't explain it. The server sending the data and the client receives it. So the listener is opened in the client side

          A Offline
          A Offline
          Addy Tas
          wrote on last edited by
          #4

          I take it that your listener has nothing to do with the terminology in socket communication? Why not use sender and receiver? That takes away the uncertainty of what issue you are really facing. Cheers, AT

          Cogito ergo sum

          1 Reply Last reply
          0
          • M michaelgr1

            yes, but in my solution, probably i didn't explain it. The server sending the data and the client receives it. So the listener is opened in the client side

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            The "server" is the one that opens the listener. The "client" is the one that connects with a client socket to the server (where the listener is.) I don't care how you label your app(s). And what I said holds. The client can't close the listener. The client can close the client socket. The server can close the client socket (the one it created based on a listener request.)

            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