fork download
  1. // server
  2. #pragma comment(lib, "ws2_32.lib")
  3. #include <WinSock2.h>
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/types.h>
  9. //#include <sys/socket.h>
  10. //#include <netinet/in.h>
  11. //#include <netdb.h>
  12. #define PORT 12345 /*適当な番号 */
  13.  
  14. int main(int argc, char* argv[])
  15. {
  16. WSADATA wsaData;
  17. char hostname[64];
  18. struct hostent *host;
  19. struct sockaddr_in me;
  20. int s0,s,n;
  21. char buf[512];
  22.  
  23. WSAStartup(MAKEWORD(2, 2), &wsaData);
  24.  
  25. gethostname(hostname,64);
  26. host = gethostbyname(hostname);
  27. // bzero((char *)&me,sizeof(me));
  28. memset(&me, 0, sizeof(me));
  29. me.sin_family = AF_INET;
  30. me.sin_port = htons(PORT);
  31. // me.sin_addr = *(struct in_addr *)host->h_addr;
  32. me.sin_addr.s_addr = INADDR_ANY;
  33. s0 = socket(AF_INET, SOCK_STREAM, 0);
  34.  
  35. if(bind(s0, (const struct sockaddr *)&me, sizeof(me)) < 0){
  36. fprintf(stderr,"cannot bind socket\n");
  37. exit(1);
  38. }
  39.  
  40. listen(s0,1);
  41. s = accept(s0,NULL,NULL);
  42.  
  43. while (fgets(buf, 512, stdin)) {
  44. n = strlen(buf);
  45. send(s, buf, n, 0);
  46. }
  47.  
  48. shutdown(s, SD_BOTH);
  49. closesocket(s0);
  50. closesocket(s);
  51. WSACleanup();
  52. return 0;
  53. }
  54.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty