Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CSocket problem

CSocket problem

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialsysadmindebuggingquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vladimir Georgiev
    wrote on last edited by
    #1

    Hi, the those are my client and server procedures using CSocket to send a file... When I run the programs onto a single machine, I receive the file without any problems... But when I run them on two different computers, I get a different file... something is changing my buffer that I send via the socket. Can anyone help me? Does anyone know an article explaining how to use CSocketFile and an example source??? RECEIVING PROCEDURE: UINT SendProcThread(LPVOID pParam) { size = 128; CSocket sock; sock.Create(12234); sock.Listen(); CSocket recv; sock.Accept(recv); char num[20]; recv.Receive(num, 20, NULL); length = atoi(num); TRACE ("\n%d - length\n", length); recv.Receive(num, 20, NULL); int f_name_len = atoi(num); TRACE("\n%d - name len\n", f_name_len); char* name; name = (char*) malloc(f_name_len); recv.Receive(name, f_name_len, NULL); strName = name; strName.ReleaseBuffer(f_name_len); TRACE("\n%s - name\n", strName); int l = length/size; char* buf; buf = (char*) malloc(size); try { CFile file; file.Open(strName, CFile::modeCreate | CFile::modeReadWrite, NULL); for (int i=0; iReportError(); e->Delete(); } recv.Close(); sock.Close(); free(name); free(buf); CString msg; msg.Format("%s file with legth %d has arrived!", strName, length); AfxMessageBox(msg); return 0; } SENDING PROC: UINT SendFileThreadProc(LPVOID pParam) { size = 128; char* buf; buf = (char*) malloc(size); CSocket socket; socket.Create(); socket.Connect(m_dest_name, 12234); char num[20]; itoa(length, num, 10); socket.Send(num, 20, NULL); TRACE ("\n%d - length\n", atoi(num)); itoa(m_strName.GetLength(), num, 10); socket.Send(num, 20, NULL); TRACE("\n%d - name len\n", atoi(num)); socket.Send(m_strName, m_strName.GetLength(), NULL); TRACE("\n%s - name\n", m_strName); int l = length/size; try { CFile file; file.Open(m_strFileName, CFile::modeRead, NULL); for (int i=0; i

    J 1 Reply Last reply
    0
    • V Vladimir Georgiev

      Hi, the those are my client and server procedures using CSocket to send a file... When I run the programs onto a single machine, I receive the file without any problems... But when I run them on two different computers, I get a different file... something is changing my buffer that I send via the socket. Can anyone help me? Does anyone know an article explaining how to use CSocketFile and an example source??? RECEIVING PROCEDURE: UINT SendProcThread(LPVOID pParam) { size = 128; CSocket sock; sock.Create(12234); sock.Listen(); CSocket recv; sock.Accept(recv); char num[20]; recv.Receive(num, 20, NULL); length = atoi(num); TRACE ("\n%d - length\n", length); recv.Receive(num, 20, NULL); int f_name_len = atoi(num); TRACE("\n%d - name len\n", f_name_len); char* name; name = (char*) malloc(f_name_len); recv.Receive(name, f_name_len, NULL); strName = name; strName.ReleaseBuffer(f_name_len); TRACE("\n%s - name\n", strName); int l = length/size; char* buf; buf = (char*) malloc(size); try { CFile file; file.Open(strName, CFile::modeCreate | CFile::modeReadWrite, NULL); for (int i=0; iReportError(); e->Delete(); } recv.Close(); sock.Close(); free(name); free(buf); CString msg; msg.Format("%s file with legth %d has arrived!", strName, length); AfxMessageBox(msg); return 0; } SENDING PROC: UINT SendFileThreadProc(LPVOID pParam) { size = 128; char* buf; buf = (char*) malloc(size); CSocket socket; socket.Create(); socket.Connect(m_dest_name, 12234); char num[20]; itoa(length, num, 10); socket.Send(num, 20, NULL); TRACE ("\n%d - length\n", atoi(num)); itoa(m_strName.GetLength(), num, 10); socket.Send(num, 20, NULL); TRACE("\n%d - name len\n", atoi(num)); socket.Send(m_strName, m_strName.GetLength(), NULL); TRACE("\n%s - name\n", m_strName); int l = length/size; try { CFile file; file.Open(m_strFileName, CFile::modeRead, NULL); for (int i=0; i

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      (Please tick on the checkbox "Display this messahe as-is (no HTML)" when submitting code. Otherwise, your '<'s won't appear.) I'd say (though I haven't thoroughly examined your code) that you are implicitly relying on the false assumption that CSocket::Read will read as many bytes as requested. Actually, the only guarantee is that the number of bytes read will be more than zero and not more than requested (unless the connection is closed and no more data is available.) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups