bind Invalid argument - c

I'm playing with the unix sockets. The code compiles fine but I get the following message on execute
Invalid argument
Here is the code i use. It's pretty simple I think
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#define PORT 7000
int main(){
int socket_desc;
struct sockaddr_in address;
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if(socket_desc == -1)
perror("Create socket");
/* type of socket created in socket() */
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
/* set port */
address.sin_port = htons(PORT);
while(1) {
/* bind the socket to the port specified above */
if(bind(socket_desc,(struct sockaddr *)&address, sizeof(address)) < 0) {
perror("Error");
exit(-1);
}
}
return 0;
}

The problem is that you're trying to bind more than once -- what's with the while(1) loop?
while(1) {
/* bind the socket to the port specified above */
if(bind(socket_desc,(struct sockaddr *)&address, sizeof(address)) < 0) {
perror("Error");
exit(-1);
}
}
bind() succeeds the first time, and on the subsequent call it fails with EINVAL, which means (from man 2 bind):
[EINVAL] socket is already bound to an address and the protocol does not support binding to a new address. Alternatively, socket may have been
shut down.
As a side note, it's probably a good idea to zero out the sockaddr prior to passing it in:
#include <string.h>
/* ... */
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);

Related

socket binding failed (address already in use, even with SO_REUSEADDR)

I am working in a simple socket project. I would like to know:
why error messages appear before telnet localhost 5678?
why SO_REUSEADDR (between socket() and bind()) don't work, and what else I should try?
Code Output Message:
bind error
Error opening file: Address already in use
telnet localhost 5678
[+]Server Socket is created.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#define BUFSIZE 1024 // Buffer Size
#define PORT 5678
int main() {
printf("telnet localhost 5678\n");
int rfd; // socket descriptor
int clientfd; // client descriptor
struct sockaddr_in client; // Client Socket address
socklen_t client_len; // Length of Client Data
char input[BUFSIZE]; // Client Data -> Server
int bytes_read; // Client Bytes
// 1. socket() = create a socket, SOCK_STREAM = TCP
rfd = socket(AF_INET, SOCK_STREAM, 0);
if (rfd < 0) {
fprintf(stderr, "socket error\n");
exit(-1);
}
printf("[+]Server Socket is created.\n");
// optional
int enable = 1;
if (setsockopt(rfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
fprintf(stderr, "setsockopt(SO_REUSEADDR) failed");
//Initialize the server address by the port and IP
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET; // Internet address family: v4 address
server.sin_addr.s_addr = INADDR_ANY; // Server IP address
server.sin_port = htons(PORT); // Server port
// 2. bind() = bind the socket to an address
int brt = bind(rfd, (struct sockaddr *) &server, sizeof(server));
if (brt < 0) {
int errnum;
errnum = errno;
fprintf(stderr, "bind error\n");
fprintf(stderr, "Error opening file: %s\n", strerror(errnum));
exit(-1);
}
printf("[+]Bind to port %d\n", PORT);
// 3. listen() = listen for connections
int lrt = listen(rfd, 50);
if (lrt < 0) {
printf("listen error\n");
exit(-1);
}
if (lrt == 0) {
printf("[+]Listening....\n");
}
// non-stop loop
while (1) {
// 4. accept() = accept a new connection on socket from client
clientfd = accept(rfd, (struct sockaddr *) &client, &client_len);
if (clientfd < 0) {
fprintf(stderr, "accept failed with error %d\n");
exit(-1);
}
printf("Client connected\n");
...
close(clientfd);
printf("Client disconnected\n");
}
close(rfd);
}
I'm assuming that you are using Linux. If you want to rebind to an address, you should use SO_REUSEPORT not SO_REUSEADDR. Name is really misleading. But make sure that you know how it works and whether you really want to use it or not.
You can check difference here: How do SO_REUSEADDR and SO_REUSEPORT differ?

Only one of two UDP listeners receives message

I'm trying to extend the example from here, to having to services receiving the same message from the same UDP port.
From this question, I understand that I should use SO_REUSEADDR to avoid the error of "address already in use". I have one client sending a "hello" message om port 8080 and two identical services, which simply prints out the received message from the port. SO_REUSEADDR solved the problem of using the same address, however only one of the services receives and prints out the message, while the other keep waiting.
Would it not be possible to have the same message received by both services?
The client:
// Client side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8080
#define MAXLINE 1024
// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from client";
struct sockaddr_in servaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = INADDR_ANY;
int n, len;
sendto(sockfd, (const char *)hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &servaddr,
sizeof(servaddr));
printf("Hello message sent.\n");
close(sockfd);
return 0;
}
One of the two identical services:
// Server side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8080
#define MAXLINE 1024
// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from server";
struct sockaddr_in servaddr, cliaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
int reuse = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0)
perror("setsockopt(SO_REUSEADDR) failed");
#ifdef SO_REUSEPORT
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0)
perror("setsockopt(SO_REUSEPORT) failed");
#endif
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr,
sizeof(servaddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}
int len, n;
len = sizeof(cliaddr); //len is value/resuslt
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, ( struct sockaddr *) &cliaddr,
&len);
buffer[n] = '\0';
printf("Client : %s\n", buffer);
return 0;
}
I think that Sockets act like a queue to your application. Each message received from the network gets placed in that queue, and applications calling recv() or recvfrom() pop messages off of that queue.
The two clients using the same UDP port will share the same queue. I think calling recvfrom() on one client will pop a message off of the queue for that client, and make that message unavailable for the other client.
I think that generally the best approach is to have a one-to-one relationship between clients and sockets.
But, you're curious and really want the message to be available for both clients, you could experiment with passing the MSG_PEEK flag to recvfrom(). That flag changes recvfrom() to not consume the next message from the socket queue, so the other client could also receive it.

I need help understanding the bind function in C socket programming

I'm trying to really understand sockets, so I am learning it in C. This is the server code.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
char server_message[255] = "You have reached the server!";
//create server socket
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
//define server addr
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
//Bind socket to IP and Port
bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address));
listen(server_socket, 5);
int client_socket;
client_socket = accept(server_socket, NULL, NULL);
//send message
send(client_socket, server_message, sizeof(server_message), 0);
//close socket
close(server_socket);
return 0;
}
In the bind function, this part is needed to specify the address
(struct sockaddr*) &server_address
I read the man page, and it does say it has to be this way, the code also runs, but I don't understand this syntax. What is happening in this part of the code. I know * is used to declare pointers, and & refers to the memory address of the variable. I just can't put it all together.

C UDP client and server on remote hosts

I wrote a simple C UDP client and server, where the client sends a character to the server. Everything works fine on the same pc. So I wanted to send a character over to a remote pc. Unfortunately, it's not working: the server is not receiving anything but using wireshark I can see that the packet is going out to the correct destination.
Here is the code:
(servip contains the server's address)
//client
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERV_PORT 50000
int main(){
int id, s;
char buf;
char servip[] = "...";
id = socket(AF_INET,SOCK_DGRAM,0);
buf = 'a';
struct sockaddr_in servaddr;
memset((void *)&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
inet_pton(AF_INET,servip, &servaddr.sin_addr); //controlla se minore di 0
s = sendto(id,&buf,sizeof(char),0,(struct sockaddr *) &servaddr,sizeof(servaddr));
if (s == 0)
fprintf(stderr,"errore in sendto");
return EXIT_SUCCESS;
}
//server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERV_PORT 50000
int main(){
int id, s;
unsigned int len;
char buf;
id = socket(AF_INET,SOCK_DGRAM, 0);
struct sockaddr_in addr;
memset((void *)&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* il server accetta pacchetti su
una qualunque delle sue interfacce di rete */
addr.sin_port = htons(SERV_PORT); /* numero di porta del server */
len = sizeof(addr);
bind(id, (struct sockaddr *) &addr, sizeof(addr));
s = recvfrom(id,&buf,sizeof(char),0,(struct sockaddr *) &addr,&len);
printf("%c\n",buf);
if (s==0)
fprintf(stderr,"errore in recvfrom");
return EXIT_SUCCESS;
}
Can anybody help me? What am I doing wrong? Can you please link me an example of a UDP server and client which actually work on remote hosts?
Thanks in advance.
First, you should test every state of "bind","recvfrom" and "sendto", they both return "-1" if an error occured. Both side , client and server.
Also you should check state of variable global variable errno to know the error that occured.
Check your firewall too. And use if you are not using client and server on the same host, try wireshark on the client and on the server.
Add header errno to check errno.
#include <errno.h>
.
.
.
if (sendto(id,&buf,sizeof(char),0,(struct sockaddr *) &servaddr,sizeof(servaddr))== -1) {
int errsv = errno;
printf("sendto() failed\n");
if (errsv == ...) { ... }
}
.
I suspect you will want to bind to a socket to set the source address and port:
struct sockaddr_in myaddr;
memset((char *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(0);
if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
perror("bind failed");
exit (1);
}
If wireshark is showing the packet was transmitted, can you confirm it also shows the packet being received at the server? This should help eliminate firewall and routing problems.

Can a listener receive and send broadcast request?

Any help/feedback would be appreciated. i would like to set a listener (server) such that it would receive data from a broadcaster client and then it would send information over the same socket. Is this possible to do in C Programming if so how?
EDIT: CODE ADDED
/* udpserver.c */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int sock;
int addr_len, bytes_read;
char recv_data[1024];
struct sockaddr_in server_addr , client_addr;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
if (bind(sock,(struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1)
{
perror("Bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
printf("\nUDPServer Waiting for client on port 5000");
fflush(stdout);
while (1)
{
bytes_read = recvfrom(sock,recv_data,1024,0,
(struct sockaddr *)&client_addr, &addr_len);
recv_data[bytes_read] = '\0';
printf("\n(%s , %d) said : ",inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port));
printf("%s", recv_data);
fflush(stdout);
/* HERE IS MY CODE ADDED */
if (sendto(sockfd, "HELLO", 5, 0, (struct sockaddr *)&client_addr, &addr_len) == -1) {
perror("talker: sendto");
exit(1);
}
}
return 0;
}
Sockets are already bidirectional, meaning you can send and receive data with the same connection.
There is a good example of a TCP/UDP server and client in C here
Is the recvFrom working? Do you get the prints which you have after recvFrom()?

Resources