开发者

Using strange format for sending dat through a socket

开发者 https://www.devze.com 2023-03-07 02:17 出处:网络
I have an app in android which is a kind of client-server. I\'ve established the connection bet开发者_高级运维ween those two and now I\'m sending data through the socket.

I have an app in android which is a kind of client-server.

I've established the connection bet开发者_高级运维ween those two and now I'm sending data through the socket.

The server is the one who sends the data and the client reads it from the socket.

The server sends to socket some GPS data (longitude,latitude) and for really sending it I'm using the following format:

public class Coordinate {

  private final double lon;

  private final double lat;

  private final int workerId;

  public Coordinate(double lat, double lon, int workerId) {

    this.lat = lat;
    this.lon = lon;

    this.workerId=workerId;
  }

  public double getLon() {
    return lon;
  }

  public double getLat() {
    return lat;
  }
  public int getwId(){
      return workerId;
  }


}

So for every longitude and latitude that I wanna send I add a workerID which is an int.

So finally through my socket I'm sending an instance of the Coordinate class.

   Coordinate coord=new Coordinate(lat,lon,workerID);
   os=new PrintStream(clientSocket.getOutputStream());

    os.println(coord);

Well,until here everything goes fine.

On the client side I'm reading the data this way:

Scanner is=new Scanner(new DataInputStream(socket.getInputStream()));

while(is.hasNext())
            {
                try{
                Coordinate coord=is.next();
                    System.out.println(coord.getLon());

                    }
                catch(Exception e){
                    e.printStackTrace();
                }
            }

The problem is that Scanner class that I'm using it for reading from the socket input stream returns String,but what I'm sending through the socket is something completly different......

Does someone knows how could I convert the input data to Coordinate or how should I proceed to find the latitude and longitude from what I'm sending through the socket?????

Thx!

EDIT:I'm using that format because I have my needs with that data!!


os.println(coord) is just writing a reference, which doesn't make much sense, if you want to send something meaningful then the Coordinate class should implement Serializable. Have a look at this article where it is explained.

Optionally I recommend you to dump and load at both ends using the JSON format. It will provide you an easy and readable way to inter-operate.


PrintStream.println(Object) writes a string which it gets from the Object.toString() method of the object (PrintStream in general only writes strings to the outputstream it is connected to)

you can either create a text encoding for Coordinate (with encode and decode functions) which you can use without much alteration to your code

or use the serializable functionality and use the ObjectStreams

0

精彩评论

暂无评论...
验证码 换一张
取 消