Hi all,
i construct a program that send in java integer numbers (that transfer to byte array) over a datagram socket.
at the receive side i did in c/c++ (it has to be in c/c++).
when i send numbers between 1 -127 everything pass fine but when i send numbers large than 127 (for example 128 129 etc...)
i got negative numbers at the c/c++ side.
i also make some test at the java side i convert it to byte array and than convert it back to integer and the wright number has got.
this is my code:
client at java side:
private void sendProcedure() throws Exception {
// TODO Auto-generated method stub
DatagramSocket serverSocket = new DatagramSocket();
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
String IpDest ="127.0.0.1";
int getVal= convertStringToInt(jTextField.getText());
InetAddress IPAddress = InetAddress.getByName(IpDest);// receivePacket.getAddress();
int port = 12345;//receivePacket.getPort();
sendData =intToByteArray(getVal);// capitalizedSentence.getBytes();
sendData= reverseArray(sendData);
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
public static byte[] intToByteArray(int intValue) {
ByteBuffer b = ByteBuffer.allocate(4);
//b.order(ByteOrder..BIG_ENDIAN); // optional, the initial order of a byte buffer is always BIG_ENDIAN.
b.putInt(intValue);
byte[] result = b.array();
//for test
int testnum= bytesToInt(result);
System.out.println("reverse value is="+testnum);
return result;
}
private static int bytesToInt(byte[] intBytes){
ByteBuffer bb = ByteBuffer.wrap(intBytes);
return bb.getInt();
}
public byte[] reverseArray(byte[] arr)
{
int left = 0;
int right = arr.length - 1;
while (left < right) {
byte temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
return arr;
}
Server Side at c/c++:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define BUFLEN 512
#define NPACK 100
#define PORT 12345
typedef char byte;
void diep(char *s)
{
perror(s);
exit(1);
}
int convertToNUms()
{
return 1;
}
uint GetInt32( byte *pBytes )
{
uint i0= (uint)(*(pBytes + 3)) << 24;
uint i1= (uint)(*(pBytes + 2)) << 16;
uint i2= (uint)(*(pBytes + 1)) << 8;
uint i3= (uint)(*(pBytes + 0));
uint res= i0|i1|i2|i3;
printf("i0=%d\n",i0);
printf("i1=%d\n",i1);
printf("i2=%d\n",i2);
printf("i3=%d\n",i3);
printf("Res=%d\n",res);
return (uint)(*(pBytes + 3) << 24 | *(pBytes + 2) << 16 | *(pBytes + 1) << 8 | *pBytes);
}
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i, slen=sizeof(si_other);
char buf[5];
int var1;
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, &si_me, sizeof(si_me))==-1)
diep("bind");
for (i=0; i<NPACK; i++) {
if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
diep("recvfrom()");
var1 = GetInt32(buf);//here I have to do casting.
printf("Received packet from %s:%d\nData: %d\n\n",
inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), var1);
}
close(s);
return 0;
}
THanks for the attentaion