Trouble with socket connection in C and Linux -
Trouble with socket connection in C and Linux -
i working on simple project because wanted larn more socket programming under linux. created simple server take connections , respond single statement. however, added while loop maintain connection alive, maintain sending commands without disconnecting. however, 1 time send first command, server respond connection. after first response, sits idle , quits responding. can tell code, tried take scheme phone call within , outside of while loop because not sure if calling take more 1 time on same client cause hang. 1 time moved take phone call outside of while loop, function still hangs, not sure go here.
#include "server.h" #include <netinet/in.h> #include <sys/socket.h> #include <stdlib.h> #include <stdio.h> #include <string.h> struct server { int sockfd; int client_socket; //the socket used communication client int backlog; //maximum pending connections struct sockaddr_in *serv_addr; struct sockaddr_in *client_addr; //the clients info when connects }; int server_init(server **serv, int max_connections) { *serv = (server *)malloc(sizeof(server)); if (*serv) { (*serv)->backlog = max_connections; (*serv)->sockfd = socket(af_inet, sock_stream, ipproto_tcp); (*serv)->serv_addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in)); if ((*serv)->serv_addr) memset((*serv)->serv_addr->sin_zero, 0, sizeof((*serv)->serv_addr->sin_zero)); else { free(*serv); homecoming -1; } (*serv)->serv_addr->sin_family = af_inet; (*serv)->serv_addr->sin_addr.s_addr = inaddr_any; } else { homecoming -1; } homecoming 0; } /* * bind server port , start listening */ int server_bind(server *serv, int port) { int result = 0; serv->serv_addr->sin_port = htons(port); if (bind(serv->sockfd, (struct sockaddr *)serv->serv_addr, (socklen_t)sizeof(*(serv->serv_addr))) < 0) homecoming -1; //if bind phone call fails, dont want go on seek hear phone call if (listen(serv->sockfd, serv->backlog) < 0) result = -1; homecoming result; } int server_unbind(server *serv) { int result = 0; if (close(serv->client_socket) < 0) result = -1; if (close(serv->sockfd) < 0) result = -1; homecoming result; } int server_accept_connections(server *serv, int (*process_data)(char *buffer, ssize_t length, int client_scoket)) { int result = 0; char *buffer = (char *) malloc(sizeof(char) * 250); if (buffer) { int length = sizeof((serv)->client_addr); //may maintain calculating every time serv->client_socket = accept(serv->sockfd, (struct sockaddr *)(serv)->client_addr, &length); while (result == 0) { //int length = sizeof((serv)->client_addr); //serv->client_socket = accept(serv->sockfd, (struct sockaddr *)(serv)->client_addr, &length); ssize_t data_length = recv(serv->client_socket, buffer, sizeof(buffer), 0); result = (* process_data)(buffer, data_length, serv->client_socket); } result = 0; } else result = -1; free(buffer); homecoming result; } void server_destroy(server *serv) { if (serv) { free(serv->serv_addr); free(serv); } }
#include <stdio.h> #include "server.h" #include <string.h> #include <stdlib.h> int process_netdata(char *buffer, ssize_t length, int socket_desc) { printf("%s",buffer); if(strcmp(buffer, "quit") != 0) { char *str = "i got message"; int str_length = strlen(str); write(socket_desc, str, str_length); homecoming 0; } else { char *str = "quit received"; int str_length = strlen(str); write(socket_desc, str, str_length); homecoming -1; } } int main(int argc, char **argv) { server *server; int value = 0; value = server_init(&server, 10); printf("server_init: %i\n", value); value = server_bind(server, 2021); printf("server_bind: %i\n", value); value = server_accept_connections(server, &process_netdata); printf("server_accept: %i\n", value); value = server_unbind(server); printf("server_unbind: %i\n", value); value = server_destroy(server); printf("server_destroy: %i\n", value); homecoming 0; }
you missing fork parent continues listen(accept) new connections , while kid operates on established connection.
c linux sockets
Comments
Post a Comment