The online racing simulator
can't work IS_INI [solved]
(8 posts, started )
can't work IS_INI [solved]
I am making my program for new inSim(W39).

However, my code did not work at "IS_INI".
Always displaying "inSim : guest name must end with zero" after sending "IS_INI" packet.
I'm using this code.


Quote :
IS_ISI is_ini;
IS_TINY is_p;
IS_VER is_v;
WSADATA wsaData;
short listenPort;
listenPort = InSimPort - 1;
if (WSAStartup(0x202, &wsaData) == SOCKET_ERROR)
{
AfxMessageBox("Init : WSAStartup failed");
WSACleanup();
return 0;
}

UDPSocket = socket(AF_INET, SOCK_DGRAM, 0); // create UDP listen socket
if (UDPSocket == INVALID_SOCKET)
{
AfxMessageBox("SetupUDPSocket : Error opening listen socket");
WSACleanup();
return 0;
}
// setup UDP send socket
UDPSockAddr.sin_family = AF_INET;
UDPSockAddr.sin_addr.s_addr = INADDR_ANY;
// Port MUST be in Network Byte Order
UDPSockAddr.sin_port = htons(listenPort); // any port (10000 to 65535)
// bind
if (bind(UDPSocket,(sockaddr *)&UDPSockAddr, sizeof(UDPSockAddr)) == SOCKET_ERROR)
{
AfxMessageBox("SetupUDPSocket : bind failed");
closesocket(UDPSocket);
UDPSocket = INVALID_SOCKET;
WSACleanup();
return 0;
}

is_ini.Size = 44;
is_ini.Type = ISP_ISI;
is_ini.UDPPort = listenPort;
is_ini.Flags = 0;
is_ini.Interval = 0;
is_ini.ReqI = 1;
is_ini.Zero = 0;
is_ini.Sp0 = 0;
strcpy(is_ini.Admin,"");
strcpy(is_ini.IName,"MyProgram");
unsigned int addr = inet_addr(connAddr);
memset(&temp_addr, 0, sizeof(temp_addr));
memcpy(&temp_addr.sin_addr, &addr, sizeof(addr));
temp_addr.sin_family = AF_INET;
temp_addr.sin_port = htons(InSimPort);
if(sendto(UDPSocket, (char *)&is_ini, sizeof(is_ini), 0, (sockaddr *)&temp_addr, sizeof(temp_addr)) == SOCKET_ERROR){
AfxMessageBox("Sendto : send error ISI\n");
closesocket(UDPSocket);
UDPSocket = INVALID_SOCKET;
WSACleanup();
ConnectFlag = false;
return 0;
}
if(RecvWait((char *)&is_v, sizeof(is_v)) == 1){
AfxMessageBox("Can not received IS_VER packet\n",MB_ICONSTOP);
closesocket(UDPSocket);
UDPSocket = INVALID_SOCKET;
WSACleanup();
ConnectFlag = false;
return 0;
}


int myProgram::RecvWait(char *buf, int len)
{
HANDLE hEvent = WSACreateEvent();
WSANETWORKEVENTS events;
int ret,retVal,recvSize;
WSAEventSelect(UDPSocket,hEvent,FD_READ | FD_CLOSE);
while(1) {
ret = WSAWaitForMultipleEvents(1,&hEvent,FALSE,5000,FALSE);
if(ret == WSA_WAIT_TIMEOUT) {
retVal = 1;
break;
}
if(WSAEnumNetworkEvents(UDPSocket,hEvent,&events) == SOCKET_ERROR) {
retVal = 1;
break;
} else {
if(events.lNetworkEvents & FD_READ) {
recvSize = recv(UDPSocket,buf,len,0);
retVal = 0;
break;
}
}
}
WSACloseEvent(hEvent);
return retVal;
}


But did not work...

Please help me.
Sorry, solved this problem.
I added this line.

memset(&is_ini, 0, sizeof(is_ini));

It's working after adding this line.
Thanks - yes, I think that is good advice for most InSim packets - memset to zero before filling the relevant data.
Quote from MoHaX :Small note: there is an err in documentation: prefix field of IS_INI packet is char, not byte. It's not a problem on C or C++, but it could be a problem on other languages.

Read the Insim documentation;
// TYPES : (all multi-byte types are PC style - lowest byte first)
// =====

// char 1-byte character
// byte 1-byte unsigned integer
// word 2-byte unsigned integer
// short 2-byte signed integer
// unsigned 4-byte unsigned integer
// int 4-byte signed integerz
// float 4-byte float
// MSHT 4-byte minutes, seconds, hundredths, thousandths

Quote from MoHaX :I've read it many times. Do you mean that byte and char are the same? Or what are you trying to say by pointing to doc?

Just because a datatype doesn't exist in a language doesn't mean how it's defined isn't correct.

The header states that a byte should be interpreted as a char, and it's up to you as a coder of that language you have chosen to make that into a relevant datatype for your language.

"byte" isn't valid in a multitude of languages - but it's still relevant to call it a byte as we know we should treat it as an (unsigned) char.

For example, as a C programmer (using a non-microsoft compiler or library) you are expected to understand this and either do one of the following;
typedef unsigned char byte;
#DEFINE BYTE char
Quote from MoHaX :Then why are you against description of prefix field as "char" in documentation?

I didn't say I was against it?
I don't really want to change it. At the moment, it allows characters higher than 127 value to be used as a prefix. I'm not sure if it would go wrong if I changed it to char, and I don't want to get into testing that.

If there is a serious issue, like for some reason, a character cannot be converted into an unsigned char, to write to this byte, then let me know. But I guess in most cases the comparison issue can be solved.

For one thing, you could just change that field to char, if you are using InSim.txt as a header file, that will still work. This file is a real header file used in LFS, that's why I can't just change it.

can't work IS_INI [solved]
(8 posts, started )
FGED GREDG RDFGDR GSFDG