Error “Attempted to access an unnamed file past its end.” when data receive over sock
I have an MFC server which is listening on a port all the time. Now my requirement is to send data over the socket(using TCP/IP) to this server from a client written in python 3.6.
The python client sends the bytes, but once I receive data at the server end, the exception is thrown "Attempted to access an unnamed file past its end."
//Server code snippet
Code:
void CClientSocket::ReceiveMsg(CMsg* pMsg)
{
pMsg->Serialize(*m_pArchiveIn);
}
void CMsg::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << (WORD)bClose;
ar << m_strText;
}
else
{
WORD wd;
ar >> wd;
bClose = (BOOL)wd;
ar >> m_strText;
}
m_msgList.Serialize(ar);
}
//Client code
Code:
'''
Simple socket client
'''
import socket
import sys
def testme():
PlayerHost = "127.0.0.1"
PlayerPort = 501
try:
print ("Connecting --- to server" + PlayerHost)
playerSoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
playerSoc.connect((PlayerHost,PlayerPort))
except e:
print ("Connectiong error")
print(e)
try:
dataSendCommand = '$Command Command 0 100';
print ('---Before encode Command' + dataSendCommand);
#print ('After encode command' + dataSendCommand.encode());
nbytes = playerSoc.send(dataSendCommand);
print(nbytes);
playerSoc.close();
except e:
print ("Some issue")
testme()
exit(0)
I am not much aware of how to MFC sockets are working with CArchive and what exactly happens in there. Also, if I send message from C++ client, it works fine. Any help on how to send data from python to MFC C++ server which is compatible so that receive is successful is what I want know. Your help appreciated.
I have the following in MFC C++ which I want to replicate in python but I am not sure on the datatype to be used.
Code:
USES_CONVERSION;
char buf[1024];
int nBufLen1;
CString sendBuffer("$MyCommand Hi");
//int nBufLen1 = sendBuffer.GetLength();
buf[0] = 0;
buf[1] = 0;
WORD len = sendBuffer.GetLength() * 2;
nBufLen1 = len + 5; // length of message
buf[2] = (BYTE)len;
char *pStr = T2A(sendBuffer);
strncpy(&buf[3],pStr,len); // actual string to send
buf[len + 3] = 0;
buf[len + 4] = 0;
if(sock.Send (&buf,nBufLen1) != nBufLen1)
{
OutputDebugString(L"Socket Send Failed");
return FALSE;
}
Can anyone help me with the datatype ?
Since I am new to python here is what i tried
Code:
bufArr2 = ["0" , "0" , "26" , "$MyCommand Hi" , "0" , "0"]
print(bufArr2[3]);
nbytes = playerSoc.send(bufArr2.encode()); //Error
I received error "descriptor 'encode' requires a 'str' object but received a 'list'"
Not sure on how to send the list as bytes. Also, not sure if there is something else that I can use instead of list.
Megha
Re: Error “Attempted to access an unnamed file past its end.” when data receive over