我正在制作某种由Android应用程序控制的记分牌。
我开始将数据发送到服务器,但我尝试在我的JFrame
和我的Android应用上同步得分,但它无效。
无法理解为什么它不起作用,我可以从应用程序向服务器发送数据,但不能倒退,这是代码:
public class server {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static BufferedOutputStream bufferedOutputStream;
private static OutputStreamWriter outputStreamWriter;
private static String message;
public String ipadd;
public String ip;
public controller control;
public pantalla Controlador;
public void run() {
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.out.println("Nope Port!");
}
while (true) {
try {
clientSocket = serverSocket.accept();
ipadd = String.valueOf(clientSocket.getInetAddress());
ipadd = ipadd.substring(1, (ipadd.length()));
controller.ipAddress.setText("IP: " + ipadd);
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
message = bufferedReader.readLine();
OutputStream os = clientSocket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(controller.txtRedScore2.getText());
bw.flush();
switch (message) {
case "redUp":
controller.btnUpRed.doClick();
break;
case "redDown":
controller.btnDownRed.doClick();
break;
case "blueUp":
controller.btnUpBlue.doClick();
break;
case "blueDown":
controller.btnDownBlue.doClick();
break;
case "update":
controller.btnStart.doClick();
break;
case "x1":
controller.tglByOne.doClick();
break;
case "x5":
controller.tglByFive.doClick();
break;
case "x10":
controller.tglByTen.doClick();
break;
case "x20":
controller.tglByTwenty.doClick();
break;
case "x50":
controller.tglByFifty.doClick();
break;
}
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex) {
System.out.println("Nope!!" + ex);
}
}
}
}
现在我的Android应用程序
public class MainActivity extends Activity {
private Button redUp;
private Button redDown;
private Button blueUp;
private Button blueDown;
private Button update;
private Button x1;
private Button x5;
private Button x10;
private Button x20;
private Button x50;
private Socket client;
private Socket server;
private PrintWriter printwriter;
public String message;
public String score;
private TextView redScore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redUp = (Button) findViewById(R.id.redUp);
redDown = (Button) findViewById(R.id.redDown);
blueUp = (Button) findViewById(R.id.blueUp);
blueDown = (Button) findViewById(R.id.blueDown);
update = (Button) findViewById(R.id.update);
x1 = (Button) findViewById(R.id.x1);
x5 = (Button) findViewById(R.id.x5);
x10 = (Button) findViewById(R.id.x10);
x20 = (Button) findViewById(R.id.x20);
x50 = (Button) findViewById(R.id.x50);
redScore = (TextView) findViewById(R.id.redScore);
redUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
message = "redUp";
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
redDown.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
message = "redDown";
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
blueUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
message = "blueUp";
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
blueDown.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
message = "blueDown";
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
update.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
message = "update";
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
x1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
message = "x1";
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
x5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
message = "x5";
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
x10.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
message = "x10";
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
x20.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
message = "x20";
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
x50.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
message = "x50";
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
client = new Socket("192.168.11.23", 4444);
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(message);
printwriter.flush();
printwriter.close();
InputStream is = client.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
score = br.readLine();
redScore.setText(score);
Log.v("TEST", "--> " + score + " <--");
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}