I have written this code in a project based on the client-server model but in one of my files I have this error: fork error resource temporarily unavailable.
this is the
Code:
#include "LibreriaFunzioni.c"
int main (int argc , char *argv[])
{
int list_fd,list_fd2,fd,fd2,SocketFiglioSede;
int i;
struct sockaddr_in s_addr,s_addr2,c_addr,c_addr2;
char buffer [4096];
socklen_t len,len2;
pid_t pid_Admin,pid_Client,pid;
int logging =1,N_Portate;
PORTATA *Menu = NULL;
ORDINE Ordine;
ORDINE OrdineID;
Menu = CaricaMenuPortate(&N_Portate);
//------------------ADMIN--------------------------------------
/* create socket ADMIN*/
if ( (list_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation error");
exit(1);
}
/* initialize address */
memset((void *)&s_addr, 0, sizeof(s_addr)); /* clear server address */
s_addr.sin_family = AF_INET; /* address type is INET */
s_addr.sin_port = htons(2700); /* echo port is 7 */
s_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* connect from anywhere */
/* bind socket */
if (bind(list_fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) < 0) {
perror("bind error");
exit(1);
}
/* main body */
if (listen(list_fd, 4096) < 0) {
perror("listen error");
exit(1);
}
//-----------------------CLIENT-------------------------------------
/* create socket CLIENT */
if ( (list_fd2 = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation error");
exit(1);
}
/* initialize address */
memset((void *)&s_addr2, 0, sizeof(s_addr2)); /* clear server address */
s_addr2.sin_family = AF_INET; /* address type is INET */
s_addr2.sin_port = htons(2800); /* echo port is 7 */
s_addr2.sin_addr.s_addr = htonl(INADDR_ANY); /* connect from anywhere */
/* bind socket */
if (bind(list_fd2, (struct sockaddr *) &s_addr2, sizeof(s_addr2)) < 0) {
perror("bind error");
exit(1);
}
/* main body */
if (listen(list_fd2, 4096) < 0) {
perror("listen error");
exit(1);
}
while(1){
//---------GESTIONE ADMIN--------------------------------------------
pid_Admin = fork();
if(pid_Admin < 0) {
perror (" fork error ");
exit ( -1);
}
if(pid_Admin == 0)
{
len = sizeof(c_addr);
if ((fd = accept(list_fd, (struct sockaddr *) &c_addr, &len)) < 0) {
perror("accept error");
exit(1);
}
close ( list_fd );
if(logging)
{
inet_ntop(AF_INET, &c_addr.sin_addr, buffer, sizeof(buffer));
printf("\nNUOVA SESSIONE con admin IP:PORTA => %s:%d su socket => %d\n", buffer, ntohs(c_addr.sin_port), fd);
SpedisciTabella(fd, N_Portate, Menu);
}
close(fd);
exit (0);
}
//-------------GESTIONE CLIENT----------------------------------------
pid_Client = fork();
if(pid_Client < 0) {
perror (" fork error ");
exit ( -1);
}
if(pid_Client == 0)
{
len2 = sizeof(c_addr2);
if ((fd2 = accept(list_fd, (struct sockaddr *) &c_addr2, &len2)) < 0) {
perror("accept error");
exit(1);
}
close ( list_fd2 );
if(logging)
{
inet_ntop(AF_INET, &c_addr.sin_addr, buffer, sizeof(buffer));
printf("\nNUOVA SESSIONE con client IP:PORTA => %s:%d su socket => %d\n", buffer, ntohs(c_addr.sin_port), fd);
SpedisciTabella(fd2, N_Portate, Menu);
}
pid = fork();
if((pid<0))
{perror ("Errore nella fork()"); exit (-1);}
if(pid==0)
{
read(fd2, &Ordine, sizeof(Ordine));
if(Ordine.sede == 1){
SocketFiglioSede = ConnettiAlServer("127.0.0.1", 2200);
FullWrite(SocketFiglioSede, &Ordine, sizeof(Ordine));
read(SocketFiglioSede,&Ordine, sizeof(Ordine));
printf("\nID %d",Ordine.ID);
printf("\nCOD %d",Ordine.SceltaP);
FullWrite(fd2, &Ordine, sizeof(Ordine));
printf("\n** (PADRE) Disconnetti client su socket ==> %d **\n", fd2);
close(SocketFiglioSede); /
exit(0);
}// Chiudi processo
if(Ordine.sede == 2){
SocketFiglioSede = ConnettiAlServer("127.0.0.1", 2300);
FullWrite(SocketFiglioSede, &Ordine, sizeof(Ordine));
read(SocketFiglioSede,&Ordine, sizeof(Ordine));
FullWrite(fd2, &Ordine, sizeof(Ordine));
close(SocketFiglioSede);
printf("\n** (PADRE) Disconnetti client su socket ==> %d **\n", fd2);
exit(0);
}// Chiudi processo
}
close(fd2);
exit (0);
}
}
exit (0);
}
I try to correct the code but I fail all the time.
Can you help me?
Related
I'm writing an ftp server in C using fork() to handle multiple client. The
create part and bind part are as follows:
//create socket
int createSocket()
{
int listenfd;
if ((listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
printf("Error socket(): %s(%d)\n", strerror(errno), errno);
exit(1);
}
printf("socketfd is: %d\n", listenfd);
return listenfd;
}
int bindSocketToServer(int listenfd, int port)
{
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = port;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(listenfd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
{
printf("Error bind(): %s(%d)\n", strerror(errno), errno);
return -1;
}
//It is normal here.
pirntf("It is still normal here.\n");
return 1;
}
pasvPort = 30000;
pasvFilefd = createSocket();
while(1)
{
printf("start.\n");
if(bindSocketToServer(pasvFilefd, pasvPort) > 0)
{
printf("going to break.\n");
break;
}
else
pasvPort += 1;
printf("error.\n");
}
printf("Listening port%d", pasvPort); //It can be printed.
These work fine when I start my server and listen to port 21 in main process. But when I try to handle 'pasv' command and bind a socket to a new port as follows(in another process created by fork()), the process dies and I don't know why.
Edit: That's how I use fork().And in serveOneClient() I handle the 'pasv' command.
pid = fork();
if(pid < 0)
{
printf("Error fork(): %s(%d)\n", strerror(errno), errno);
continue;
}
else if(pid == 0)
{
close(listenfd);
serveOneClient(connfd);
printf("serving one over.\n");
close(connfd);
return 0;
}
else
close(connfd);
I have a simply program with server and client that comunicate with AF_STREAM socket.
Client will can send multiple request to server, and the server response with a string but I don't know the lenght. The client stops in reading when read return 0 and restarts after the server stops the comunication. I don't understand why.
//SEVER
int main(void){
int idSockServer, idSockClient, clientLen;
struct sockaddr_in indirizzoClient, indirizzoServer;
unsigned short richiesta = 0;
char nomeFile[L_STRING], buffer[L_BUFFER];
printf("Inizializzaione del server\n");
idSockServer = socket(AF_INET, SOCK_STREAM, 0);
if(idSockServer == -1){
fprintf(stderr, "Errore socket\n");
exit(EXIT_FAILURE);
}
indirizzoServer.sin_addr.s_addr = htonl(INADDR_ANY);
indirizzoServer.sin_port = htons(5566);
indirizzoServer.sin_family = AF_INET;
if(bind(idSockServer, (struct sockaddr *) &indirizzoServer, sizeof(indirizzoServer))){
fprintf(stderr, "Errore bind\n");
exit(EXIT_FAILURE);
}
if(listen(idSockServer, 5) == -1){
fprintf(stderr, "Errore listen\n");
exit(EXIT_FAILURE);
}
while(1){
printf("[SERVER] Pronto!\n");
clientLen = sizeof(indirizzoClient);
idSockClient = accept(idSockServer, (struct sockaddr *) &indirizzoClient, &clientLen);
if(idSockClient == -1){
printf("[SERVER] Errore alla connessione\n");
continue;
}
read(idSockClient, &richiesta, L_BUFFER);
switch(richiesta){
case 1:
printf("[SERVER] Richiesta 1 accetata, invio risposta\n");
//this simulates the variable lenght string
strcpy(buffer, "un file\n");
write(idSockClient, buffer, L_BUFFER);
break;
}
sleep(6);
close(idSockClient);
}
}
Here the client
//CLIENT
int main(void) {
int sockIdClient, clientLen, file, len;
char buffer[L_BUFFER], nomeFile[L_STRING];
struct sockaddr_in indirizzoClient;
unsigned short scelta, risposta = 0;
sockIdClient = socket(AF_INET, SOCK_STREAM, 0);
if (sockIdClient == -1) {
perror("Errore creazione socket");
exit(EXIT_FAILURE);
}//fine if creazione socket
indirizzoClient.sin_addr.s_addr = htonl(INADDR_ANY);
indirizzoClient.sin_family = AF_INET;
indirizzoClient.sin_port = htons(5566);
if (connect(sockIdClient, (struct sockaddr *) &indirizzoClient, sizeof (indirizzoClient)) == -1) {
perror("Errore connessione al server");
exit(EXIT_FAILURE);
}//fine connect
do {
printf("1) File disponibili\n");
printf("2) Scarica file\n");
printf("0) Esci\n");
printf("Scelta --> ");
scanf("%hu", &scelta);
switch (scelta) {
case 1:
write(sockIdClient, &scelta, sizeof (unsigned short));
len = read(sockIdClient, buffer, L_BUFFER);
while (len > 0) {
printf("%s", buffer);
printf("%d\n", len);
len = read(sockIdClient, buffer, len);
}
break;
case 2:
//This part of code is not managed by Server yet
write(sockIdClient, &scelta, sizeof (unsigned short));
read(sockIdClient, &risposta, sizeof (int));
if (risposta) {
printf("Nome file: ");
leggiStringa(nomeFile, L_STRING);
write(file = sockIdClient, nomeFile, L_STRING);
if (open(nomeFile, O_WRONLY | O_CREAT, "0666") == -1) {
perror("Errore apertura del file");
close(sockIdClient);
exit(EXIT_FAILURE);
}//fine open
len = read(sockIdClient, buffer, L_BUFFER);
while (len > 0) {
write(file, buffer, L_BUFFER);
printf("leggo..\n");
len = read(sockIdClient, buffer, len);
}
close(file);
} else printf("Il server non ha accetato la richiesta\n");
break;
default:
printf("Scelta non valida");
}
} while (scelta != 0);
close(sockIdClient);
}
Hello i made this code to open a socket and make a thread to send data so the socket
int is_valid_fd(int fd)
{
return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
}
int main(int Count, char *Strings[])
{
pfd.events = POLLIN;
pfd.revents = 0;
/*---Create streaming socket---*/
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
perror("Socket");
exit(errno);
}
/*---Initialize address/port structure---*/
bzero(&self, sizeof(self));
self.sin_family = AF_INET;
self.sin_port = htons(MY_PORT);
self.sin_addr.s_addr = INADDR_ANY;
/*---Assign a port number to the socket---*/
if ( bind(sockfd, (struct sockaddr*)&self, sizeof(self)) != 0 )
{
perror("socket--bind");
exit(errno);
}
/*---Make it a "listening socket"---*/
if ( listen(sockfd, 20) != 0 )
{
perror("socket--listen");
exit(errno);
}
err = pthread_create(&(tid), NULL, &thread_accept, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
/*---Forever... ---*/
while(1){
if(0<poll(&pfd,1,100)){
if(pfd.revents & POLLIN){
run = read(pfd.fd, &t,1);
}
}
if(run){
if(is_valid_fd(clientfd))send(clientfd, "12 ",3,0);
/*---Close data connection---*/
}
printf("hejsa\n");
fflush(stdout);
sleep(1);
}
/*---Clean up (should never get here!)---*/
close(sockfd);
return 0;
}
void* thread_accept(){
while (1){
/*---accept a connection (creating a data pipe)---*/
clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);
pfd.fd=clientfd;
printf("%s:%d, connected\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
run=1;
/*---Echo back anything sent---*/
send(clientfd, buffer, recv(clientfd, buffer, MAXBUF, 0), 0);
//close(clientfd);
}
}
My problem is then, that when i close the socket connection, the program shuts down instead of just closing the socket and keep printf("hejsa\n)
I don't see where you break the while loop?
you should also protect your shared data with mutex or something.
I have a short example that do something similar(but in different way) here
I want to read a file on Server from the Client application on Linux. I have mentioned below my Server and Client application.
On Client side I open a file (which is necessary to my requirements).
The Client receive the data which I have written on the file. Now, I want to send the received data from Client to the Server side and get back the echo to the Client.
I am facing trouble with the Server to accept the Client connection. I am glad to have a solution, where I made mistake exactly. I also appreciate If I get the solution for the code. Thanks in advance.
Server.C
#include<stdio.h>
#define BufferLength 100
/* Server port number */
#define SERVPORT 3389
int main(int argc, char *argv[])
{
/* Variable and structure definitions. */
int sd, sd2, rc, length = sizeof(int);
int totalcnt = 0, on = 1;
char temp;
char buffer[BufferLength];
//char data[BufferLength];
struct sockaddr_in serveraddr;
struct sockaddr_in their_addr;
fd_set read_fd;
struct timeval timeout;
timeout.tv_sec = 15;
timeout.tv_usec = 0;
/*To read xillybus*/
// FILE *fp;
// fp = fopen("/dev/xillybus_read_8", "r");
// if(fp==NULL)
// {
// printf("Error file open\n");
// exit(-1);
// }
// else{
// printf("File found %s\n", fp);
// }
/* Get a socket descriptor */
if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Server-socket() error");
exit (-1);
}
else
{
printf("Server-socket() is OK\n");
}
/* Allow socket descriptor to be reusable */
if((rc = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) < 0)
{
perror("Server-setsockopt() error");
close(sd);
exit (-1);
}
else
{
printf("Server-setsockopt() is OK\n");
}
/* bind to an address */
memset(&serveraddr, 0x00, sizeof(struct sockaddr_in));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVPORT);
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
printf("Using %s, listening at %d\n", inet_ntoa(serveraddr.sin_addr), SERVPORT);
if((rc = bind(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0)
{
perror("Server-bind() error");
close(sd);
exit(-1);
}
else
{
printf("Server-bind() is OK\n");
}
/* Up to 10 clients can be queued */
if((rc = listen(sd, 10)) < 0)
{
perror("Server-listen() error");
close(sd);
exit (-1);
}
else
{
printf("Server-Ready for client connection...\n");
}
/* accept() the incoming connection request. */
int sin_size = sizeof(struct sockaddr_in);
if((sd2 = accept(sd, (struct sockaddr *)&their_addr, &sin_size)) < 0)
{
perror("Server-accept() error");
close(sd);
exit (-1);
}
else
{
printf("Server-accept() is OK\n");
}
/*client IP*/
printf("Server-new socket, sd2 is OK...\n");
printf("Got connection from the client: %s\n", inet_ntoa(their_addr.sin_addr));
/* Wait for up to 15 seconds on */
/* select() for data to be read. */
FD_ZERO(&read_fd);
FD_SET(sd2, &read_fd);
rc = select(sd2+1, &read_fd, NULL, NULL, &timeout);
if((rc == 1) && (FD_ISSET(sd2, &read_fd)))
{
/* Read data from the client. */
totalcnt = 0;
while(totalcnt < BufferLength)
{
/* read() from client */
rc = read(sd2, &buffer[totalcnt], (BufferLength - totalcnt));
if(rc < 0)
{
perror("Server-read() error");
close(sd);
close(sd2);
exit (-1);
}
else if (rc == 0)
{
printf("Client program has issued a close()\n");
close(sd);
close(sd2);
exit(-1);
}
else
{
totalcnt += rc;
printf("Server-read() is OK\n");
}
}
}
else if (rc < 0)
{
perror("Server-select() error");
close(sd);
close(sd2);
exit(-1);
}
/* rc == 0 */
else
{
printf("Server-select() timed out.\n");
close(sd);
close(sd2);
exit(-1);
}
/* Shows the data */
printf("Received data from the client: %s\n", buffer);
printf("Server-Echoing back to client...\n");
/*Write function*/
rc = write(sd2, buffer, totalcnt);
if(rc != totalcnt)
{
perror("Server-write() error");
rc = getsockopt(sd2, SOL_SOCKET, SO_ERROR, &temp, &length);
if(rc == 0)
{
/* Print out the asynchronously */
/* received error. */
errno = temp;
perror("SO_ERROR was: ");
}
else
{
printf("Server-write() is OK\n");
}
close(sd);
close(sd2);
exit(-1);
}
/******************************************/
close(sd2);
close(sd);
exit(0);
return 0;
}
Client.c
#include<stdio.h>
#define BufferLength 100
#define SERVER "00.00.00.00"
/* Server's port number */
#define SERVPORT 3389
/* set the server name in the #define SERVER ... */
int main(int argc, char *argv[])
{
/* Variable and structure definitions. */
int sd, rc, length = sizeof(int);
struct sockaddr_in serveraddr;
char buffer[BufferLength];
char server[255];
char temp;
int totalcnt = 0;
struct hostent *hostp;
//char data[500] = "Hello ravi !!! "; // writes data to the server
/*To open directory and read the xillybus*/
FILE *fp;
char s[100];
fp = fopen("/dev/xillybus_read_8", "r");
if(!fp)
return 1;
while(fgets(s,100,fp)!=NULL){
printf("%s ", s);
}
//fclose(fp);
/******************************************/
/* get a socket descriptor */
if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Client-socket() error");
exit(-1);
}
else
{
printf("Client-socket() OK\n");
}
/*If the server hostname is supplied*/
if(argc > 1)
{
/*Use the supplied argument*/
strcpy(server, argv[1]);
printf("Connecting to the server %s, port %d ...\n", server, SERVPORT);
}
else
{
/*Use the default server name or IP*/
strcpy(server, SERVER);
memset(&serveraddr, 0x00, sizeof(struct sockaddr_in));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVPORT);
}
if((serveraddr.sin_addr.s_addr = inet_addr(server)) == (unsigned long)INADDR_NONE)
{
/* get host address */
hostp = gethostbyname(server);
if(hostp == (struct hostent *)NULL)
{
printf("HOST NOT FOUND --> ");
/* h_errno is usually defined */
/* in netdb.h */
printf("h_errno = %d\n",h_errno);
printf("---This is a client program---\n");
printf("Command usage: %s <server name or IP>\n", argv[0]);
close(sd);
exit(-1);
}
memcpy(&serveraddr.sin_addr, hostp->h_addr, sizeof(serveraddr.sin_addr));
}
/* connect() to server. */
if((rc = connect(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0)
{
perror("Client-connect() error");
close(sd);
exit(-1);
}
else
{
printf("Connection established...\n");
printf("Sending some string to the server %s...\n", server);
}
/* Write() some string to the server. */
//rc = write(sd, data, sizeof(data));
rc = write(sd,fp, sizeof(fp)); // here is my write function
if(rc < 0)
{
perror("Client-write() error");
rc = getsockopt(sd, SOL_SOCKET, SO_ERROR, &temp, &length);
if(rc == 0)
{
/* Print out the asynchronously received error. */
errno = temp;
perror("SO_ERROR was");
}
close(sd);
exit(-1);
}
else
{
printf("Client-write() is OK\n");
printf("String successfully sent \n");
printf("Waiting the %s to echo back...\n", server);
}
totalcnt = 0;
/* Read data from the server. */
while(totalcnt < BufferLength)
{
rc = read(sd, &buffer[totalcnt], BufferLength-totalcnt); //&buffer[totalcnt],BufferLength-totalcnt
if(rc < 0)
{
perror("Client-read() error");
close(sd);
exit(-1);
}
else if (rc == 0)
{
printf("Server program has issued a close()\n");
close(sd);
exit(-1);
}
else
{
totalcnt += rc;
}
}
printf("Client-read() is OK\n");
printf("Echoed data from the server: %s\n", buffer); //data ->buffer
/* Close socket descriptor from client side. */
close(sd);
exit(0);
return 0;
}
In you client.c program, you try to send a FILE* to the server. What you are sending is the address of the file descriptor in client program and not the text contained in the file.
You send only 4 or 8 bytes (depending on architecture 32 or 64 bits), while server waits for at least 100 bytes before echoing anything. So both programs do exactly what you programmed - but not what you expected.
To send the content of a file, you read the file in a char array and send the char array. You can iterate over the file reading one buffer at a time and sending the buffer. But there is no magic allowing to send a whole file.
UDP server doesn't receive the data sent by UDP client program.I feel there is something wrong im doing in UDP Server program. Thanks.
send_client_message = malloc(message_size);
recv_client_message = malloc(message_size);
sockaddr_in_length = sizeof(struct sockaddr_in);
/* Create a datagram socket.*/
server_sock = socket(AF_INET,SOCK_DGRAM,0);
if (server_sock == return_error)
{
printf("Error in opening a datagram socket\n");
exit(0);
}
printf("Host sock = %d\n",server_sock);
/* Bind a local name to the socket.*/
server_sockaddr_in.sin_family = AF_INET;
server_sockaddr_in.sin_port = 4999;
server_sockaddr_in.sin_addr.s_addr = INADDR_ANY;
rc = bind(server_sock,(struct sockaddr *)&server_sockaddr_in,
sockaddr_in_length);
if (rc == return_error)
{
printf("Error in binding - \n");
(void)close(server_sock);
exit(0);
}
/* Get the server information, and print its data out.*/
rc = getsockname(server_sock,(struct sockaddr *)&server_sockaddr_in,
&sockaddr_in_length);
if (rc != return_error)
{
printf("Server Information\n");
printf("--------------------------------\n");
printf("Server sock - %d\n",server_sock);
printf("Server IP address - %x\n",
server_sockaddr_in.sin_addr.s_addr);
printf("Server port # - %d\n\n",
server_sockaddr_in.sin_port);
}
optval = message_size;
optlen = sizeof(optval);
rc = setsockopt(server_sock,SOL_SOCKET,SO_SNDBUF,(char *)&optval,
optlen);
if (rc == return_error)
{
//printf("Error in setsockopt - %d\n",sock_errno());
printf("Error in setsockopt - \n");
(void)close(server_sock);
exit(0);
}
printf(".... Ready for clients ....\n");
/* Loop forever */
for (;;)
{
/* Monitor incoming buffers. */
rc = recvfrom(server_sock,recv_client_message,message_size,0,
(struct sockaddr *)&client_sockaddr_in,
&sockaddr_in_length);
printf("Recieved packet from %s: %d\nData: %s\n\n", inet_ntoa(client_sockaddr_in.sin_addr), ntohs(client_sockaddr_in.sin_port), recv_client_message);
fflush(stdout);
if (rc == return_error)
{
printf("Error in receiving message \n");
break;
}
} /* end of for(;;) */
/* Return the socket back to the system and exit normally. */
rc = close(server_sock);
if (rc == return_error)
{
printf("Error in closing the socket \n");
exit(0);
}
printf("Datagram server terminated\n");
exit(0);
}
for(i = 0; i < NPACK; i += 1) {
printf("Sending packet %d\n", i);
sprintf(buf, "This is packet %d\n", i);
if(sendto(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, slen) == -1)
diep("sendto()");
}
close(s);
return 0;
}
server_sockaddr_in.sin_port = 4999;
You likely want to htons that 4999.