0

我正在制作的程序中有两个问题...问题是我想通过 Edittext 将该信息通过 UDP 发送到服务器...问题是该程序仅在我第一次运行该程序时才有效,我的意思是,如果我打开应用程序并写一些文本,信息会发送到服务器,但如果我输入另一个内容并按下按钮以便发送新信息,它就不起作用......另一件事是TextView 没有附加信息,并且服务器发送了一些信息,但应用程序没有收集该信息......所以如果有人知道为什么会发生这种情况或我做错了什么,我感谢任何帮助!...谢谢指教...

这是代码:

public class MainActivity extends Activity implements View.OnClickListener {

public static final String SERVERIP = "190.99.20.200";
public static final int SERVERPORT = 5153;
public TextView serverResponse;
public EditText messageToSend;
public Button btnSend;
public boolean start;
public Handler handler;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    serverResponse = (TextView)findViewById(R.id.textView);
    messageToSend = (EditText)findViewById(R.id.editText);
    btnSend = (Button)findViewById(R.id.button);
    btnSend.setOnClickListener(this);
    start = false;
    new Thread(new Server()).start();
    try{
        Thread.sleep(500);
    }catch (InterruptedException e){
        updatetrack("Error on Server:" + e.getMessage());
    }
    new Thread(new Client()).start();
    handler = new Handler(){
        public void handledMessage(Message msg){
            String text = (String)msg.obj;
            serverResponse.append(text);
        }
    };
}

public class Client implements Runnable {

    @Override
    public void run() {
        while(start == false)
        {
        }
        try{
            Thread.sleep(500);
        }catch (InterruptedException e1){
            e1.printStackTrace();
        }
        try{
            InetAddress serverAddres = InetAddress.getByName(SERVERIP);
            updatetrack("Client:Start connectingn");
            DatagramSocket socket = new DatagramSocket();
            byte[] buffer;
            if(!messageToSend.getText().toString().isEmpty())
            {
                buffer = messageToSend.getText().toString().getBytes();
            }
            else
            {
                buffer = ("Message from android").getBytes();
            }
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length,serverAddres,SERVERPORT);
            updatetrack("Client:Sending" + new String(buffer)+ "'n");
            socket.send(packet);
            updatetrack("Client: Messange sentn");
            updatetrack("Client: Succed!n ");
            socket.close();
        }catch (Exception e){
            updatetrack("Client:Error!n" + e.getMessage());
        }
    }
}

public class Server implements Runnable{

    @Override
    public void run() {
        while (start == false)
        {
        }
        try{
            InetAddress serverAddress = InetAddress.getByName(SERVERIP);
            updatetrack("nServer: Start connectingn");
            DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddress);
            byte[] buffer = new byte[17];
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
            updatetrack("Server: Receivingn");
            socket.receive(packet);
            updatetrack("Server: Message received:" + new String(packet.getData())+"'n");
            updatetrack("Server : Succed!n");

        }catch (Exception e){
            updatetrack("Server: Error!n"+ e.getMessage());
        }
    }
}

public void onClick(View view)
{
    start = true;
}

public void updatetrack(String s)
{
    Message msg = new Message();
    String textTochange = s;
    msg.obj = textTochange;
    handler.sendMessage(msg);
}

}

4

1 回答 1

1

服务器类中的run方法将只运行一次。

while(start == false){
}

此 while 循环将继续循环,直到您调用该onClick方法,此时该方法中的其余代码run()被执行,并且服务器线程被终止。您需要稍微重新排列代码,并将其放在while循环中:

public void run() {
    while (true){

    try{
        InetAddress serverAddress = InetAddress.getByName(SERVERIP);
        updatetrack("nServer: Start connectingn");
        DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddress);
        byte[] buffer = new byte[17];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        updatetrack("Server: Receivingn");
        socket.receive(packet);
        updatetrack("Server: Message received:" + new String(packet.getData())+"'n");
        updatetrack("Server : Succed!n");

    }catch (Exception e){
        updatetrack("Server: Error!n"+ e.getMessage());
    }
}
}
于 2013-08-30T17:44:22.007 回答