我正在写一个Android应用程序,它本质上是一个基于网络的tic tac toe,计算机上有一台服务器,两台Android手机作为客户互相对抗。为了完成这个任务,我创建了一个可以处理两个客户端的多线程服务器,并创建了两个客户端,我让它们接收服务器在单独的线程中发送它们的内容并使用处理程序我在服务器发送另一个后更改UI球员的举动。
问题是serverThread从不识别任何客户端发送的内容。我在客户端和服务器之间使用的协议是一个基于数字的协议,一旦客户端点击一个磁贴,一个数字被发送到服务器,服务器将这个数字添加到一个数组,并将其发送到所有线程(两个客户端),以便他们可以根据数字
将此图块从图块更改为“X”或“O”以下是两个按钮的监听器代码,所有其他按钮类似
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (value == 1) {
numberSent = 0;
Global.os.println(Integer.toString(0));
Global.os.flush();
Log.i("Sent", "The Zero");
}
else if (value == 2) {
numberSent = 9;
Global.os.println(Integer.toString(Activity2.numberSent));
Global.os.flush();
}
}
});
b2.setImageResource(R.drawable.tile);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(value == 1) {
numberSent = 1;
Global.os.println(Integer.toString(Activity2.numberSent));
Global.os.flush();
}
else if(value == 2) {
numberSent = 10;
Global.os.println(Integer.toString(Activity2.numberSent));
Global.os.flush();
}
}
});
以下是ClientThread的代码(处理从服务器收到的消息并具有更改UI的处理程序的代码)
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.util.Log;
public class ClientThread implements Runnable {
int s;
public void run() {
try {
try {
Global.s = new Socket("10.0.2.2", 5555);
Global.is = new DataInputStream(Global.s.getInputStream());
Global.os = new PrintStream(Global.s.getOutputStream());
Global.os.println(Integer.toString(100));
Log.i("Socket", "Created");
} catch (Exception e) {
e.printStackTrace();
}
while (true) {
Log.i("In the", "While");
try {
String S = null;
while ((S = Global.is.readLine()) != null) {
Log.d("ServerActivity", "line");
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Global.os.print(Integer.toString(Activity2.numberSent));
try {
String S = Global.is.readLine();
s = Integer.parseInt(S);
if (s == 0) {
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b1.setImageResource(R.drawable.x);
}
});
}
else if(s == 1){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b2.setImageResource(R.drawable.x);
}
});
}
else if(s == 2){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b3.setImageResource(R.drawable.x);
}
});
}
else if(s == 3){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b4.setImageResource(R.drawable.x);
}
});
}
else if(s == 4){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b5.setImageResource(R.drawable.x);
}
});
}
else if(s == 5){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b6.setImageResource(R.drawable.x);
}
});
}
else if(s == 6){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b7.setImageResource(R.drawable.x);
}
});
}
else if(s == 7){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b8.setImageResource(R.drawable.x);
}
});
}
else if(s == 8){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b9.setImageResource(R.drawable.x);
}
});
}
else if(s == 9){
Activity2.handler.post(new Runnable() {
public void run() {
Activity2.b1.setImageResource(R.drawable.o);
}
});
}
else if(s == 10){
Activity2.handler.post(new Runnable() {
public void run() {
Activity2.b2.setImageResource(R.drawable.o);
}
});
}
else if(s == 11){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b3.setImageResource(R.drawable.o);
}
});
}
else if(s == 12){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b4.setImageResource(R.drawable.o);
}
});
}
else if(s == 13){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b5.setImageResource(R.drawable.o);
}
});
}
else if(s == 14){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b6.setImageResource(R.drawable.o);
}
});
}
else if(s == 15){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b7.setImageResource(R.drawable.o);
}
});
}
else if(s == 16){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b8.setImageResource(R.drawable.o);
}
});
}
else if(s == 17){
Activity2.handler.post(new Runnable() {
@Override
public void run() {
Activity2.b9.setImageResource(R.drawable.o);
}
});
}
else {
}
} catch (IOException e) {
e.printStackTrace();
}
});
}
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
}
}
}
这是我的服务器线程的代码
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
class ServerThread extends Thread {
DataInputStream is = null;
PrintStream os = null;
Socket clientSocket = null;
ServerThread t[];
public String received;
public ServerThread(Socket clientSocket, ServerThread[] t){
this.clientSocket=clientSocket;
this.t=t;
}
public void run() {
try {
is = new DataInputStream(clientSocket.getInputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
try {
os = new PrintStream(clientSocket.getOutputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
try{
// Once the socket is open all the work will be here!!!!!!
while(true) {
System.out.print("In the while");
received = is.readLine();
int s = Integer.parseInt(received);
if(s == 100) {
System.out.print("I recieved the 100");
}
if(s == 0) {
System.out.println("I recieved the 0");
XOServer.boardArray[0] = 1;
XOServer.turn = true;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 0;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 1) {
XOServer.boardArray[1] = 1;
XOServer.turn = true;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 1;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 2) {
XOServer.boardArray[2] = 1;
XOServer.turn = true;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 2;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 3) {
XOServer.boardArray[3] = 1;
XOServer.turn = true;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 3;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 4) {
XOServer.boardArray[4] = 1;
XOServer.turn = true;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 4;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 5) {
XOServer.boardArray[5] = 1;
XOServer.turn = true;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 5;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 6) {
XOServer.boardArray[6] = 1;
XOServer.turn = true;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 6;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 7) {
XOServer.boardArray[7] = 1;
XOServer.turn = true;
System.out.print("I recieved the 7");
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 7;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 8) {
XOServer.boardArray[8] = 1;
XOServer.turn = true;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 8;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 9) {
XOServer.boardArray[0] = 2;
XOServer.turn = false;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 9;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 10) {
XOServer.boardArray[1] = 2;
XOServer.turn = false;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 10;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 11) {
XOServer.boardArray[2] = 2;
XOServer.turn = false;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 11;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 12) {
XOServer.boardArray[3] = 2;
XOServer.turn = false;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 12;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 13) {
XOServer.boardArray[4] = 2;
XOServer.turn = false;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 13;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 14)
XOServer.boardArray[5] = 2;
XOServer.turn = false;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 14;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 15) {
XOServer.boardArray[6] = 2;
XOServer.turn = false;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 15;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 16) {
XOServer.boardArray[7] = 2;
XOServer.turn = false;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 16;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else if(s == 17) {
XOServer.boardArray[8] = 2;
XOServer.turn = false;
for(int i = 0; i<=t.length; i++) {
if (t[i]!=null) {
XOServer.sentValue = 17;
t[i].os.println(Integer.toString(XOServer.sentValue));
t[i].os.flush();
}
}
}
else {
}
}
}
catch (Exception e){
System.out.print(e);
}
}
以下是我服务器的代码
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class XOServer{
// Declaration section:
// declare a server socket and a client socket for the server
// declare an input and an output stream
static Socket clientSocket = null;
static ServerSocket serverSocket = null;
static PrintStream os = null;
static DataInputStream is = null;
static int sentValue;
// This chat server can accept up to 10 clients' connections
static ServerThread t[] = new ServerThread[10];
public static int [] boardArray = new int[9];
public static boolean turn = false;
public static void main(String args[]) {
// The default port
int port_number=5555;
if (args.length < 1) {
System.out.println("Usage: java MultiThreadChatServer \n"+
"Now using port number="+port_number);
} else {
port_number=Integer.valueOf(args[0]).intValue();
}
for(int i = 0; i <=8; i++ ) {
boardArray[i] = 0;
}
// Initialization section:
// Try to open a server socket on port port_number (default 2222)
// Note that we can't choose a port less than 1023 if we are not
// privileged users (root)
try {
serverSocket = new ServerSocket(port_number);
}
catch (IOException e) {
System.out.println(e);
}
// Create a socket object from the ServerSocket to listen and accept
// connections.
// Open input and output streams for this socket will be created in
// client's thread since every client is served by the server in
// an individual thread
while (true) {
try {
clientSocket = serverSocket.accept();
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
for(int i=0; i<=9; i++){
if(t[i]==null) {
(t[i] = new ServerThread(clientSocket,t)).start();
break;
}
}
}
catch (IOException e) {
System.out.println(e);
}
}
}
}
答案 0 :(得分:1)
我认为您的问题(或至少一个问题)在服务器上。当客户端连接时,您正在设置:
os = new PrintStream(clientSocket.getOutputStream());
os
是服务器内部的静态变量。这是错误的。
static PrintStream os
每个客户端线程必须拥有自己独立的PrintStream
。您需要获取输入/输出流并将它们传递给您的处理程序。
clientSocket = serverSocket.accept();
DataInputStream is = new DataInputStream(clientSocket.getInputStream());
PrintStream os = new PrintStream(clientSocket.getOutputStream());
... new ServerThread(is, os, clientSocket, t)).start();
另外,我不明白为什么你为每个客户端连接分叉10个线程。这似乎是错的。通常,您可以接受客户端连接,然后fork 1线程来处理与该输入和输出流的特定连接。