[英]Android client/Java server socket; android sending but not receiving?
I've been searching for four hours and this is driving me nuts. 我一直在寻找四个小时,这让我疯了。 I'm going to try keeping this short, if you need more information/code ask and I'll edit.
如果你需要更多信息/代码问我会编辑,我会试着保持这个简短。
So I have an Android client that connects to a server using PrintWriter and BufferedReader. 所以我有一个Android客户端,使用PrintWriter和BufferedReader连接到服务器。 The way it works is it starts a new ASyncTask() to load the connection.
它的工作方式是启动一个新的ASyncTask()来加载连接。 When the connection is made, it sends a "Join" message to the server, and then loads a listen thread that has a while loop waiting for UserInput.readLine() != null, and once broken it returns a string that runs a process function that takes the string and does it's action, and reloads the listen task.
建立连接后,它会向服务器发送一条“Join”消息,然后加载一个监听线程,该线程有一个等待UserInput.readLine()!= null的while循环,一旦打破它就会返回一个运行一个进程的字符串获取字符串并执行操作的函数,并重新加载侦听任务。
//Listener thread
class listen extends AsyncTask<Integer, Integer, Void> {
@Override
protected Void doInBackground(Integer... params) {
//Disconnect variable that's only turned true on backpress
if (!disconnect) {
try {
message = Connection.listen(); //socket object
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// async task finished
if (!disconnect) {
say("INCOMMING"); //easy-made function for Toast
input(message);
}
}
}
and in that Connection: 在那个连接中:
public String listen() throws IOException {
String userInput;
while ((userInput = in.readLine()) != null) {
}
return userInput;
}
Now in my server java app, I have a thread that loads up other connection threads into an ArrayList and acts as a headquarters to dispatch messages to all child clients 现在在我的服务器java应用程序中,我有一个线程将其他连接线程加载到ArrayList中,并充当总部将消息分发给所有子客户端
In my connection: 在我的联系中:
while ((inputLine = in.readLine()) != null) {
//Tells HQ to process string, with id being who it's coming from
hq.Process(id, inputLine);
if (!connected)
break;
}
in HQ object: 在HQ对象中:
public void Process(int id, String str) {
String[] msg = str.split(","); //split message
String send = " "; //string I print to console
if (msg[0].equals("join")) {
send = msg[1] + " has joined!";
parent.seats[cnew.get(id).seat] = id;
cnew.get(id).sendData();
System.out.println(id);
}
And after join, the HQ tells that connection to send that player's information to the phone 加入后,HQ告诉该连接将该播放器的信息发送到手机
public void sendData() {
out.println("chips," + chips); // Update chip count
//give player his cards
out.println("card," + hq.parent.gameCards.getCard(10) + ","
+ hq.parent.gameCards.getCard(11));
//a cry for help to get some output on the phone
out.println("error,SAY THIS");
// out.flush(); //commented out because it didn't help at all
System.out.println("sending id " + id); //debug checker (ignore this)
}
My problem is, it worked when I connected four phones and they all sent toasts to each other. 我的问题是,当我连接四部手机并且他们都互相发送祝酒时,它有效。 But as soon as I changed it to send back data as soon as the player joins, I'm not getting a response in Android at all.
但是一旦我改变它以便在玩家加入后立即发回数据,我根本就没有得到Android的响应。
I can't figure out why it's not working. 我无法弄清楚为什么它不起作用。 On server side, it's going through everything (Checked with system.prints).
在服务器端,它正在通过所有内容(使用system.prints检查)。 The connection IS made, and when I click buttons on the phone the Server is outputting it's responses.
建立了连接,当我点击手机上的按钮时,服务器正在输出它的响应。 But the phone is not receiving anything -- I still don't know if the server is failing to send or the phone is failing to read.
但手机没有收到任何东西 - 我仍然不知道服务器是否无法发送或手机无法读取。 Can you see anything in the code that may be causing this?
您能在代码中看到可能导致此问题的任何内容吗? Need to see more?
需要看更多? Or have any tips on how to debug the connection status?
或者有关于如何调试连接状态的任何提示? The listen() task is never finishing it's execution anymore.
listen()任务永远不会完成它的执行。
UPDATE: So I figured out it's probably to do with my while() loop on android side, doh, probably never breaking. 更新:所以我发现它可能与我在android端的while()循环有关,doh,可能永远不会破坏。 Stupid mistake.
愚蠢的错误。 But I tried to add this as a tester, and still nothing:
但我试图将其添加为测试人员,但仍然没有:
public String listen() throws IOException {
String userInput;
while ((userInput = in.readLine()) != null) {
if (userInput.length() > 2)
break;
}
return userInput;
}
UPDATE: Next desperate update - When I hit "back" (which sends quit msg to server that closes connection, and calls out.close and the rest.close) then I get a never ending loop of "MSG" Toast's -- The Toast that I put when an input is recognized. 更新:下一次绝望的更新 - 当我点击“返回”(它将退出消息发送到关闭连接的服务器,并调用out.close和rest.close)然后我得到一个永无止境的“MSG”Toast循环 - Toast当输入被识别时我放了。 Is a out.close causing a block?
out.close会导致阻塞吗?
So it turns out, println on the server's side wasn't printing a new line -- adding + "\\n" at the end of the server's messages made it go through. 事实证明,服务器端的println没有打印新行 - 在服务器消息结束时添加+“\\ n”使其通过。 Why?
为什么? I don't know..
我不知道..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.