By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,897 Members | 1,490 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,897 IT Pros & Developers. It's quick & easy.

Writing into a file

P: 62
hi,
i am doing socket programming , and once socket is connected getting data from client and wrting into the file.
the file is created but nothing is in it? whats up ?
code:
Expand|Select|Wrap|Line Numbers
  1.  WORD wVersionRequested;
  2. int err;
  3.  
  4.  WSADATA wsaData;
  5.  SOCKET m_socket;
  6.  SOCKET AcceptSocket;
  7. SOCKADDR_IN  service;
  8. HANDLE hFile;
  9. DWORD  dwBytesRead, dwBytesWritten;
  10.  
  11.  #define MY_MESSAGE_NOTIFICATION      1048 //Custom notification message
  12.  
  13.  int bytesSent;
  14.  int bytesRecv = SOCKET_ERROR;
  15.  char sendbuf[32] = "Sever sending data";
  16.  char recvbuf[32] = "";
  17.  
  18.  
  19.  //sockaddr_in server;
  20.  
  21.   int iResult = WSAStartup( 0X0202, &wsaData );
  22.   if ( iResult != NO_ERROR )
  23.   {
  24.   printf("Error at WSAStartup()\n");
  25.   return(0);
  26.   }
  27.  
  28.  
  29.  
  30.   m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP);
  31.  
  32. if ( m_socket == INVALID_SOCKET )
  33.     {
  34.     printf( "Error at socket(): %ld\n", WSAGetLastError() );
  35.     WSACleanup();
  36.     return(0);
  37.     }
  38.  
  39. service.sin_family = AF_INET;
  40. service.sin_addr.s_addr = inet_addr( "127.0.0.1");
  41. service.sin_port = htons(27015);
  42.  
  43. if (  bind( m_socket,  (SOCKADDR *)&service, sizeof(service) ) == SOCKET_ERROR ) 
  44. {
  45.     printf( "bind() failed.\n  %ld\n", WSAGetLastError() );
  46.   closesocket(m_socket);
  47.    return(0);
  48. }
  49. if ( listen( m_socket, 1 ) == SOCKET_ERROR )
  50.     printf( "Error listening on socket. %ld \n",WSAGetLastError()  );
  51.  
  52.   //creating a object of  socket for accepting the connection
  53.  
  54.  
  55. printf( "Waiting for a client to connect...\n" );
  56. while (1) 
  57. {
  58.     AcceptSocket = SOCKET_ERROR;
  59.     while ( AcceptSocket == SOCKET_ERROR )
  60.     {
  61.         AcceptSocket = accept( m_socket, NULL, NULL );
  62.      }
  63.   printf( "Client Connected.\n");
  64.     m_socket = AcceptSocket;  
  65.     break;
  66. }
  67.  
  68.  
  69.  
  70.   bytesRecv = recv( m_socket, recvbuf, 32, 0 );
  71.   printf( "Bytes Recv: %ld\n", bytesRecv );
  72.   bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
  73.   printf( "Bytes Sent: %ld\n", bytesSent );
  74.  
  75.  
  76. hFile = CreateFile(TEXT("myfile.xls"),     // file to create
  77.                      GENERIC_WRITE | GENERIC_READ ,// open for writing &reading
  78.                      0,                      // do not share
  79.                      FILE_SHARE_WRITE,                   // default security
  80.                      OPEN_EXISTING,          // overwrite existing
  81.                      FILE_ATTRIBUTE_NORMAL | // normal file
  82.                      FILE_FLAG_OVERLAPPED,   // asynchronous I/O
  83.                      NULL);                  // no attr. template
  84.  
  85. if (hFile == INVALID_HANDLE_VALUE) 
  86.   printf("Could not open file (error )%ld\n", GetLastError());
  87.  
  88. }
  89.  
  90.  
  91. do {
  92.            WriteFile(hFile , recvbuf, 512, 
  93.                 &dwBytesWritten, NULL); 
  94.     }while(dwBytesWritten == 32);
  95.  
  96.      GetLastError();
  97.  
  98. CloseHandle(hFile);
  99. GetLastError();
  100.   return(0);
Jul 11 '07 #1
Share this Question
Share on Google+
5 Replies


kky2k
P: 34
are u sure the file is created?..if yes then where it is created i mean which path...
Jul 11 '07 #2

P: 62
the file is being created in the folder where the project codes (.c & .cpp) files are there in visual studio projects.
Jul 11 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
do {
WriteFile(hFile , recvbuf, 512,
&dwBytesWritten, NULL);
}while(dwBytesWritten == 32);
Do I read this correctly? You ask to write 512 bytes and then check that 32 bytes are written? It looks like dwBytesWritten won't be 32 after the first write so you drop out of the loop.
Jul 11 '07 #4

P: 62
Do i need to allocate memory before writing into the file and is it that the no of bytes written should be in multiples of 512. And the file creation parameters are they fine? will they work for writing once to a file and closing to it.
Jul 12 '07 #5

P: 62
What the moderator said was true and also check up with the "dwflagsandatrributes" parameter in the create file. its only File_Atrribute_normal. And also the do while loop make it write the no of times. So do while loop is also not needed. Else is working fine.
thanks for the help.
Jul 12 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.