How to send text file through socket using WinSock2 and fopen_s() in C -
a lot of examples came across uses fopen visual studio 2013 doesn't fopen , suggested fopen_s() instead. got sockets on both server side , client side connect , able send messages strings on socket. send() , recv() uses char buffers fopen_s() uses file type. i'm having trouble making these functions work send server , receive client. program run without runtime errors nothing gets written in output.txt.
sending part of server:
//send data log.txt file *log; char buff[2024]; errno_t err = fopen_s(&log, "c:\\users\\amooc\\desktop\\log.txt", "r+"); if (err <= 0){ puts("can't open \n"); } int bytes = send(new_socket, buff, sizeof(buff), 0); if (bytes <= 0){ fclose(log); puts("sent not successful"); } else{ printf("sent completed %d", err); } /* message = "12345678asdljfa wjr owje fawj ;amfj fwerf9"; int ret = send(new_socket, message, strlen(message), 0); ret = send(new_socket, message, strlen(message), 0); if (ret == socket_error){ printf("socket error %d\n", wsagetlasterror()); } else{ printf("sent\n"); }*/ printf("done\n"); closesocket(s); wsacleanup(); return 0;
receiving part of cilent:
//receive data log.txt , write data output.txt file *log; char buff[2024]; errno_t err = fopen_s(&log, "c:\\users\\amooc\\desktop\\output.txt", "a"); int bytes = recv(s, buff, sizeof(buff), 0); if (bytes <= 0){ fclose(log); puts("receive not successful"); } else{ printf("receive completed %d", bytes); } puts(buff); fclose(log); printf("done\n"); closesocket(s); wsacleanup(); return 0;
Comments
Post a Comment