Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <signal.h>
  8. #include <sys/wait.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <arpa/inet.h>
  13. #include <netdb.h>
  14.  
  15. int main(int argc, char **argv) {
  16. int z, len_inet;
  17. struct sockaddr_in adr_srvr;
  18. int sockfd;
  19. int i;
  20. char buf[256];
  21. int buffer[256];
  22.  
  23. memset(&adr_srvr, 0, sizeof(adr_srvr));
  24.  
  25. if (argc ==2)
  26. adr_srvr.sin_addr.s_addr = inet_addr(argv[1]);
  27.  
  28.  
  29.  
  30. adr_srvr.sin_family = AF_INET;
  31. adr_srvr.sin_port = htons(9090);
  32.  
  33. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  34. if (sockfd == -1) {
  35. perror("socket error");
  36. exit(1);
  37. }
  38.  
  39. z = connect(sockfd, (struct sockaddr *)&adr_srvr, sizeof(adr_srvr));
  40. if(z == -1) {
  41. perror("connect error");
  42. exit(1);
  43. }
  44. while(1){
  45. // printf("here");
  46. //接受SERVER傳送HELLO
  47. read(sockfd, buf, 256);
  48. printf("%s\n",buf);
  49. //告知輸入答案
  50. read(sockfd, buf, 256);
  51. printf("%s\n",buf);
  52.  
  53. while (1) {
  54. //讀取輸入
  55. scanf("%d",&buffer[0]);
  56. //傳給SERVER
  57. write(sockfd, buffer, 256);
  58. //讀取結果
  59. read(sockfd, buf, 256);
  60. printf("%s\n",buf);
  61. if(strlen(buf)==6){
  62. //如果答案是BINGO,就輸出最小的測測者次數(WINNER)
  63. read(sockfd, buf, 256);
  64. printf("%s\n",buf);
  65. break;
  66. }
  67. }
  68. }
  69. close(sockfd);
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement