CSocket problem
-
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
-
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
(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 thatCSocket::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