gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:10:56 25 4
gpt4 key购买 nike

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

这是代码:



<p>public class MainActivity extends Activity implements View.OnClickListener {</p>

<pre><code>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);
}
</code></pre>

<p>}</p>

最佳答案

服务器类中的 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());
}
}
}

关于java - 套接字只能发送一次,并且 Textview 不会从服务器收集信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18537107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com