Assign specific port to RPC server\client - windows

I am writting a RPC client server application on windows. I have gone through RPC sample programs MS has given. But none of them mention port specifically. This probably because RPC uses dynamic port above 1024. But what if I wanted to assign specific port/port range to specific service (my server and client app for example). How can I do that? I can use RPCCFG to assign range but that range will be for all RPC programs (http://support.microsoft.com/kb/908472) right? How can I control a single program? I know it's possible because exchange seem to able to do it for Client Access Service?
Thanks in advance,
-Neel.

You can define ports in the code or use a config file which you read in the code.
status = RpcServerUseProtseqEp(
(char *)"ncacn_ip_tcp", // Use TCP/IP
RPC_C_PROTSEQ_MAX_REQS_DEFAULT, // Backlog queue length for TCP/IP.
(char *)"4747", // TCP/IP port to use.
NULL); // No security.
Success. Jasper

Related

Adafruit Huzzah32 ESP32 WebSocket and WebServer on the same port

I have a Adafruit Huzzah32. I am using wifi as the transport.
I am trying to get the WebServer and WebSocket to work together. Is there any example of this?
I have tried a couple different libraries, but none of them seem to do both server pages and WS socket handling on the same port.
I can set them up separately, but then I have to use 2 different ports and I would like them to be on the same port.
What you're asking for doesn't make any sense.
Port numbers uniquely identify the service using the TCP connection. They determine how the connection's data is handled. You can't have two different things using one port because the port number is what's used to distinguish between them.
What is it that you're actually trying to accomplish by trying to use the same port number?

Generate port number at runtime in Linux [duplicate]

I need to create a program that will communicate with other programs on the same computer via UDP sockets. It will read commands from stdin, and some of this commands will make it to send/receive packets without halting execution. I've read some information out there, but since I'm not familiar with socket programming and need to get this done quickly, I have the following questions:
I need to get a random unused port for the program to listen to, and reserve it so other programs can communicate with this and also the port isn't reserved by another program. I also need to store the port number on a variable for future usage.
Since the communication is across processes on the same machine, I'm wondering if I can use PF_LOCAL.
Also a code sample of the setup of such socket would be welcome, as well as an example of sending/receiving character strings.
Call bind() specifying port 0. That will allow the OS to pick an unused port. You can then use getsockname() to retreive the chosen port.
Answer by Remy Lebeau is good if you need a temporary port. It is not so good if you need a persistent reserved port because other software also uses the same method to get a port (including OS TCP stack that needs a new temporary port for each connection).
So the following might happen:
You call bind with 0 and getsockname() to get a port;
then save it into config (or into several configs) for future uses;
software that needs this port starts and binds the port.
Then you need to e.g. restart the software:
software stops and unbinds the port: the port can now be returned by bind(0) and getsockname() again;
e.g. TCP stack needs a port and binds your port;
software can't start because port is already bound.
So for "future uses" you need a port that is not in ephemeral port range (that's the range from which bind(host, 0) returns a port).
My solution for this issue is the port-for command-line utility.
If it being a random port is actually important, you should do something like:
srand(time(NULL));
rand() % NUM_PORTS; // NUM_PORTS isn't a system #define
Then specify that port in bind. If it fails, pick a new one (no need to re-seed the random generator. If the random port isn't important, look at Remy Lebeau's answer.

Is there a Windows socket API call / option to "block" a range of ports à la SO_EXCLUSIVEADDRUSE

In this (rather old) article, the author states:
"Port Blocking
Port blocking allows an application to prevent other applications from performing specific binds to the ports within a specified range. When blocking a port range, the application must choose a contiguous range of port numbers that are between the value of the MaxUserPort setting (5000 by default) + 1 and either 49151 (for Windows XP and Windows Server 2003 with no service packs installed) or 65535 (for Windows Server 2003 Service Pack 1). There should be no existing bindings to the ports within the range of blocked ports. Windows Sockets returns the last port number in the blocked range as the handle. When unblocking (removing the block), Windows Sockets unblocks the range that has the same left edge as the unblock request."
The implication I get from reading this is that there is some api an application can use to set/unset this, but I have drawn complete blanks trying to search documentation for any such thing, nor do I recall ever seeing such functionality in my socket programming days.
Is there such a capability in Windows, and if so, pointers to documentation of it will be heartily appreciated!
There is no such call in the winsock API itself, but I believe there is in the LSP feature. Another (easy but dirty <3) way is to just bind() to all the ports you want blocked.
You can set the range of ports which will be used for dynamic clients (binding to any available socket) on the system by the MaxUserPort registry value (applicable from Vista onwards). The dynamic port range can be set for tcp and udp sockets. More details in the following links :
MaxUserPort
KB929851
So you can set the range (to allow), as per your needs

changing the protocol for client to server

I have to test the value from client to server using different protocols (Tcp , UDP , Sctp).
Please guide how it can be possible. Is there any way in windows to change the protocol or is there anyway to find it by using software like packet tracer .
Thanks
While the question is not entirely clear, it sounds as though your interested in seeing the information sent between the client and the server when each of those protocols is used.
Windows does not provide a built in utility to view packet data but it can be viewed using a packet analyser such as Wireshark.
In order to see the values sent by each protocol you must run a client for each of the protocols and use it to connect to the server for that protocol. If you don't have a server to connect to, you may need to run one on your local machine.
You can narrow down the data captured to just the protocol you're interested in using a filter in Wireshark If you don't know the protocol being used, you can filter by the port number used for that connection which can be established using the netstat command.
You may need to use netstat -b to show you which programs are using which ports.
If you just have to generate packets using different protocols then the tool like netcat can also help. It supports TCP and UDP and has been ported to windows .

Receiving datagrams using Udp connection

In order to receive datagrams through an UDP connection I have created an object of type UDPClient.
receivedNotificationSock = new UdpClient();
However once done and on using the receive method:
receivedHostNameBuffer=receivedNotificationSock.Receive(ref receivedNotificationIP);
I am getting an exception saying that I must call the bind method.
But there is no bind method in the UDPClient class.
Could You guys please provide me with the code if possible as to what should be done to overcome this exception.
You need I think to know some more about sockets.
All sockets possess a port number. First, you create a socket - which is almost useless on its own. It just floats there. But then you bind it - you assign it a port number. Now it's useful - now you can send and receive data on it.
Remember, all UDP communications are defined by the quad data set of the IP and port of the source and the IP and port of the destination. A freshly created socket doesn't have an IP address or port; binding gives it an IP address and port.
Unfortunately, I'm not a C# programmer, so I can't properly answer your question. But at least you know why it's important.
Pass the port number into the constructor of your UDP client.
receivedNotificationSock = new UdpClient(21000);
You may need to change firewall settings to allow the bind, though a popup window normally opens when you first run this on your dev machine.
For Socket proramming you need to know the sequence of syscalls you need to do on client side and on the server side.
If you are writting a client :
you open a socket with a socket call.
you then connect to the server port with a connect call
once connect is successful
then you send the request to the server using either a send or sendto or a write
which results in reception of data that you can read using a receive or read
On Server Side
you create a socket
bind it to a port
start listening on the socket for incoming connections from various clients using a listen.
There is a non blocking way of listening for connections as well with a select syscall.
Once the you establish a connection you can essentially read the request and start processing it.
Here's an example in C# that may be useful to you.
http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/

Resources