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 Socket Exception , two ports / one IP address

TCP Socket Exception , two ports / one IP address

Scheduled Pinned Locked Moved C#
sysadminjson
2 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.
  • R Offline
    R Offline
    Rick van Woudenberg
    wrote on last edited by
    #1

    Dear all, I'm trying to bind two ports to one IP address. In my first method I bind a port ( 31010 ) to IpAddress.Any Int32 port = 31010; IPAddress localAddr = IPAddress.Any; //TcpListener server = new TcpListener(port); server = new TcpListener(localAddr, port); server.ExclusiveAddressUse = false; // Start listening for client requests. server.Start(); // Buffer for reading data Byte[] bytes = new Byte[256]; String data = null; // Enter the listening loop. while (true) { // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. client = server.AcceptTcpClient(); client.ExclusiveAddressUse = false; //blah blah the rest ... } This works fine in itself. The application is listening on this port and receives data which is processed happily. However, I add another method to bind another port ( 31009 ) which I call after the first one : private static void SendMessageToMessenger(string data) { //create the socket instance... mm_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // get the remote IP address... IPAddress ip = IPAddress.Any; mm_socClient.ExclusiveAddressUse = false; int iPortNo = 31009; //create the end point IPEndPoint ipEnd = new IPEndPoint(ip.Address, iPortNo); //connect to the remote host... mm_socClient.Connect(ipEnd); Object objData = data; byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString()); mm_socClient.Send(byData); // End string mm_socClient.Disconnect(true); } And then it goes wrong. It throws an Exception saying : SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted I know that the first method is performing a blocking call to receive data, but I'm assuming this blocking call is done for the port and not the IP address but I could be wrong there. Anyhow, it won't allow me to bind a dif

    B 1 Reply Last reply
    0
    • R Rick van Woudenberg

      Dear all, I'm trying to bind two ports to one IP address. In my first method I bind a port ( 31010 ) to IpAddress.Any Int32 port = 31010; IPAddress localAddr = IPAddress.Any; //TcpListener server = new TcpListener(port); server = new TcpListener(localAddr, port); server.ExclusiveAddressUse = false; // Start listening for client requests. server.Start(); // Buffer for reading data Byte[] bytes = new Byte[256]; String data = null; // Enter the listening loop. while (true) { // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. client = server.AcceptTcpClient(); client.ExclusiveAddressUse = false; //blah blah the rest ... } This works fine in itself. The application is listening on this port and receives data which is processed happily. However, I add another method to bind another port ( 31009 ) which I call after the first one : private static void SendMessageToMessenger(string data) { //create the socket instance... mm_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // get the remote IP address... IPAddress ip = IPAddress.Any; mm_socClient.ExclusiveAddressUse = false; int iPortNo = 31009; //create the end point IPEndPoint ipEnd = new IPEndPoint(ip.Address, iPortNo); //connect to the remote host... mm_socClient.Connect(ipEnd); Object objData = data; byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString()); mm_socClient.Send(byData); // End string mm_socClient.Disconnect(true); } And then it goes wrong. It throws an Exception saying : SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted I know that the first method is performing a blocking call to receive data, but I'm assuming this blocking call is done for the port and not the IP address but I could be wrong there. Anyhow, it won't allow me to bind a dif

      B Offline
      B Offline
      Bert delaVega
      wrote on last edited by
      #2

      I would try "netstat -a -o -n | more" before you start your application and check that the 31009 port isn't already open.

      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