使用奇怪的格式通过套接字发送数据
我在android中有一个应用程序,它是一种客户端服务器。
我已经在两者之间建立了连接,现在我正在通过套接字发送数据。
服务器发送数据,客户端从套接字读取数据。
服务器向套接字发送一些 GPS 数据(经度、纬度),为了真正发送它,我使用以下格式:
public class Cooperative {
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;
}
}
因此,对于我想要发送的每个经度和纬度,我添加一个 int 类型的workerID。
最后,我通过我的套接字发送了一个坐标类的实例。
Coordinate coord=new Coordinate(lat,lon,workerID);
os=new PrintStream(clientSocket.getOutputStream());
os.println(coord);
好吧,直到这里一切都很顺利。
在客户端,我以这种方式读取数据:
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();
}
}
问题是我使用它从套接字输入流读取的 Scanner 类返回 String,但我通过套接字发送的内容完全不同......
有人知道我如何转换输入数据到坐标或者我应该如何继续从我通过套接字发送的内容中查找纬度和经度???
谢谢!
编辑:我使用该格式是因为我需要该数据!
I have an app in android which is a kind of client-server.
I've established the connection between 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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
os.println(coord)
只是写一个引用,这没有多大意义,如果你想发送一些有意义的东西那么Cooperative
类应该实现可序列化
。请查看这篇文章,其中对此进行了解释。或者,我建议您使用 JSON 格式在两端转储和加载。它将为您提供一种简单易读的互操作方式。
os.println(coord)
is just writing a reference, which doesn't make much sense, if you want to send something meaningful then theCoordinate
class should implementSerializable
. 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)
写入从对象的Object.toString()
方法获取的字符串(PrintStream
通常只写入字符串到它所连接的输出流)您可以为坐标创建文本编码(使用编码和解码函数),您可以使用它而无需对代码进行太多更改
,或者使用可序列化功能并使用 ObjectStreams
PrintStream.println(Object)
writes a string which it gets from theObject.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