I have just started learning TCPSocket recently. I hope to write a quiz app online through a socket using Android Studio. However, during the execution of the client, it was unable to connect to the server. I repeatedly checked a lot of data and code, but couldn't find a solution
This is the Server part code I currently have:
public class ServerWaitStart extends AppCompatActivity {
private static GlobalVariable gv;
private ServerSocket serverSocket;
TextView currentNum;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_11_server_wait_start);
gv = (GlobalVariable)getApplicationContext();
TextView IPaddress = findViewById(R.id.IPaddress);
gv.IP = getLocalIP(this);
IPaddress.setText("群組代碼:" + gv.IP);
currentNum = findViewById(R.id.currentNum);
serverConnect();
}
private ServerThread serverThread;
public void serverConnect(){
try {
serverSocket= new ServerSocket(5050);
} catch (IOException e) {
throw new RuntimeException(e);
}
serverThread = new ServerThread();
serverThread.startServer();
}
class ServerThread extends Thread implements Runnable{
private boolean serverRunning;
private int count = 1;
public void startServer(){
serverRunning = true;
start();
System.out.println("startServer execute");
}
@Override
public void run() {
try {
while(serverRunning){
System.out.println("in run");
Socket socket = serverSocket.accept();
System.out.println("accept!");
count++;
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(count + "\n");
// 立即發送
dos.flush();
currentNum.setText("目前已加入人數" + count);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
if (gv.chooseNum == count){
break;
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}//ServerThread
This is the Client part code I currently have:
public class ClientWaitStart extends AppCompatActivity {
private GlobalVariable gv;
private TextView currentNum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_07_client_wait_start);
currentNum = findViewById(R.id.currentNum);
gv = (GlobalVariable)getApplicationContext();
}
private Runnable Connection=new Runnable() {
@Override
public void run() {
try {
Socket socket = new Socket("10.0.2.16", 5050);
System.out.println("client connected");
DataInputStream dis = new DataInputStream(socket.getInputStream());
String waitPeopleNum = dis.readUTF();
runOnUiThread(new Runnable() {
@Override
public void run() {
currentNum.setText("目前已加入人數;" + waitPeopleNum);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
Please help and ask me any questions that I left vague. Thank you, Laimo.
Server System.out
2023-06-02 13:22:19.504 5063-5063 System.out com.appcompat.app.quiz I startServer execute 2023-06-02 13:22:19.505 5063-5315 System.out com.appcompat.app.quiz I in run