C - Creating endless socket connection between server and client (Linux) -
i have simple socket program created in c (using ubuntu), connection works fine, server compiles , client provides ip , port number.
but when connection established, client can send 1 message
, connection closes
. when client sends message, server receives , program ends itself
.
i tried use while loop
, gives me error on binding
. there way make infinite, when user enters exit
or specific key/command
, program ends.
server.c updated while loop
#include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<string.h> int main(int argc, char *argv[]) { int sockfd, newsockfd, portnum, clien, n; char buffer[256]; struct sockaddr_in serv_addr,cli_addr; if (argc < 2) { fprintf(stderr, "\nno port provided"); exit(1); } sockfd = socket(af_inet,sock_stream, 0); if (sockfd < 0) printf("\nprovide valid port number\n"); bzero((char *)&serv_addr, sizeof(serv_addr)); portnum = atoi(argv[1]); serv_addr.sin_family = af_inet; serv_addr.sin_addr.s_addr = inaddr_any; serv_addr.sin_port = htons(portnum); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0 ) printf("error on binding"); while(1) { listen(sockfd,5); } clien = sizeof(cli_addr); newsockfd=accept(sockfd, (struct sockaddr *) &cli_addr, &clien ); if (newsockfd < 0) printf("=> error on accepting...\n"); bzero(buffer, 256); n = read(newsockfd, buffer, 256); if (n < 0) printf("error reading socket...\n"); printf("%s\n", buffer); n = write(newsockfd, "message received.", 58); if (n < 0) printf("cannot write socket...\n"); return 0; }
client.c updated while loop
#include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netdb.h> #include<string.h> int main(int argc, char *argv[]) { int sockfd,portno,n; char buffer[256]; struct sockaddr_in serv_addr; struct hostent *server; //while(counter != 5) // /{ if (argc < 3) { fprintf(stderr,"usage %s hostname port...\n",argv[0]); exit(1); } portno = atoi(argv[2]); sockfd = socket(af_inet, sock_stream, 0); if (sockfd < 0) printf("\error opening socket...\n"); bzero((char *) &serv_addr, sizeof(serv_addr)); server = gethostbyname(argv[1]); if (server == null) { fprintf(stderr,"no such host...\n"); exit(0); } serv_addr.sin_family = af_inet; bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd, &serv_addr,sizeof(serv_addr)) <0 ) printf("\error connecting\n"); printf("your message:"); fgets(buffer, 256, stdin); while(1) { n = write(sockfd,buffer,strlen(buffer)); } if (n < 0) printf("error writing socket\n"); n = read(sockfd, buffer, 255); if (n < 0) printf("error reading socket\n"); printf("%s\n", buffer); return 0; }
thank you.
the problem server terminate whenever receive message. should keep on listening recv() function until client disconnects. here 1 piece of code should trick.
disclaimer: haven't tested it, it's old project modified on fly it's supposed work i'm not 100% sure does, have no time test right feel free ask questions if there wrong, i'll try correct it.
edit: trying re-bing causes error because binding done , port busy cannot "assign" other program port therefore giving error. thing have make sure server listens until client disconnects server.
also, possible of code made c++ @ point, networking part should work fine.
feel free try out , comment if have questions. server.c:
int socket_desc , client_sock , c , *new_sock; struct sockaddr_in server , client; socket_desc = socket(af_inet , sock_stream , 0); if (socket_desc == -1) { //handle error } printf("socket created\n"); //prepare sockaddr_in structure server.sin_family = af_inet; server.sin_addr.s_addr = inaddr_any; server.sin_port = htons( 8000 ); //binding, correctly. if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server))< 0) { //error } printf("bind done"); //listen listen(socket_desc , 3); printf("waiting incoming connections..."); c = sizeof(struct sockaddr_in); while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) ) { puts("connection accepted"); pthread_t sniffer_thread; new_sock = (int*) malloc(1); *new_sock = client_sock; if( pthread_create( &sniffer_thread , null , connection_handler , (void*) new_sock) < 0) { //error } printf("handler assigned"); } if (client_sock < 0) { //error } //return 0; } /* * handle connection each client may have , read indefinitely * */ void *connection_handler(void *socket_desc) { //get socket descriptor int sock = *(int*)socket_desc; int read_size; char *message , client_message[2000]; string smatrix ; int ind ; string tok ; int = 0 ; //receive message client while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 ) { //what want client's message } if(read_size == 0) { printf("client disconnected"); fflush(stdout); } else if(read_size == -1) { //error } //free socket pointer free(socket_desc);
as client, while loop until time has passed or given character or value entered. like:
while(wanttoexit == false){ char a; printf("please enter char"); scanf("%c", &a); write(sock , , 1); //note use strlen() strings }
hope helps
Comments
Post a Comment