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. Socket Exception

Socket Exception

Scheduled Pinned Locked Moved C#
sysadminhelp
8 Posts 5 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
    Paul Harsent
    wrote on last edited by
    #1

    I am trying to connect to a tcp listener, after I hit the clint.Connect(server, port) it throughts a SocketException

    public void Connect(String server, String message)
    {
    try
    {
    StreamWriter sw;
    StreamReader sr;

                int port = 5000;
                TcpClient client = new TcpClient(server, port);
    
                client.Connect(server, port);
    
    
                sw = new StreamWriter(client.GetStream());
    
                sr = new StreamReader(client.GetStream());
    
    
                sw.WriteLine(message);
                sw.Flush();
                
                string rtnMsg = sr.ReadToEnd().Trim();
    
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            catch (IOException e)
            {
    
            }
            catch (NullReferenceException e)
            {
    
            }
    
            Console.WriteLine("\\n Press Enter to continue...");
            Console.Read();
        }
    

    Any help would be really helpful.

    P L 2 Replies Last reply
    0
    • P Paul Harsent

      I am trying to connect to a tcp listener, after I hit the clint.Connect(server, port) it throughts a SocketException

      public void Connect(String server, String message)
      {
      try
      {
      StreamWriter sw;
      StreamReader sr;

                  int port = 5000;
                  TcpClient client = new TcpClient(server, port);
      
                  client.Connect(server, port);
      
      
                  sw = new StreamWriter(client.GetStream());
      
                  sr = new StreamReader(client.GetStream());
      
      
                  sw.WriteLine(message);
                  sw.Flush();
                  
                  string rtnMsg = sr.ReadToEnd().Trim();
      
              }
              catch (ArgumentNullException e)
              {
                  Console.WriteLine("ArgumentNullException: {0}", e);
              }
              catch (SocketException e)
              {
                  Console.WriteLine("SocketException: {0}", e);
              }
              catch (IOException e)
              {
      
              }
              catch (NullReferenceException e)
              {
      
              }
      
              Console.WriteLine("\\n Press Enter to continue...");
              Console.Read();
          }
      

      Any help would be really helpful.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      The reason this happens is because you've already attempted to establish a connection in your constructor. The exception you're getting will be indicating that the port's already in use.

      I'm not a stalker, I just know things. Oh by the way, you're out of milk.

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Onyx

      P 1 Reply Last reply
      0
      • P Pete OHanlon

        The reason this happens is because you've already attempted to establish a connection in your constructor. The exception you're getting will be indicating that the port's already in use.

        I'm not a stalker, I just know things. Oh by the way, you're out of milk.

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Onyx

        P Offline
        P Offline
        Paul Harsent
        wrote on last edited by
        #3

        It is just a method which is call by a thread, the method is passed the server IP to connect to. It throws an exception even when connecting only to one IP

        P 1 Reply Last reply
        0
        • P Paul Harsent

          It is just a method which is call by a thread, the method is passed the server IP to connect to. It throws an exception even when connecting only to one IP

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Did you read what I said carefully? Basically, when you call new TcpClient(address, port) you are establishing a connection at that point. You can remove the Connect call as you've already established your connection. See this[^] for more details.

          I'm not a stalker, I just know things. Oh by the way, you're out of milk.

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Onyx

          realJSOPR 1 Reply Last reply
          0
          • P Paul Harsent

            I am trying to connect to a tcp listener, after I hit the clint.Connect(server, port) it throughts a SocketException

            public void Connect(String server, String message)
            {
            try
            {
            StreamWriter sw;
            StreamReader sr;

                        int port = 5000;
                        TcpClient client = new TcpClient(server, port);
            
                        client.Connect(server, port);
            
            
                        sw = new StreamWriter(client.GetStream());
            
                        sr = new StreamReader(client.GetStream());
            
            
                        sw.WriteLine(message);
                        sw.Flush();
                        
                        string rtnMsg = sr.ReadToEnd().Trim();
            
                    }
                    catch (ArgumentNullException e)
                    {
                        Console.WriteLine("ArgumentNullException: {0}", e);
                    }
                    catch (SocketException e)
                    {
                        Console.WriteLine("SocketException: {0}", e);
                    }
                    catch (IOException e)
                    {
            
                    }
                    catch (NullReferenceException e)
                    {
            
                    }
            
                    Console.WriteLine("\\n Press Enter to continue...");
                    Console.Read();
                }
            

            Any help would be really helpful.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Paul Harsent wrote:

            TcpClient client = new TcpClient(server, port);
            client.Connect(server, port);

            I'm with Pete here; you basically have provided the same information (server, port) twice; that would not make much sense, would it? The documentation[^] provides an example, it does not have a Connect() call. :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            1 Reply Last reply
            0
            • P Pete OHanlon

              Did you read what I said carefully? Basically, when you call new TcpClient(address, port) you are establishing a connection at that point. You can remove the Connect call as you've already established your connection. See this[^] for more details.

              I'm not a stalker, I just know things. Oh by the way, you're out of milk.

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Onyx

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              Just let it go Pete... :)

              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
              -----
              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
              -----
              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

              P 1 Reply Last reply
              0
              • realJSOPR realJSOP

                Just let it go Pete... :)

                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                -----
                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                I can't. I've got CDO.

                I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Onyx

                A 1 Reply Last reply
                0
                • P Pete OHanlon

                  I can't. I've got CDO.

                  I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Onyx

                  A Offline
                  A Offline
                  ankitjoshi24
                  wrote on last edited by
                  #8

                  I think you link should help him out. I went thru it and msdn is always helpful

                  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