Hi, my question is very simple.
Is it possible to create more of an TcpListener in the same service
I did a service and I need that it's listening to two different port.
I think that i only can to create one tcplistener in my service. Is it true
What can i do to solve this
Thanks a lot.
Best regards.

tcplistener and services
Siliconxp
Here is a quick sample (I would never use this in production code as there are better ways of doing it .. ) but it is the easiest way to listen on two sockets .. It is the above example modified to use two TCPListeners without threading.
using System;
using
System.Net;using
System.Net.Sockets;using
System.Text;using
System.Threading;using
System.Globalization;class
Server { public static void Main() {DateTime now;
String strDateLine;
Encoding ASCII = Encoding.ASCII;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
try {TcpListener tcp1 =
new TcpListener(13); // listen on port 13TcpListener tcp2 =
new TcpListener(1313); // listen on port 1313tcp1.Start();
tcp2.Start();
Console.WriteLine("Waiting for clients to connect");
Console.WriteLine("Press Ctrl+c to Quit...");
Socket s =
null; while (true) { if(tcp1.Pending()) {s = tcp1.AcceptSocket();
}
else if(tcp2.Pending()) {s = tcp2.AcceptSocket();
}
if(s!=null) { // Get the current date and time then concatenate it // into a stringnow = DateTime.Now;
strDateLine = now.ToShortDateString() + " " + now.ToLongTimeString();
// Convert the string to a Byte Array and send itByte[] byteDateLine = ASCII.GetBytes(strDateLine.ToCharArray());
s.Send(byteDateLine, byteDateLine.Length, 0);
s.Close();
s=
null;Console.WriteLine("Sent {0}", strDateLine);
}
Thread.Sleep(100);
}
}
catch (SocketException socketError) { if (socketError.ErrorCode == 10048) {Console.WriteLine("Connection to this port failed. There is another server is listening on this port.");
}
}
}
}
ski99
No you should be able to create two TCPListeners one for each port. The only "gotcha" would be if you follow examples such as http://samples.gotdotnet.com/quickstart/util/srcview.aspx path=/quickstart/howto/samples/net/TCPUDP/dateTimeServer.src&file=CS\dateTimeServer.cs&font=3 you would need to run 2 threads (one for each listener).
Cheers,
Greg