对于我正在尝试创建的 mmo,我有一个连接到 java 服务器的 android 客户端。安卓在模拟器上运行,而服务器直接在我的电脑上运行。他们可以正常连接并确认连接,但客户端停在一个奇怪的地方,我不知道为什么。
服务器的 WorkerThread 的 Run 方法:
public void run() {
try {
InputStream input = clientSocket.getInputStream();
OutputStream output = clientSocket.getOutputStream();
String returns="";
String s="";
try{
s= inputStreamToString(input).toString();
}
catch(Exception e)
{}
output.write(("HTTP/1.1 200 OK\n\nWorkerRunnable: " +
this.serverText + " - " +
"").getBytes());
output.close();
input.close();
} catch (IOException e) {
//report exception somewhere.
e.printStackTrace();
}
}
InputStream 字符串构建器:
private static StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (Exception e) {
}
return total;}
客户端的连接方式:
public String sendMessage(String message)
{
try{ clientSocket = new Socket("10.0.2.2", 9000);
String modifiedSentence;
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToServer.writeBytes(message+"\n");
//Stops here
modifiedSentence = inFromServer.readLine();
outToServer.close();
inFromServer.close();
clientSocket.close();
Log.v(modifiedSentence, modifiedSentence);
return modifiedSentence;}
catch(Exception e)
{
return "";}
}
谢谢,任何帮助将不胜感激。
编辑:如果我关闭客户端或服务器,则更新另一个可以接收前者的消息,但如果两者都打开,则它们不能...