套接字只能发送一次,Textview不会从服务器收集信息

时间:2013-08-30 15:58:18

标签: java android sockets interface udp

我在程序中遇到了两个问题......我想通过一个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);
}

}

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); }

1 个答案:

答案 0 :(得分: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());
    }
}
}