如题,代码如下:
android上的客户端,忽略了Activity部分
public class LocationClient {
private final String remoteIP;//外网IP
private final int PORT = 28888;
private final int PACKET_SIZE = 4096;
private DatagramSocket dSocket;
private DatagramPacket inPacket;
private DatagramPacket outPacket;
public LocationClient(String remoteIP) {
super();
this.remoteIP = remoteIP;
try {
dSocket = new DatagramSocket();
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
inPacket = new DatagramPacket(new byte[PACKET_SIZE], PACKET_SIZE);
try {
//处理remoteIP地址字符串
String[] remoteIPSplit = remoteIP.split("\\.");
byte[] remoteIPBuf = new byte[4];
for(int i = 0; i < 4; i++){
remoteIPBuf[i] = (byte)(Integer.parseInt(remoteIPSplit[i])&0xff);
}
outPacket = new DatagramPacket(new byte[PACKET_SIZE], 0,
PACKET_SIZE, InetAddress.getByAddress(remoteIPBuf), PORT);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 发送数据 MAX长度 = 4k
*/
public void sendData(String content) {
outPacket.setData(content.getBytes());
try {
dSocket.send(outPacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void close() {
dSocket.close();
}
}
PC机上的服务器:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.swing.filechooser.FileNameExtensionFilter;
public class LocationServer {
private final int PORT = 28888;
private final int PACKET_SIZE = 4096;
private DatagramSocket dSocket;
private DatagramPacket inPacket;
private DatagramPacket outPacket;
public LocationServer() {
super();
try {
dSocket = new DatagramSocket(PORT);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
inPacket = new DatagramPacket(new byte[PACKET_SIZE], PACKET_SIZE);
}
public void run(){
while(true){
try {
System.out.println("~~~~~服务器启动~~~~~");
dSocket.receive(inPacket);
byte[] content = inPacket.getData();
System.out.println(String.valueOf(content));
new RecordManager(inPacket.getAddress().getHostAddress(), content).run();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
dSocket.close();
}
}
}
private class RecordManager implements Runnable{
private String fileName;
private byte[] addContent;
public RecordManager(String fileName, byte[] addContent) {
super();
this.fileName = fileName;
this.addContent = addContent;
}
@Override
public void run() {
// TODO Auto-generated method stub
File file = new File(fileName);
try(RandomAccessFile rAccessFile = new RandomAccessFile(file, "w")){
//指针跳到末尾
rAccessFile.seek(file.length());
//追加内容
rAccessFile.write(addContent);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args){
new LocationServer().run();
}
}