how to tell a socket what port to listen to?

fguihen

Junior Contributor
Joined
Nov 10, 2003
Messages
248
Location
Eire
i have to create a socket (in C#) and tell it to listen to the Irda port on my laptop. i hav'nt a clue how to do this , as all examples are specifying IP addresses. anyone help me?
 
well, im not sure about IrDA, but with a regular socket class you can specify the port in the endpoint that the socket listens to, like this:

IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );

listener.Bind(localEndPoint);
listener.Listen(number_of_backlogs);

then you can do a listener.Accept() and listener.Recieve(),

There's a really good tutorial on sockets in the msdn.


Jimmy
 
Back
Top