Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- // -- threaded client with ncurses iu, by dono (2011)
- */
- // compile: "gcc file.c -o file -l ncurses -l pthread"
- // standard includes
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // socket
- #include <unistd.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- // threads
- #include <pthread.h>
- // ncurses
- #include <ncurses.h>
- #define MAX 1024
- #define MIN 128
- // prototypes
- void *recv_thread_function(void *parameter);
- void *send_thread_function(void *parameter);
- WINDOW *create_window(int height, int width, int start_y, int start_x);
- // global variables
- char prompt[MAX];
- char in_buffer[MAX];
- char out_buffer[MAX];
- char user_input[MAX];
- int result, loop = 1;
- int max_height, max_width;
- int body_row = 1;
- pthread_t recv_thread, send_thread;
- // global ncurses variables
- WINDOW *header_window;
- WINDOW *body_window;
- WINDOW *input_window;
- // global socket variables
- int sock, server_port;
- struct sockaddr_in server_addr;
- struct hostent *server;
- int main(int argc, char *argv[])
- {
- // check for correct number of args
- if(argc != 4)
- {
- printf("usage: %s [server] [port] [handle]\n", argv[0]);
- exit(1);
- }
- // assign address, and port; get hostname
- server = gethostbyname(argv[1]);
- server_port = atoi(argv[2]);
- // set up remote server address structure
- memset(&server_addr, 0, sizeof(server_addr));
- server_addr.sin_family = AF_INET;
- bcopy((char *)server->h_addr, (char *) &server_addr.sin_addr.s_addr, server->h_length);
- server_addr.sin_port = htons(server_port);
- // create client socket
- sock = socket(AF_INET, SOCK_STREAM, 0);
- // connect to server socket
- connect(sock, (struct sockaddr *) &server_addr, sizeof(server_addr));
- printf("Connecting to %s on port %d\n", argv[1], server_port);
- // display server greeting
- memset(in_buffer, 0, MAX);
- recv(sock, in_buffer, sizeof(in_buffer), 0);
- if((strcmp(in_buffer, "Welcome to the server!")) == 0)
- {
- printf("%s\n", in_buffer);
- }
- else
- {
- printf("No server answering!\n");
- close(sock);
- exit(1);
- }
- // initialize ncurses
- initscr();
- getmaxyx(stdscr, max_height, max_width);
- // create windows
- header_window = create_window(3, max_width, 0, 0);
- sprintf(in_buffer, "%s\t\t[^C or \'/exit\'to quit]", in_buffer);
- mvwprintw(header_window, 1, 2, in_buffer);
- body_window = create_window((max_height/3)*2, max_width, 3, 0);
- input_window = create_window(3, max_width, ((max_height/3)*2)+3, 0);
- wrefresh(header_window);
- wrefresh(body_window);
- wrefresh(input_window);
- int return_recv = pthread_create(&recv_thread, 0, recv_thread_function, 0);
- int return_send = pthread_create(&send_thread, 0, send_thread_function, (void *)argv[3]);
- if(return_recv != 0)
- {
- printf("Creating thread \'recv_thread\' failed!\n");
- return 1;
- }
- if(return_send != 0)
- {
- printf("Creating thread \'send_thread\' failed!\n");
- return 1;
- }
- // close threads
- pthread_join(recv_thread, 0);
- pthread_join(send_thread, 0);
- // clean up socket
- printf("Closing socket\n");
- close(sock);
- return 0;
- }
- void *recv_thread_function(void *parameter)
- {
- while(loop == 1)
- {
- memset(in_buffer, 0, MAX);
- recv(sock, in_buffer, sizeof(in_buffer), 0);
- if(body_row < ((max_height/3)*2)-2)
- {
- mvwaddnstr(body_window, body_row, 2, in_buffer, sizeof(in_buffer));
- mvwprintw(input_window, 1, 2, prompt);
- wrefresh(body_window);
- wrefresh(input_window);
- body_row++;
- }
- else
- {
- int i;
- for(i = 0; i < ((max_height/3)*2)-1; i++)
- {
- wmove(body_window, i, 0);
- wclrtoeol(body_window);
- }
- body_row = 1;
- }
- }
- }
- void *send_thread_function(void *parameter)
- {
- char *handle = (char *)parameter;
- while(loop == 1)
- {
- sprintf(prompt, "[%s]: ", handle);
- mvwprintw(input_window, 1, 2, prompt);
- wrefresh(input_window);
- wgetstr(input_window, user_input);
- if((strcmp(user_input, "/exit")) == 0)
- {
- loop = 0;
- delwin(header_window);
- delwin(body_window);
- delwin(input_window);
- refresh();
- endwin();
- exit(1);
- }
- else
- {
- sprintf(out_buffer, "[%s]: %s\n", handle, user_input);
- send(sock, out_buffer, sizeof(out_buffer), 0);
- wmove(input_window, 1, 2);
- wclrtoeol(input_window);
- }
- }
- }
- WINDOW *create_window(int height, int width, int start_y, int start_x)
- {
- WINDOW *local_window;
- local_window = newwin(height, width, start_y, start_x);
- box(local_window, 0, 0);
- wrefresh(local_window);
- return local_window;
- }
Add Comment
Please, Sign In to add comment