Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <netdb.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include "message.h"
  12.  
  13. #define SERVER "127.0.0.1"
  14. #define SERVPORT 9876
  15.  
  16. #define BUFLEN 1024
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20. int serverPort = SERVPORT;
  21. struct sockaddr_in serveraddr;
  22. char msg_buf[BUFLEN];
  23. char server[255];
  24. char chat_msg[160];
  25. int unread;
  26. int sd = socket(AF_INET, SOCK_STREAM, 0); // socket descriptor
  27. if(sd < 0)
  28. {
  29. perror("Creating socket error");
  30. exit(-1);
  31. }
  32.  
  33. if(argc > 1)
  34. strcpy(server, argv[1]);
  35. else
  36. strcpy(server, SERVER);
  37.  
  38. if (argc > 2)
  39. serverPort = atoi(argv[2]);
  40.  
  41. memset(&serveraddr, 0x00, sizeof(struct sockaddr_in));
  42. serveraddr.sin_family = AF_INET;
  43. serveraddr.sin_port = htons(serverPort);
  44.  
  45. if((serveraddr.sin_addr.s_addr = inet_addr(server)) == (unsigned long)INADDR_NONE)
  46. {
  47. struct hostent *hostp = gethostbyname(server);
  48. if(hostp == (struct hostent *)NULL)
  49. {
  50. printf("404 Host not found \n");
  51. shutdown(sd,0);
  52. exit(-1);
  53. }
  54. memcpy(&serveraddr.sin_addr, hostp->h_addr, sizeof(serveraddr.sin_addr));
  55. }
  56.  
  57. struct msg *message = (struct msg *) msg_buf;
  58. printf("Insert your nickname\n");
  59. fgets(message->join.nickname, 20, stdin);
  60. message->join.nickname[strlen(message->join.nickname)-1] = 0; // remove \n from the end
  61. message->type = JOIN;
  62. message->len = strlen(message->join.nickname)+1+HDR_SIZE;
  63.  
  64. if(connect(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0){
  65. perror("Connection error");
  66. shutdown(sd, 0);
  67. exit(EXIT_FAILURE);
  68. }
  69.  
  70. if(send(sd, message, message->len, 0) == -1){
  71. perror("Socket send error");
  72. exit(EXIT_FAILURE);
  73. }
  74. printf("**Connected, prepend your message with / to chat, write :q to quit\n");
  75. printf("**When it`s your turn insert x and y coordinate with space between them - eg. \"1 0\"\n");
  76. char moves[3][3];
  77. memset(moves,'_',9);
  78.  
  79. while(1){
  80. // handling console input and network socket at the same time without threading
  81. fd_set rfds;
  82. FD_ZERO(&rfds);
  83. FD_SET(STDIN_FILENO, &rfds);
  84. FD_SET(sd, &rfds);
  85. select((STDIN_FILENO > sd ? STDIN_FILENO : sd)+1, &rfds, NULL, NULL, NULL);
  86.  
  87. // handle networking
  88. if(FD_ISSET(sd, &rfds)){
  89. unread = recv(sd, msg_buf, BUFLEN, 0);
  90. message = (struct msg *) msg_buf;
  91. if(unread < 1){
  92. perror("Connection error, exiting");
  93. shutdown(sd, 0);
  94. exit(0);
  95. }
  96.  
  97. while(unread > 0){
  98. switch(message->type){
  99. case CHAT:
  100. printf("[CHAT] %s: %s\n", message->chat.nickname, message->chat.msg);
  101. break;
  102. case MOVE:
  103. // no need to check if < 0, we use unsigned type
  104. if(message->move.x > 2 || message->move.y > 2)
  105. break;
  106. if(message->move.player == CIRCLE)
  107. moves[message->move.x][message->move.y] = 'O';
  108. else
  109. moves[message->move.x][message->move.y] = 'X';
  110. for (int x=0; x<3; x++)
  111. {
  112. for (int y=0; y<3; y++)
  113. {
  114. printf("%c", moves[x][y]);
  115. }
  116. printf("\n");
  117. }
  118. printf("\n");
  119. break;
  120. case MOVE_YOUR_ASS:
  121. if(message->move_your_ass.you == CIRCLE)
  122. printf("YOUR TURN, CIRCLE \n");
  123. else if(message->move_your_ass.you == CROSS)
  124. printf("YOUR TURN, CROSS \n");
  125. break;
  126. case FINISH:
  127. switch(message->finish.result){
  128. case WIN_CIRCLE:
  129. printf("CIRCLE WINS\n");
  130. break;
  131. case WIN_CROSS:
  132. printf("CROSS WINS\n");
  133. break;
  134. case DRAW:
  135. printf("DRAW\n");
  136. break;
  137. case JEDEN_RABIN_POWIE_TAK_DRUGI_RABIN_POWIE_NIE:
  138. printf("JEDEN RABIN POWIE TAK, DRUGI RABIN POWIE NIE\n");
  139. break;
  140. }
  141. shutdown(sd, 0);
  142. exit(EXIT_SUCCESS);
  143. }
  144. // handling of multiple messages in one buffer
  145. int len = message->len;
  146. unread -= len;
  147. message = (struct msg *)(((char*) message) + len);
  148. }
  149. }
  150.  
  151. // handle console input
  152. if(FD_ISSET(STDIN_FILENO, &rfds)){
  153. message = (struct msg *) msg_buf;
  154. fgets(chat_msg, 160, stdin);
  155. if(strlen(chat_msg) == 3 && chat_msg[0]==':' && chat_msg[1]=='q'){
  156. printf("Bye!");
  157. shutdown(sd, 0);
  158. exit(EXIT_SUCCESS);
  159. } else if(chat_msg[0]!='/'){
  160. sscanf(chat_msg,"%d %d",&(message->move.x),&(message->move.y));
  161. message->type = MOVE;
  162. message->len = 3+HDR_SIZE;
  163. if(send(sd, message, message->len, 0) == -1){
  164. perror("Socket send error");
  165. exit(EXIT_FAILURE);
  166. }
  167. } else {
  168. message->type = CHAT;
  169. chat_msg[strlen(chat_msg)-1] = 0; // remove \n from the end
  170. strcpy(message->chat.msg, &(chat_msg[1]));
  171. message->len = strlen(message->chat.msg)+21+HDR_SIZE;
  172. if(send(sd, message, message->len, 0) == -1){
  173. perror("Socket send error");
  174. exit(EXIT_FAILURE);
  175. }
  176. }
  177. }
  178. }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement