tags:

views:

33

answers:

3

Hello all, I want to code a server in java which will accept the information from many devices and store the information in database. The devices will keep sending the packets. The first packet will contain the unique deviceId, and after that only data related to the device. I want to save data along with deviceId. Up to now, I could develop a multithreaded server which can server many clients. But when I get data, I either loose deviceId or gets wrong deviceId. Can anybody help?

Thanks in advance.

A: 

Java is not my best field of coding but i hope these ideas will help.

The database accessing can be done with hibernate or some other OR-Mappe, so you can manipulate your database like an object. For the information in the packets you can send the information when you successfully connected to the server like:

public void connect(string servername)
{
  ConnectionMethod();
  if(connected)
  {
    SendObject(/*Enter information here*/);
  }
}

Then in the receiving thread you could check for a certain class of the incoming message and handle it accordingly, like store it in the database or something like this. For the wrong device ID you have to check if you are in the correct thread when you get the packets

Bastian
A: 

If you want to develop stateless protocol,you must sending ID to every device like HTTP Session and Device must connect with SessionID that Server given.

Artiya4u
A: 

If you are using the one-thread-per-connection pattern, you can keep the deviceId as a field of your thread objects, and pass it to each method it calls, or you can store the deviceid in a ThreadLocal:

private static ThreadLocal<String> deviceId = new ThreadLocal<String>();

public static String getDeviceId() {
    return deviceId.get();
}

public static void setDeviceId(String newValue) {
    deviceId.set(newValue);
}
Maurice Perry