這個客戶機服務器程序只運行一次,android發送字符串到服務器和服務器將它轉發給arduino。當我第一次發送arduino得到它,但是當我發送第二次服務器接收它,但不轉發給arduino。我看不到問題。我很感激,如果有人可以幫助我..客戶機/服務器程序沒有正常運行
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class server {
private ServerSocket serverSocket;
String type1;
String type;
Socket socket;
Socket s;
DataOutputStream dos;
private ClientHandler c = new ClientHandler();
private Connection con = new Connection();
public server(int port) {
try {
serverSocket = new ServerSocket(port);
new Connection().start();
// new ClientHandler().start();
} catch (IOException e) {}
}
private class Connection extends Thread {
public void run() {
//Socket socket;
while (true) {
try {
socket = serverSocket.accept();
// DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
System.out.println("connected");
DataInputStream dis = new DataInputStream(socket.getInputStream());
type = dis.readUTF();
System.out.println("jag fick det från android: " + type);
new ClientHandler().start();
dis.close();
//c.run();
} catch (IOException e) {}
}
}
}
private class ClientHandler extends Thread {
public void run() {
//System.out.println("1");
try {
// System.out.println("2");
socket = serverSocket.accept();
// System.out.println("3");
System.out.println("got it");
dos = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (true) {
try {
if (type == null) {
//break;
continue;
}
// System.out.println("4");
//DataInputStream dis1 = new DataInputStream(s.getInputStream());
//type1 = dis1.readUTF();
//System.out.println("3");
dos.writeUTF(type);
System.out.println("jag fick det från ardunio: " + type);
//dos.flush();
//dos.close();
// con.run();
// type = null;
//socket.close();
break;
//continue;
} catch (IOException e2) {}
}
}
}
public static void main(String[] args) {
new server(3389);
System.out.println(" Server startar");
}
}
開始時不會吞嚥異常......如果有什麼東西在引發異常,您至少應該知道引發異常,至少應該嘗試打印它們。 – SJuan76
主耶穌那個縮進... xD –
我看不到編輯我不知道爲什麼你可以把它發給我plz? –