TCP Socket Exception , two ports / one IP address
-
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 -
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 difI would try "netstat -a -o -n | more" before you start your application and check that the 31009 port isn't already open.