Java Android Socket客户端没有';不要发送消息
我正在Android上开发socket客户端。 连接:Java Android Socket客户端没有';不要发送消息,java,android,sockets,tcpclient,Java,Android,Sockets,Tcpclient,我正在Android上开发socket客户端。 连接: public class MyClientTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... arg0) { try { InetAddress serverAddr = InetAddress.getByName(ip.getTex
public class MyClientTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
try {
InetAddress serverAddr = InetAddress.getByName(ip.getText().toString());
socket = new Socket(serverAddr, Integer.parseInt(port.getText().toString()));
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
//textResponse.setText(response);
super.onPostExecute(result);
}
服务器(Java)
只有在您按下按钮“buttonTest”时,才会在第一次发送消息。在成功之后什么也没有发生。
如何修复它?它是未发送还是未接收?你怎么知道的?什么都没发生?那有例外吗?不,我没有例外。消息只发送和接收一次。如果它不是第二次发送,那么您应该有一个异常。如果第二次未收到该消息,那么您如何知道该消息已发送?如果您不进行更好的解释,我们将不再进一步。你怎么知道消息没有发送?你没有记录任何东西。那么你怎么知道发生了什么?哪些语句第二次执行/未执行?请不要在注释中添加代码。它不可读。最好在声明dataoutputstream后启动while循环。
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.buttonConnect){
new MyClientTask().execute();
}
else if (v.getId() == R.id.buttonTest){
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println("String");
out.flush();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Server {
public static void main(String args[]) throws Exception {
String clientSentence;
String capitalizedSentence;
System.out.println(InetAddress.getLocalHost());
ServerSocket welcomeSocket = new ServerSocket(6000);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
System.out.println(clientSentence);
}
}
}