Guest User

Untitled

a guest
Oct 19th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. /*
  2. // -- threaded client with ncurses iu, by dono (2011)
  3. */
  4.  
  5. // compile: "gcc file.c -o file -l ncurses -l pthread"
  6.  
  7. // standard includes
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. // socket
  13. #include <unistd.h>
  14. #include <sys/socket.h>
  15. #include <sys/types.h>
  16. #include <netinet/in.h>
  17. #include <arpa/inet.h>
  18. #include <netdb.h>
  19.  
  20. // threads
  21. #include <pthread.h>
  22.  
  23. // ncurses
  24. #include <ncurses.h>
  25.  
  26. #define MAX 1024
  27. #define MIN 128
  28.  
  29. // prototypes
  30. void *recv_thread_function(void *parameter);
  31. void *send_thread_function(void *parameter);
  32. WINDOW *create_window(int height, int width, int start_y, int start_x);
  33.  
  34. // global variables
  35. char prompt[MAX];
  36. char in_buffer[MAX];
  37. char out_buffer[MAX];
  38. char user_input[MAX];
  39. int result, loop = 1;
  40. int max_height, max_width;
  41. int body_row = 1;
  42. pthread_t recv_thread, send_thread;
  43.  
  44. // global ncurses variables
  45. WINDOW *header_window;
  46. WINDOW *body_window;
  47. WINDOW *input_window;
  48.  
  49. // global socket variables
  50. int sock, server_port;
  51. struct sockaddr_in server_addr;
  52. struct hostent *server;
  53.  
  54. int main(int argc, char *argv[])
  55. {
  56. // check for correct number of args
  57. if(argc != 4)
  58. {
  59. printf("usage: %s [server] [port] [handle]\n", argv[0]);
  60. exit(1);
  61. }
  62.  
  63. // assign address, and port; get hostname
  64. server = gethostbyname(argv[1]);
  65. server_port = atoi(argv[2]);
  66.  
  67. // set up remote server address structure
  68. memset(&server_addr, 0, sizeof(server_addr));
  69. server_addr.sin_family = AF_INET;
  70. bcopy((char *)server->h_addr, (char *) &server_addr.sin_addr.s_addr, server->h_length);
  71. server_addr.sin_port = htons(server_port);
  72.  
  73. // create client socket
  74. sock = socket(AF_INET, SOCK_STREAM, 0);
  75.  
  76. // connect to server socket
  77. connect(sock, (struct sockaddr *) &server_addr, sizeof(server_addr));
  78.  
  79. printf("Connecting to %s on port %d\n", argv[1], server_port);
  80.  
  81. // display server greeting
  82. memset(in_buffer, 0, MAX);
  83. recv(sock, in_buffer, sizeof(in_buffer), 0);
  84. if((strcmp(in_buffer, "Welcome to the server!")) == 0)
  85. {
  86. printf("%s\n", in_buffer);
  87. }
  88. else
  89. {
  90. printf("No server answering!\n");
  91. close(sock);
  92. exit(1);
  93. }
  94.  
  95. // initialize ncurses
  96. initscr();
  97. getmaxyx(stdscr, max_height, max_width);
  98.  
  99. // create windows
  100. header_window = create_window(3, max_width, 0, 0);
  101. sprintf(in_buffer, "%s\t\t[^C or \'/exit\'to quit]", in_buffer);
  102. mvwprintw(header_window, 1, 2, in_buffer);
  103.  
  104. body_window = create_window((max_height/3)*2, max_width, 3, 0);
  105.  
  106. input_window = create_window(3, max_width, ((max_height/3)*2)+3, 0);
  107.  
  108. wrefresh(header_window);
  109. wrefresh(body_window);
  110. wrefresh(input_window);
  111.  
  112. int return_recv = pthread_create(&recv_thread, 0, recv_thread_function, 0);
  113. int return_send = pthread_create(&send_thread, 0, send_thread_function, (void *)argv[3]);
  114.  
  115. if(return_recv != 0)
  116. {
  117. printf("Creating thread \'recv_thread\' failed!\n");
  118. return 1;
  119. }
  120.  
  121. if(return_send != 0)
  122. {
  123. printf("Creating thread \'send_thread\' failed!\n");
  124. return 1;
  125. }
  126.  
  127. // close threads
  128. pthread_join(recv_thread, 0);
  129. pthread_join(send_thread, 0);
  130.  
  131. // clean up socket
  132. printf("Closing socket\n");
  133. close(sock);
  134.  
  135. return 0;
  136. }
  137.  
  138. void *recv_thread_function(void *parameter)
  139. {
  140. while(loop == 1)
  141. {
  142. memset(in_buffer, 0, MAX);
  143. recv(sock, in_buffer, sizeof(in_buffer), 0);
  144.  
  145. if(body_row < ((max_height/3)*2)-2)
  146. {
  147. mvwaddnstr(body_window, body_row, 2, in_buffer, sizeof(in_buffer));
  148. mvwprintw(input_window, 1, 2, prompt);
  149. wrefresh(body_window);
  150. wrefresh(input_window);
  151. body_row++;
  152. }
  153. else
  154. {
  155. int i;
  156.  
  157. for(i = 0; i < ((max_height/3)*2)-1; i++)
  158. {
  159. wmove(body_window, i, 0);
  160. wclrtoeol(body_window);
  161. }
  162. body_row = 1;
  163. }
  164. }
  165. }
  166.  
  167. void *send_thread_function(void *parameter)
  168. {
  169. char *handle = (char *)parameter;
  170.  
  171. while(loop == 1)
  172. {
  173. sprintf(prompt, "[%s]: ", handle);
  174. mvwprintw(input_window, 1, 2, prompt);
  175. wrefresh(input_window);
  176. wgetstr(input_window, user_input);
  177.  
  178. if((strcmp(user_input, "/exit")) == 0)
  179. {
  180. loop = 0;
  181. delwin(header_window);
  182. delwin(body_window);
  183. delwin(input_window);
  184. refresh();
  185. endwin();
  186. exit(1);
  187. }
  188. else
  189. {
  190. sprintf(out_buffer, "[%s]: %s\n", handle, user_input);
  191. send(sock, out_buffer, sizeof(out_buffer), 0);
  192. wmove(input_window, 1, 2);
  193. wclrtoeol(input_window);
  194. }
  195. }
  196. }
  197.  
  198. WINDOW *create_window(int height, int width, int start_y, int start_x)
  199. {
  200. WINDOW *local_window;
  201. local_window = newwin(height, width, start_y, start_x);
  202. box(local_window, 0, 0);
  203. wrefresh(local_window);
  204.  
  205. return local_window;
  206. }
Add Comment
Please, Sign In to add comment