I need to generate port numbers for some programm. Google tells, that bind with zero-port generates binds unused port. I want to bind zero-port, remember it's number, close port. Trying to do this? but netstat doesn't see my programm's port. Linux 2.6.27
int sfd;
struct sockaddr_in my_addr;
sfd = socket(PF_INET , SOCK_STREAM, 0);
if (sfd == -1)
{
printf("socket error\n");
return -1;
}
memset(&my_addr, 0, sizeof(struct sockaddr_in ));
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = INADDR_ANY;
my_addr.sin_port = 0;
if (bind(sfd, (struct sockaddr_in *) &my_addr,
sizeof(struct sockaddr_in)) == -1)
printf("bind error\n");
if (listen(sfd, LISTEN_BACKLOG) == -1)
printf("listen error\n");
bind() expects a sockaddr* not a sockaddr_in*:
bind(sfd, (struct sockaddr *) &my_addr, sizeof(my_addr))
Other than that, I don't see any other errors. Binding to port 0 is the correct way to bind to a random available port. If bind() and listen() do not report a failure then netstat should see an open port. Use getsockname() to find out which port bind() actually chose, eg:
int sfd = socket(PF_INET , SOCK_STREAM, 0);
if (sfd == -1)
{
printf("socket error: %d\n", errno);
return -1;
}
struct sockaddr_in my_addr;
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = INADDR_ANY;
my_addr.sin_port = 0;
if (bind(sfd, (struct sockaddr *) &my_addr, sizeof(my_addr)) == -1)
{
printf("bind error: %d\n", errno);
close(sfd);
return -1;
}
if (listen(sfd, LISTEN_BACKLOG) == -1)
{
printf("listen error: %d\n", errno);
close(sfd);
return -1;
}
memset(&my_addr, 0, sizeof(my_addr));
socklen_t my_addrlen = sizeof(my_addr);
if (getsockname(sfd, (struct sockaddr *) &my_addr, &my_addrlen ) == -1)
{
printf("getsockname error: %d\n", errno);
close(sfd);
return -1;
}
printf("listening on port: %hu\n", ntohs(my_addr.sin_port));
Related
I am currently experiencing some trouble while testing UDP sockets.
So I have 2 c programs, one sends and receives, the other receives and sends a message.
Here is send_recv.c:
#define PORT 1010
int main(int argc, const char *argv[]) {
int socketInfo, opt;
struct sockaddr_in server;
char msgOut[30], msgIn[30];
opt = 1;
printf("Input message: ");
fgets(msgOut, 30, stdin);
socketInfo = socket(AF_INET, SOCK_DGRAM, 0);
if(socketInfo < 0) {
perror("socket error");
return EXIT_FAILURE;
}
if(setsockopt(socketInfo, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt failed");
return EXIT_FAILURE;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);
if(bind(socketInfo, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("bind error");
return EXIT_FAILURE;
}
if(connect(socketInfo, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("connection error");
return EXIT_FAILURE;
}
puts("connected");
send(socketInfo, msgOut, strlen(msgOut), 0);
puts("sleeping for 5");
sleep(5);
puts("message sent");
recv(socketInfo, msgIn, sizeof(msgIn), 0);
printf("received msg: %s\n", msgIn);
close(socketInfo);
return EXIT_SUCCESS;
}
And here is recv_send.c:
#define PORT 1010
int main(int argc, const char *argv[]) {
int socketInfo, opt;
struct sockaddr_in server;
char msgOut[30], msgIn[30];
opt = 1;
socketInfo = socket(AF_INET, SOCK_DGRAM, 0);
if(socketInfo < 0) {
perror("socket error");
return EXIT_FAILURE;
}
if(setsockopt(socketInfo, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt failed");
return EXIT_FAILURE;
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;
if(bind(socketInfo, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("bind error");
return EXIT_FAILURE;
}
if(connect(socketInfo, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("connection error");
return EXIT_FAILURE;
}
puts("connected");
recv(socketInfo, msgIn, sizeof(msgIn), 0);
printf("received msg: %s\n", msgIn);
printf("Input message: ");
fgets(msgOut, 30, stdin);
send(socketInfo, msgOut, strlen(msgOut), 0);
puts("message sent");
close(socketInfo);
return EXIT_SUCCESS;
}
The problem is that the receive program never receives anything, and the send program sends and then just receives the message it already received.
The output of send_rcv is this:
Input message: hello there
connected
message sent
received msg: hello there
and the output of rcv_send is this:
connected
(ctrl+z) Job 6, 'sudo ./rcv_send' has stopped
Another problem is that when I don't run the program with sudo it just says this: bind error: Permission denied
The problem is that both endpoints are listening on the same port while running on the same machine, and you have both sides sending to INADDR_ANY.
Also, port numbers below 1024 are privileged ports and therefore require root access to bind to.
Use a different port (1024 or higher) for each side of the connection, and choose a specific IP address to connect to. For example:
#define LOCAL_PORT 1100
#define REMOTE_PORT 1101
...
struct sockaddr_in local, remote;
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port = htons(LOCAL_PORT);
remote.sin_family = AF_INET;
remote.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
remote.sin_port = htons(LOCAL_PORT);
if(bind(socketInfo, (struct sockaddr *)&local, sizeof(local)) < 0) {
perror("bind error");
return EXIT_FAILURE;
}
if(connect(socketInfo, (struct sockaddr *)&remote, sizeof(remote)) < 0) {
perror("connection error");
return EXIT_FAILURE;
}
The values of LOCAL_PORT and REMOTE_PORT would be swapped in the two programs.
Note also that you can get rid of the setsockopt call as you don't have two programs open on the same port.
#define PORT 3000
int sock()
{
int valread;
struct sockaddr_in serv_addr;
const char* hello = "Hello from client";
char buffer[1024] = { 0 };
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
send(sock, hello, strlen(hello), 0);
printf("Hello message sent\n");
return 0;
}
I have this code for the sockets, to be sent at localhost:3000 address. But they are not sent, and Connection failed error pops up. I have node.js server listener socket open at that port.
On Windows only 1, you must call WSAStartup() to initialize the Winsock library before using any other socket functions, like connect(). See Initializing Winsock. And you should call WSACleanup() when you are done using Winsock.
1: On non-Windows platforms, there are no equivalent startup/cleanup functions that need to be called when using sockets.
Try something more like this:
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#define PORT 3000
int main()
{
WSADATA wsa;
int iResult = WSAStartup(MAKEWORD(2,0), &wsa);
if (iResult != 0)
{
printf("WSAStartup failed: %d\n", iResult);
return INVALID_SOCKET;
}
printf("Winsock initialized\n");
struct sockaddr_in serv_addr;
ZeroMemory(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0)
{
printf("Invalid address/ Address not supported \n");
WSACleanup();
return -1;
}
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET)
{
printf("Socket creation failed: %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == SOCKET_ERROR)
{
printf("Connection failed: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
printf("Connection successful\n");
const char* hello = "Hello from client";
if (send(sock, hello, strlen(hello), 0) == SOCKET_ERROR)
{
printf("Send failed: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
printf("Hello message sent\n");
closesocket(sock);
WSACleanup();
return 0;
}
That said, you really should be using getaddrinfo() instead of inet_pton(), especially if you want to support both IPv4 and IPv6, eg:
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <stdio.h>
#define PORT "3000"
int main()
{
WSADATA wsa;
int iResult = WSAStartup(MAKEWORD(2,0), &wsa);
if (iResult != 0)
{
printf("WSAStartup failed: %d\n", iResult);
return INVALID_SOCKET;
}
printf("Winsock initialized\n");
struct addrinfo hints, *addrs;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Convert IPv4 and IPv6 addresses from text to binary form
iResult = getaddrinfo("127.0.0.1", PORT, &hints, &addrs);
if (iResult != 0)
{
printf("Get Address Info failed: (%d) %s\n", iResult, gai_strerror(iResult));
WSACleanup();
return -1;
}
SOCKET sock = INVALID_SOCKET;
for(struct addrinfo *addr = addrs; addr != NULL; addr = addr->ai_next)
{
sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (sock != INVALID_SOCKET)
{
if (connect(sock, addr->ai_addr, addr->ai_addrlen) != SOCKET_ERROR)
break;
closesocket(sock);
sock = INVALID_SOCKET;
}
}
if (sock == INVALID_SOCKET)
{
printf("Socket creation failed/Connection failed\n");
WSACleanup();
return -1;
}
printf("Connection successful\n");
const char* hello = "Hello from client";
if (send(sock, hello, strlen(hello), 0) == SOCKET_ERROR)
{
printf("Send failed: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
printf("Hello message sent\n");
closesocket(sock);
WSACleanup();
return 0;
}
I have the following 2 functions
int listen_socket(unsigned int ip, int port, char *inf)
{
struct ifreq interface;
int fd;
struct sockaddr_in addr;
int n = 1;
DEBUG(LOG_INFO, "Opening listen socket on 0x%08x:%d %s\n", ip, port, inf);
if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
DEBUG(LOG_ERR, "socket call failed: %s", strerror(errno));
return -1;
}
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = ip;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) {
close(fd);
return -1;
}
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) == -1) {
close(fd);
return -1;
}
strncpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ);
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) {
close(fd);
return -1;
}
if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) {
close(fd);
return -1;
}
return fd;
}
int raw_socket(int ifindex)
{
int fd;
struct sockaddr_ll sock;
DEBUG(LOG_INFO, "Opening raw socket on ifindex %d\n", ifindex);
if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
DEBUG(LOG_ERR, "socket call failed: %s", strerror(errno));
return -1;
}
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
sock.sll_family = AF_PACKET;
sock.sll_protocol = htons(ETH_P_IP);
sock.sll_ifindex = ifindex;
if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
DEBUG(LOG_ERR, "bind call failed: %s", strerror(errno));
close(fd);
return -1;
}
return fd;
}
Both are socket listener functions.
I used these functions in my application in this way
fd = listen_socket(INADDR_ANY, 67, client_config.interface);
fd2 = raw_socket(client_config.ifindex);
Now if I send packet to my application (with destination = ip of the interface and port=67). What socket should catch my packet? is it fd2 or fd or both?
And if I send a packet to my application (with destination = broacast:255.255.255.0 and port=67). What socket should catch my packet? is it fd2 or fd or both?
Both sockets will receive that packet. As each packet arrives from the network driver to the kernel, it is duplicated and sent to all PF_PACKET (layer 2) sockets. The packet is also sent to the layer 3 (IP/TCP) kernel code and from there, to the addressed socket.
If this didn't happen, running a separate program doing raw packet captures (e. g. wireshark) would prevent any other communications over the network.
I want to receive UDP packets from a server application (on the same computer) and forward it to an UDP receive app on a different port.
The server app is not mine, nor do i have the source code.
The UDP receive app is a Java application.
If i bind the Java application directly to the server app port, there is very low latency, but if i connect it to the relay appĀ“port i get almost one second of delay.
The receiving port has to be non-blocking.
#define rcv_length 160
fd_set fds;
int n;
struct timeval tv;
void CMClient::startUDPListener(){
CMport_number = 32200;
remoteAddrLen = sizeof(struct sockaddr_in);
if (WSAStartup(0x0101, &CMw) != 0)
{
fprintf(stderr, "Could not open Windows connection.\n");
exit(0);
}
CMsd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (CMsd == INVALID_SOCKET)
{
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
exit(0);
}
CMserver.sin_family = AF_INET;
CMserver.sin_port = htons(CMport_number);
CMserver.sin_addr.s_addr = inet_addr("127.0.0.1");
long rc=bind(CMsd,(SOCKADDR*)&CMserver,sizeof(SOCKADDR_IN));
if(rc==SOCKET_ERROR)
{
printf("Error: bind code: %d\n",WSAGetLastError());
}
}
void CMClient::updateData(UDPServer* svr, int CMnr){
FD_ZERO(&fds);
FD_SET(CMsd, &fds);
tv.tv_sec = 0;
tv.tv_usec = 1;
n = select ( CMsd, &fds, NULL, NULL, &tv ) ;
if (FD_ISSET(CMsd, &fds))
{
FD_CLR(CMsd,&fds);
char* rcvBuffer = new char[rcv_length];
long rc=recv(CMsd,rcvBuffer,rcv_length,0); //receive
if(rc!=SOCKET_ERROR)
{
rcvBuffer[0] = CMnr;
sendto(svr->sd, (char*)rcvBuffer, rcv_length, 0, (struct sockaddr*)&svr->server, sizeof(svr->server)) != (int)sizeof(rcvBuffer); //send
}
if(rcvBuffer)
delete [] rcvBuffer;
}
}
The UDP server to send to the Java application is initialized as follows:
void UDPServer::startUDPServer(){
if (WSAStartup(0x0101, &w) != 0)
{
fprintf(stderr, "Could not open Windows connection.\n");
exit(0);
}
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == INVALID_SOCKET)
{
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
exit(0);
}
server.sin_family = AF_INET;
server.sin_port = htons(port_number);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(sd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Could not connect name to socket.\n");
stopUDPServer();
}
}
The main calls
UDPServer* udpsvr;
udpsvr = new UDPServer;
udpsvr->startUDPServer();
while(1){
CM->updateData(udpsvr, CMid);
}
Any help would be appreciated why i get that much delay.
The data received from the Java app is correct but ~1 second delay.
Thank you,
Regards
Don't use bind() on client. Use connect().
Summing up, for UDP:
Server:
socket()
bind()
Client:
socket()
connect()
Example of code for server:
// socket()
fd_ = socket(AF_INET, SOCK_DGRAM, 0);
if (fd_ < 0) {
throw std::runtime_error ("Client socket error");
}
// connect()
struct sockaddr_in serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
struct in_addr addr;
inet_aton(address.c_str(), &addr);
bcopy(&addr, &serv_addr.sin_addr.s_addr, sizeof(addr));
if (connect(fd_, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0) {
::close(fd_);
throw std::runtime_error ("Client socket error");
}
Example of code for client:
// socket()
fd_ = socket(AF_INET, SOCK_DGRAM, 0);
if (fd_ < 0) {
throw std::runtime_error ("Client socket error");
}
// connect()
struct sockaddr_in serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
struct in_addr addr;
inet_aton(address.c_str(), &addr);
bcopy(&addr, &serv_addr.sin_addr.s_addr, sizeof(addr));
if (connect(fd_, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0) {
::close(fd_);
throw std::runtime_error ("Client socket error");
}
Finally, you may want to have a look at flushing UDP buffers.
I use following code to check if a port is available or not:
bool ClassA::CheckPortTCP(short int dwPort , char *ipAddressStr)
{
struct sockaddr_in client;
int sock;
client.sin_family = AF_INET;
client.sin_port = htons(dwPort);
client.sin_addr.S_un.S_addr = inet_addr(ipAddressStr);
sock = (int) socket(AF_INET, SOCK_STREAM, 0);
int result = connect(sock, (struct sockaddr *) &client,sizeof(client));
// change to result == 0 -> failure in writing code too quick ;-)
if (result = 0) return true; // port is active and used
else return false;
}
The problem is if the port is opened but not connected the check failed! How can I easily examine that the port is available (not listening, not connected)?
e.g. port 21111 (output of netstat) -> my function doesn't recognize that the port is not free
TCP 0.0.0.0:21111 xxxxDUMMYxxxx:0 LISTENING
Thx
You have two errors: The first is that in the if statement you assign zero to result. The other is that connect returns -1 on failure to connect, and a non-negative value if it manages to connect.
There is also a problem that if you manage to connect, you don't close that connection.
the line client.sin_addr.S_un.S_addr = inet_addr(ipAddressStr); can not work for me. change it to:
bool CheckPortTCP(short int dwPort, char *ipAddressStr)
{
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup function failed with error: %d\n", iResult);
return false;
}
struct sockaddr_in client;
int sock;
client.sin_family = AF_INET;
client.sin_port = htons(dwPort);
client.sin_addr.s_addr = inet_addr(ipAddressStr);
sock = (int)socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
wprintf(L"ERROR: Socket function failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return false;
}
printf("INFO: Checking Port : %s:%d\n", ipAddressStr, dwPort);
int result = connect(sock, (struct sockaddr *) &client, sizeof(client));
if (result == SOCKET_ERROR) {
printf("ERROR: %s", WSAGetLastError());
WSACleanup();
return false;
}
else
{
WSACleanup();
closesocket(sock);
return true;
}
}