multithreading - Multiple client and a server communication -
i novice client server programming, how understood things , trying communicate between client , server. code buggy not dislaying entire string, can 1 tell me how send entire string(which has spaces) server client.
output:
rhu>> confgiure cm port (this entire string) server reply=configure
rhu>> server reply=cm rhu>> server reply=port
the above entire thing output 1 string configure cm port
and 1 more things there way catch these arguments in argv[] above case. here server code.
//create socket socket_desc = socket(af_inet , sock_stream , 0); if (socket_desc == -1) { printf("could not create socket"); } puts("socket created"); //prepare sockaddr_in structure server.sin_family = af_inet; server.sin_addr.s_addr = inaddr_any; server.sin_port = htons( 8888 ); //bind if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) { //print error message perror("bind failed. error"); return 1; } puts("bind done"); //listen listen(socket_desc , 3); //accept , incoming connection puts("waiting incoming connections..."); c = sizeof(struct sockaddr_in); //accept , incoming connection puts("waiting incoming connections..."); c = sizeof(struct sockaddr_in); pthread_t thread_id; while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) ) { puts("connection accepted"); if( pthread_create( &thread_id , null , connection_handler , (void*) &client_sock) < 0) { perror("could not create thread"); return 1; } //now join thread , dont terminate before thread //pthread_join( thread_id , null); puts("handler assigned"); } if (client_sock < 0) { perror("accept failed"); return 1; } return 0;
}
//void connectionhandler(void *sock_desc) { //write(sock , message , strlen(message)); //message = "now type , shall repeat type \n"; //write(sock , message , strlen(message)); //receive message client while( (read_size = recv(sock , client_message , 2000, 0)) > 0) { client_message[read_size] = '\0'; write(sock, client_message, strlen(client_message)); memset(client_message, 0, 2000); } if(read_size == 0) { puts("client disconnected"); fflush(stdout); } else if(read_size == -1) { perror("recv failed"); } return 0;
}
client code
//create socket sock = socket(af_inet , sock_stream , 0); if (sock == -1) { printf("could not create socket"); } //puts("socket created"); server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_family = af_inet; server.sin_port = htons( 8888 ); //connect remote server if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) { perror("connect failed. error"); return 1; } //puts("connected\n"); printf("!!welcome rhu cli!!\n"); //keep communicating server while(1) { fflush(stdout); printf("rhu>> : "); scanf("%s" , message); //send data if( send(sock , message , strlen(message) , 0) < 0) { puts("send failed"); return 1; } //receive reply server if( recv(sock , server_reply , 2000 , 0) < 0) { puts("recv failed"); break; } printf("server reply=%s\n", server_reply); //if(strcmp(server_reply, "?") == 0) //puts("confgiure"); parse_init(server_reply); memset(server_reply, 0, 2000); fflush(stdout); } close(sock); return 0;
}
Comments
Post a Comment