B
<p>While I'm trying to implement a modbus slave on a linux system, I've problems using port number 502. On modbus messaging implementation guide I found the following text line:
<p>"All MODBUS/TCP ADU are sent via TCP on registered port 502."
<p>I wanted to do so, but my linux system doesn't want to:
<pre>
int sockfd, i;
struct sockaddr_in address;
size_t addrlength = sizeof(struct sockaddr_in);
if ( (sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
printf("error on socket\n");
i = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
address.sin_family = AF_INET;
address.sin_port = htons(PORT_NUMBER);
memset(&address.sin_addr, 0, sizeof(address.sin_addr));
if (bind(sockfd, (struct sockaddr *) &address, sizeof(address)))
printf("error on bind\n");
if (listen(sockfd, 5))
printf("error on listen\n");
</pre>
<p>Starting the routine, I get this message:
"error on bind". (with #define PORT_NUMBER 502)
<p>I only have to change PORT_NUMBER to:
#define PORT_NUMBER 2233 (for example)
<p>And - IT WORKS!
<p>What is wrong here? I want to use Port 502...
<p>Thanks for helping!
<p>"All MODBUS/TCP ADU are sent via TCP on registered port 502."
<p>I wanted to do so, but my linux system doesn't want to:
<pre>
int sockfd, i;
struct sockaddr_in address;
size_t addrlength = sizeof(struct sockaddr_in);
if ( (sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
printf("error on socket\n");
i = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
address.sin_family = AF_INET;
address.sin_port = htons(PORT_NUMBER);
memset(&address.sin_addr, 0, sizeof(address.sin_addr));
if (bind(sockfd, (struct sockaddr *) &address, sizeof(address)))
printf("error on bind\n");
if (listen(sockfd, 5))
printf("error on listen\n");
</pre>
<p>Starting the routine, I get this message:
"error on bind". (with #define PORT_NUMBER 502)
<p>I only have to change PORT_NUMBER to:
#define PORT_NUMBER 2233 (for example)
<p>And - IT WORKS!
<p>What is wrong here? I want to use Port 502...
<p>Thanks for helping!