Hi Guys,

Just wondering why my console doesn't stay open when I run my program?

Here is the code for it:

Qt Code:
  1. #include <QtCore/QCoreApplication>
  2. #include <winsock2.h>
  3. #include <stdio.h>
  4. #include <QDebug>
  5.  
  6. // #include <netinet/in.h>
  7. // #include <arpa.inet.h>
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. QCoreApplication a(argc, argv);
  12.  
  13. struct sockaddr_in peer; //"struct sockaddr_in" is a structure type and "peer" is a name of the variable. The "struct" word comes from C and it is not needed in C++
  14. int s;
  15. int rc;
  16. char buf[1];
  17.  
  18. peer.sin_family = AF_INET;
  19. peer.sin_port = htons(7500);
  20. qDebug() << peer.sin_port;
  21. peer.sin_addr.s_addr = inet_addr("127.0.0.1");
  22.  
  23. s = socket(AF_INET, SOCK_STREAM, 0);
  24.  
  25. if(s < 0)
  26. {
  27. perror("socket call failed");
  28. exit(0);
  29. }
  30.  
  31. rc = connect(s, (struct sockaddr *)&peer, sizeof(peer));
  32.  
  33. if(rc)
  34. {
  35. perror("connect call failed");
  36. exit(1);
  37. }
  38.  
  39. rc = send(s, "1", 1, 0);
  40.  
  41. if(rc <= 0)
  42. {
  43. perror("send call failed");
  44. exit(1);
  45. }
  46.  
  47. rc = recv(s, buf, 1, 0);
  48. if(rc <= 0)
  49. perror("recv call failed");
  50. else
  51. printf("%c\n", buf[0]);
  52. exit(0);
  53.  
  54. return a.exec();
  55. }
To copy to clipboard, switch view to plain text mode