This might look basic to you, but since this is my first experience with networking in C, I wanted to make sure I'm not doing anything stupid, so I figured... better safe than running into some undefined, bizarre behaviour somewhere along the way.
Consider the following code for setting a server socket in C:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
int main(int argc, char* argv[]) {
int listenfd, connfd;
struct sockaddr_in serv_addr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
// setting 'serv_addr'
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr));
if (listen(listenfd, 10) == -1) {
printf("Failed to listen\n");
return -1;
}
while(1) {
connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL);
// do some writing/reading with connfd...
close(connfd);
}
return 0;
}
Notice that the bind() function takes &struct sockaddr_in as argument.
According to the doc:
bind() assigns
the address specified by addr to the socket referred to by the file
descriptor sockfd.
Now suppose that no explicit references are made by me to this serv_addr again, during the execution of the while loop.
Is the struct sockaddr_in needed by the socket, or, after bind() returns, struct sockaddr_in is not needed anymore?
Are any implicit references somehow made by the socket?
To be more precise, is the following code safe?
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
int some_other_function() {
int listenfd;
/* struct sockaddr_in is defined locally in some_other_function()*/
struct sockaddr_in serv_addr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
// setting 'serv_addr'
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr));
if (listen(listenfd, 10) == -1) {
printf("Failed to listen\n");
return -1;
}
return listenfd;
} /* if the socket will refer to &serv_addr from now on, it'll dereference a dangling pointer */
int main(int argc, char* argv[]) {
int listenfd=some_other_function();
int connfd;
while(1) {
/** is sockaddr_in serv_addr being referenced here? is it needed by accept()? **/
connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL);
// do some writing/reading with connfd...
close(connfd);
}
return 0;
}
No, it does not. The contents of the address parameter are copied by the kernel; you can reuse or free that memory as soon as the call returns.
In fact, it's possible to make a more general statement here: unless the documentation specifically says otherwise, arguments passed to the kernel by pointer reference only need to be kept 'alive' for the duration of the system call. Their values will be copied by the kernel if needed.
It is safe. bind() does not keep a reference to the pointer after it returns. It's only taking its input as a pointer because it's receiving a variable-sized struct, which can't be passed by value. (It's polymorphism in C, essentially.)
Related
I am trying to create two threads that can listen at two different ports.
My logic is:
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdbool.h>
typedef struct server_arg {
int portNum;
} server_arg;
void *server_socket_creation(void *arg) {
server_arg *s = (server_arg*)arg;
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0))
== 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
int enable = 1;
if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&enable,sizeof(int)) < 0) {
perror("error");
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(s->portNum);
if (bind(server_fd, (struct sockaddr*)&address,
sizeof(address))
< 0) {
perror("bind failed");
}
if (listen(server_fd, 3) < 0) {
perror("listen");
}
if ((new_socket
= accept(server_fd, (struct sockaddr*)&address,
(socklen_t*)&addrlen))
< 0) {
perror("accept");
}
printf("Server Connected\n");
}
int main(int argc, char const* argv[])
{
server_arg *s = (server_arg*)malloc(sizeof(server_arg));
pthread_t id_1;
pthread_t id_2;
s->portNum = htons(9191);
pthread_create(&id_1,NULL,(void *)server_socket_creation,(void *)s);
s->portNum = htons(6123);
pthread_create(&id_2,NULL,(void *)server_socket_creation,(void *)s);
pthread_detach(id_1);
pthread_detach(id_2);
pthread_exit(0);
}
error: binding error: Address Already in use
Now, after searching about this issue on stackoverflow:
I could find 2 reasons:
Ports are already in use. (Means using same ports in this case)
which is not the case here as you can see.
server IP address is same.
I think this is the issue here, but I can't think of any way to solve this.
You have a single server_arg structure. You pass a pointer to this single server_arg structure to both threads.
There's a very significantly large chance that the assignment
s->portNum = htons(6123);
will happen before the first thread have copied the value with
address.sin_port = htons(s->portNum);
This is what is called a data-race. And as any data-race, the only looser is you as the programmer.
You need two structures, one for each thread. For example as an array:
server_arg s[2[ = {
{ 9191 },
{ 6123 }
};
Then pass &s[0] to the first thread, and &s[1] to the second.
There's also another problem with the port number, which should be quite easily seen when you put both the port assignments close to each other like I did above: You call htons twice.
I have the following code for client and server
#include <sys/types.h>
#include <netinet/in.h>
#include <inttypes.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <strings.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int socket_fd;
struct sockaddr_in dest;
struct hostent *hostptr;
struct { char head; u_long body; char tail; } msgbuf;
socket_fd = socket (AF_INET, SOCK_DGRAM, 0);
bzero((char *) &dest, sizeof(dest)); /* They say you must do this */
hostptr = gethostbyname(argv[1]);
dest.sin_family = (short) AF_INET;
bcopy(hostptr->h_addr, (char *)&dest.sin_addr,hostptr->h_length);
dest.sin_port = htons((u_short)0x3333);
msgbuf.head = '<';
msgbuf.body = htonl(getpid()); /* IMPORTANT! */
msgbuf.tail = '>';
sendto(socket_fd,&msgbuf,sizeof(msgbuf),0,(struct sockaddr *)&dest,
sizeof(dest));
return 0;
}
server:
#include <sys/types.h>
#include <netinet/in.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <strings.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int socket_fd, cc, fsize;
struct sockaddr_in s_in, from;
struct { char head; u_long body; char tail;} msg;
socket_fd = socket (AF_INET, SOCK_DGRAM, 0);
bzero((char *) &s_in, sizeof(s_in)); /* They say you must do this */
s_in.sin_family = (short)AF_INET;
s_in.sin_addr.s_addr = htonl(INADDR_ANY); /* WILDCARD */
s_in.sin_port = htons((u_short)0x3333);
printsin( &s_in, "RECV_UDP", "Local socket is:");
fflush(stdout);
bind(socket_fd, (struct sockaddr *)&s_in, sizeof(s_in));
for(;;) {
fsize = sizeof(from);
cc = recvfrom(socket_fd,&msg,sizeof(msg),0,(struct sockaddr *)&from,&fsize);
//printsin( &from, "recv_udp: ", "Packet from:");
printf("Got data ::%c%ld%c\n",msg.head,(long) ntohl(msg.body),msg.tail);
fflush(stdout);
}
return 0;
}
I'm looking for a way to change this code so that:
1.The client will send my name to the server and then will receive the server response.
2.On the server side, the server will receive the client name (instead of the current msg structure) and will send back its name.
I'm assuming I should just put my name in the msgbuf.body like this
msgbuf.head = '<';
msgbuf.body = 'liana';
msgbuf.tail = '>';
and delete the
msgbuf.body = htonl(getpid()); line.
or maybe make a new string for my name like this string name="liana";
and put it in the msgbuf.body like this msgbuf.body=name;(???)
is this the right deriction?
for reciving the response of the server I assume it is the same way as it was done for the server
should I add to client something like this?
int socket_fd, cc, fsize; // the socket that we receive to
struct sockaddr_in s_in, from; // decleration of the server and sending
(to the server) struct
fflush(stdout);//to ensure that whatever you just wrote to a file/the console is indeed written out on disk/the console.
bind(socket_fd, (struct sockaddr *)&s_in, sizeof(s_in));// conecting
between
the socket and all the details we entered
for(;;) {//infinite loop
fsize = sizeof(from);//set the size of the socket we resive to
cc = recvfrom(socket_fd,&msg,sizeof(msg),0,(struct sockaddr
*)&from,&fsize);//recive massage using UDP protocol
printf("Got data ::%c%ld%c\n",msg.head,(long) ntohl(msg.body),msg.tail);
//print the whole massage
fflush(stdout);//to ensure that whatever you just wrote to a file/the
console is indeed written out on disk/the console.
}
and just leave it like this without changing anything?
**how can I make the server receive the my name (instead of the current msg
structure)and send it back?
should I send it back using the
sendto(socket_fd,&msgbuf,sizeof(msgbuf),0,(struct sockaddr *)&dest,
sizeof(dest));
line?
**if I cant use the structure anymore how should i change this line?****
any help whould be appreciated,I'm kind of new to C and never worked with the client/server model
I've read answers to other similar questions, but none of them seemd to resolve my problem.
This is my code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
typedef struct {
pthread_t thread_id;
int sockfd;
} client_t;
client_t *clients;
size_t client_n = 0;
void *client_thread(void *client_ptr) {
client_t client = *(client_t*) client_ptr;
char buffer[500];
int state;
while(1) {
state = send(client.sockfd, 0, 1, MSG_NOSIGNAL);
if(state == -1) {
printf("socket-%d closed\n", client.sockfd);
break;
}
read(client.sockfd, buffer, 500);
printf("from socket-%d: %s\n", client.sockfd, buffer);
memset(buffer, 0, 500);
}
close(client.sockfd);
free(client_ptr);
client_n--;
}
int main(int argc, char *argv[]) {
int sockfd, newsockfd, clilen;
struct sockaddr_in clientaddr, serveraddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = INADDR_ANY;
serveraddr.sin_port = htons(8080);
bind(sockfd, (struct sockaddr*) &serveraddr, sizeof(serveraddr));
listen(sockfd, 5);
clilen = sizeof(clientaddr);
clients = (client_t*) malloc(sizeof(client_t));
while(1) {
newsockfd = accept(sockfd, (struct sockaddr*) &clientaddr, &clilen);
printf("New connection: socket-%d\n", newsockfd);
clients = (client_t*) realloc(clients, (client_n + 1) * sizeof(client_t));
clients[client_n].sockfd = newsockfd;
pthread_create(&clients[client_n].thread_id, NULL, client_thread, (void*) &clients[client_n]);
client_n++;
}
return 0;
}
The program should listen for incoming connections and then create a new thread for each. The program will then handle each single client simultaneously. Since this should be the core of a game server, i created a structure to contain each players information.
It all worked fine untill i added the:
close(client.sockfd);
free(client_ptr);
client_n--;
Any idea what the problem is?
With
free(client_ptr);
client_n--;
there are two problems.
The first is the free call. You didn't actually call malloc (or realloc or calloc) for client_ptr. Instead client_ptr is pointing into an array that you allocated dynamically, but the element pointed to by client_ptr wasn't itself allocated dynamically separately. That leads to undefined behavior when you pass a pointer to free that wasn't actually allocated with malloc and family. Except for the very first element (i.e. clients[0]), when you instead free the whole array. The solution to this is to simply not call free in the thread.
The other problem is with the client_n-- expression. You don't protect this (or the corresponding client_n++ in the main function) from modification by other threads. That means two or more threads could possibly modify this simultaneously again leading to undefined behavior. You need to have a semaphore or mutex to protect this modification.
There are also a couple of other problems. For example you don't join the threads that have ended, leading to resource leaks. You don't check for errors or closed connection from the read call (a nicely closed connection is reported by the read call returning 0).
i've done a simple client/server program where the server wait for an external connection and return the connection-socket if the port number of the client is in the range of [1025-2048] otherwise return -1. The problem is that when i get the port number by the client adress (which should be stored in the sockaddr structure) it says me that the client port number is zero, but in the client program i've set the client portnumber to 1999.
SERVER
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
int function(int fd_socket) {
int fd_socket_acc;
int len;
int port;
struct sockaddr_in client_addr;
puts("WAITING FOR CLIENT...");
fd_socket_acc = accept(fd_socket, (struct sockaddr*)&client_addr, &len);
puts("CONNECTION DONE.");
port = ntohs (client_addr.sin_port);
printf("client port number: %d \n", port);
if (port >= 1024 && port <= 2048) {
close (fd_socket_acc);
return fd_socket_acc;
}
else {
close(fd_socket_acc);
return -1;
}
}
int main(int argc, char *argv[]) {
int fd_socket;
struct sockaddr_in local_addr;
fd_socket = socket(AF_INET, SOCK_STREAM, 0);
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(1887);
local_addr.sin_addr.s_addr = INADDR_ANY;
bind(fd_socket, (struct sockaddr*)&local_addr, sizeof(local_addr));
listen(fd_socket, 3);
function(fd_socket);
//close(fd_socket);
}
CLIENT
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
int main(int argc, char *argv[]) {
int fd_socket;
struct sockaddr_in local_addr;
struct sockaddr_in server_addr;
struct hostent *hp;
fd_socket = socket(AF_INET, SOCK_STREAM, 0);
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(1999);
local_addr.sin_addr.s_addr = INADDR_ANY;
bind(fd_socket, (struct sockaddr*)&local_addr, sizeof(local_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(1887);
//hostname is "ubuntu"
hp = gethostbyname("ubuntu");
bcopy(hp->h_addr, &server_addr.sin_addr, 4);
printf("%d \n", ntohs(local_addr.sin_port));
connect(fd_socket, (struct sockaddr*)&server_addr, sizeof(server_addr));
wait(2);
close(fd_socket);
}
If i get the port number in client with a printf("%d", ntohs(local_addr.sin_port)) it stamps correctly 1999, but if i get the port number of client in server with printf("%d", ntohs(client_addr.sin_port)) it stamps 0. Why?
thanks in advance!
In order to obtain the client port number in client_addr through accept you have to tell accept how big that buffer is by setting
socklen_t len = sizeof(client_addr);
You can alternatively retrieve it by calling afterwards
len = sizeof(client_addr);
getpeername(fd_socket_acc, (struct sockaddr*)&client_addr, &len);
Maybe because you do not set the variable len to anything, and I suspect that your compiler sets it to 0.
What happens is that you try to accept with an undefined len size.
Adding len=sizeof( struct sockaddr_in ); before making a call to accept would help to fill the passed client_addr correctly.
I am reading a book on sockets in c and am making a very simple server program. I copied the code verbatim. There is nothing trying to connect to this server program yet and have change the port multiple times to make sure.
The program is failing on the accept method on the first run of the loop. From what I read in the man pages, accept is supposed to block the caller until a connection is made, and not fail if there aren't connections in the queue like it is doing. Is there any reason accept would be returning a value less than 0? I will post the code up to where it fails:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//Max number of outstanding connection requests
#define MAXPEDNING 5
#define NONE
#define BUFSIZE 1024
int main(int argc, char **argv)
{
in_port_t servPort;
#ifdef CMDLINE
if(argc != 2)
{
puts("Error! Usage is <Server Port>");
return 0;
}
servPort = atoi(argv+1);
#endif //CMDLINE
#ifdef NONE
servPort = 2549;
#endif //NONE
int servSock;
if((servSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
puts("socket() failed");
return 0;
}
struct sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(servPort);
printf("Port in network order: %d\nPort in host order:%d\n", servAddr.sin_port, ntohs(servAddr.sin_port));
if((bind(servSock, (struct sockaddr*) &servAddr, sizeof(servAddr)))<0)
{
puts("bind failed");
return 0;
}
while(1)
{
struct sockaddr_in clntAddr;
socklen_t clntAddrLen = sizeof(clntAddr);
int clntSock = accept(servSock, (struct sockaddr*) &clntAddr, &clntAddrLen);
if(clntSock < 0)
{
puts("accept failed");
return 0;
}
It reaches the accept failed and quits. The only thing that is odd about my setup is that I am on a Debian VM (VirtualBox) and I am wondering if network operations are handled in a weird way. I shouldn't be even trying to accept anything because there are no connections.
You need to call listen() in between bind() and accept().