我在我的手机(Android)和我的服务器之间做了一个简单的套接字。
手机可以连接到服务器,但我可以互相发送或读取数据。
这是我用于客户端(电话端)的代码:
private static void connect(String url, int port)
{
do
{
try
{
socket = new Socket(url, port);
input = socket.getInputStream();
output = socket.getOutputStream();
inputstream = new DataInputStream(input);
outputstream = new DataOutputStream(output);
isConnected = true;
}
catch (Exception e)
{
try
{
Thread.sleep(100);
}
catch (Exception ex) {}
}
}
while (!isConnected);
}
public static String sendToServer(String json)
{
try
{
connect(Constants.SERVER_HOST, Constants.SERVER_PORT);
if (isConnected)
{
Log.e("SOCKET MNGR", "[SOCKET] Sending " + json);
outputstream.writeInt(json.getBytes().length);
outputstream.write(json.getBytes());
int arrLength = inputstream.readInt();
if (arrLength > 0)
{
byte[] fromServer = new byte[arrLength];
inputstream.read(fromServer, 0, fromServer.length);
String content = new String( Base64.decode(new String(fromServer)) );
Log.e("SOCKET MNGR", "[SOCKET] Reading " + content);
return content;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
和我的服务器的一个:
public Client(Socket socket)
{
try
{
this.socket = socket;
this.input = this.socket.getInputStream();
this.output = this.socket.getOutputStream();
this.inputstream = new DataInputStream(this.input);
this.outputstream = new DataOutputStream(this.output);
}
catch (Exception e) {}
}
public void run()
{
while (true)
{
try
{
int arrLength = inputstream.readInt();
byte[] fromClient = new byte[arrLength];
inputstream.read(fromClient, 0, fromClient.length);
String request = new String(fromClient);
String read = new String( Base64.decode(request) );
String[] data = request.split("###");
if (data.length == 2 && data[0].equals("commit"))
{
CommitInterface.commit(data[1], true);
outputstream.writeInt(0);
outputstream.write(new byte[] {});
}
else if (data.length == 3 && data[0].equals("evaluate"))
{
EvaluateInterface.eval(data[1], data[2].equals("true") ? true : false);
outputstream.writeInt(0);
outputstream.write(new byte[] {});
}
else if (data.length == 2 && data[0].equals("publish"))
{
String content = PublishInterface.publish(new Vector<String>(Arrays.asList(data[1].split("|"))));
String encoded = Base64.encode(content.getBytes());
outputstream.writeInt(content.getBytes().length);
outputstream.write(content.getBytes());
}
}
catch (Exception e)
{
this.disconnect();
}
}
}
当我sendToServer
在手机上使用该功能时,我得到以下日志:
06-29 13:26:05.823: E/Kramer(1781): [SOCKET] Sending commit###{"user":"me"}
06-29 13:26:05.823: W/System.err(1781): java.io.EOFException
06-29 13:26:05.823: W/System.err(1781): at java.io.DataInputStream.readInt(DataInputStream.java:287)
06-29 13:26:05.823: W/System.err(1781): at com.vodemki.network.KramerCommunicator.sendToServer(ServerCommunicator.java:78)
06-29 13:26:05.823: W/System.err(1781): at com.vodemki.activities.PhoneBookActivity$6.run(PhoneBookActivity.java:298)
第 78 行是int arrLength = inputstream.readInt();
.
知道为什么我会收到此错误吗?
谢谢。
我尝试使用 UDP,但无法从智能手机读取内容。
我的代码如下:
服务器端:
public void run()
{
byte[] inData = new byte[48];
byte[] outData = new byte[48];
String message;
DatagramSocket socket;
try
{
socket = new DatagramSocket(this.port);
while (true)
{
DatagramPacket in = new DatagramPacket(inData, inData.length);
socket.receive(in);
InetAddress senderIP = in.getAddress();
int senderPort = in.getPort();
message = new String(in.getData(), 0, in.getLength());
System.out.println("Got "+message+" from "+senderIP+":"+senderPort);
outData = "Pong".getBytes();
DatagramPacket out = new DatagramPacket(outData, outData.length, senderIP, senderPort);
socket.send(out);
}
}
catch (Exception e) {}
}
客户端(智能手机):
public static String sendToServer(String json)
{
try
{
DatagramSocket socket = new DatagramSocket();
InetAddress serverIP = InetAddress.getByName(Constants.SERVER_HOST);
byte[] outData = ("Ping").getBytes();
DatagramPacket out = new DatagramPacket(outData,outData.length, serverIP, Constants.SERVER_PORT);
Log.e("kramer", "SENDING SOCKET");
socket.send(out);
Log.e("kramer", "SOCKET SENT");
socket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
智能手机成功记录了两条线路,但我的服务器上没有收到任何内容。任何的想法?