I'm just starting to learn network programming in C. I did some tests, but i got stuck with an error.
I have a client:
client.c
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
int main(void)
{
struct addrinfo hints, *res;
int sock;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo("localhost", "5996", &hints, &res);
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
connect(sock, res->ai_addr, res->ai_addrlen);
char data[64];
int len = 13;
int br = recv(sock, data, len, 0);
printf("%s\n%s\n%d\n", strerror(errno), data, br);
return 0;
}
and a server:
server.c
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MYPORT "5996"
#define BACKLOG 10
int main(void)
{
struct sockaddr_storage their_addr;
socklen_t addr_size;
struct addrinfo hints, *res;
int sockfd, new_fd;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
getaddrinfo(NULL, MYPORT, &hints, &res);
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
bind(sockfd, res->ai_addr, res->ai_addrlen);
listen(sockfd, BACKLOG);
addr_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
char *msg = "Hello World!!";
int len = strlen(msg);
int bs = send(new_fd, msg, len, 0);
close(sockfd);
}
When i start the server, it waits for a connection, and if i start the client i get the message "Hello World!!", but then, for a minute or so, if i try to run the server and then run the client again, i get the message "Transport endpoint is not connected" from the strerror() call.
I did read other questions about this, but the problem was that the data should be sent to the socket returned from the accept() call ...but i think that's what i'm doing. What am I doing wrong?? ...i know it is something stupid, but i'm a real beginner.
If you restart your server on the same port every time, you may be finding that the port is still treated as being in use by the OS. This is to allow time for delivery of any in-progress requests for clients from the last server run.
You can over-ride this by calling setsockopt
for opcode SO_REUSEADDR
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
int reuse = 1;
int err = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
if (0 == err)
err = bind(sockfd, res->ai_addr, res->ai_addrlen);
Note that the above snippet checks error returns. You'd have discovered the problem (if not the solution) yourself here if you checked the return code of each call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With