Translate from C to java
i am trying to translate a little code from C to java, but i cant..
please help me..
the first part is:
}; // A genrel Packet
and the second part is :
int rcv = 0;
recv(Socket, (char *)Packet, sizeof(Packet), 0);
if (rcv > 0)
if (Packet.opcode == 0x5000 && Packet.size == 0x25)
std::cout << "First Packet received";
else if (Packet.opcode == 0x5000 && Packet.size == 0x09)
std::cout << "Second Packet received";
else if (Packet.opcode != 0x5000)
pritnf("%.4X %.4X %.2X %.2X", Packet.size, Packet.opcode, Packet.securityCount, Packet.securityCRC);
for(int i = 0; i < Packet.size; i++)
while(rcv != SOCKET_ERROR);
please help me guys :S
thx a lot.
Hello and welcome to the forum. You may have a misunderstanding about what this forum is all about, and I'd like to clarify this for you. For one, this isn't a dump my work and have someone do it for me type forum (for that try RentACoder.com). Instead we'll be happy to try to help you with any problems you may be having with your code and your problems. So let's see your attempts first and tell us of any problems you may be having with your code.
Originally Posted by ighor10
Best of luck.
ok, im sorry for this.
my problem is, in this code, that im trying to translate is:
to do this "struct", i need to do new class,
and to my new class work, i need to set the constructors,
and now, i dont know how to do it, because i have 2 variables of type BYTE, and i dont know what is WORD in java.
if i dont do it, i cant instance a new object from PACKET..
i realy need help to do it.
i am pressed to make this working.
sorry for an inconvenience and for my english :D
The trouble is that the memory layout of a C struct is different than the memory layout of a Java instantiation of a class. Therefore the recv( ... ) call in Java should read all its members (primitives) one by one and it should fill every single member of your Java object one by one, i.e. read ints and bytes and set the Java object members individually. Also the endianess can be a show stopper, depending on the architecture you're running the C code on. Probably a BYTE == byte, WORD == short and DWORD == int.
Too bad Java doesn't have structs. :/
I'm not sure that it needs structs, but perhaps it would interop more easily if it had unsigned numbers -- though I'm certainly no pro in this.
To program (good code) in Java, you need to think different than when u r programming in C. Both languages use different paradigms, so I think is best, if you tell us what ur code is supposed to do, and there may be a different solution, than then one you implemented in C. More adequate for the situation.