UDP-Connections within the emul

本文介绍了一个简单的UDP连接示例,演示了如何在两个设备之间发送和接收数据。客户端发送一条消息到指定的服务器地址和端口,服务器接收到消息并打印出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hello Guys,

TCP seems to be a kind of problem !

Arrow For now I succeeded, to send data between two UDP-Connections within the emulator. The Log:

Java:
D/UDP(1515): S: Connecting...
D/UDP(1515): S: Receiving...
D/UDP(1515): C: Connecting...
D/UDP(1515): C: Sending: 'Hello from Client'
D/UDP(1515): S: Received: 'Hello from Client'
D/UDP(1515): S: Done.
D/UDP(1515): C: Sent.
D/UDP(1515): C: Done.



I'll have a look for some working TCP-Stuff Exclamation

The code below will be put into an tutorial right now.

Java:
public class UDPConnection extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
       
        Thread sThread = new Thread(new Server());
        Thread cThread = new Thread(new Client());
       
        sThread.start();
        try {
               Thread.sleep(500);
          } catch (InterruptedException e) { }
       
          cThread.start();
    }
}



Java:
public class Server implements Runnable{
     public static final String SERVERIP = "127.0.0.1";
     public static final int SERVERPORT = 4444;
          
     @Override
     public void run() {
          try {
               Log.d("UDP", "S: Connecting...");
               InetAddress serverAddr = InetAddress.getByName(SERVERIP);
               DatagramSocket socket = new DatagramSocket(SERVERPORT,serverAddr);
               byte[] buf = new byte[17];
               DatagramPacket packet = new DatagramPacket(buf, buf.length);
               Log.d("UDP", "S: Receiving...");
               socket.receive(packet);
               Log.d("UDP", "S: Received: '" + new String(packet.getData()) + "'");
               Log.d("UDP", "S: Done.");
          } catch (Exception e) {
               Log.e("UDP", "S: Error", e);
          }
     }
}



Java:
public class Client implements Runnable {
     @Override
     public void run() {
          try {
               Log.d("UDP", "C: Connecting...");
               DatagramSocket socket = new DatagramSocket();
               byte[] buf = ("Hello from Client").getBytes();
               InetAddress serverAddr = InetAddress.getByName(Server.SERVERIP);
               DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, Server.SERVERPORT);
               Log.d("UDP", "C: Sending: '" + new String(buf) + "'");
               socket.send(packet);
               Log.d("UDP", "C: Sent.");
               Log.d("UDP", "C: Done.");
          } catch (Exception e) {
               Log.e("UDP", "C: Error", e);
          }
     }
}



Regards,
plusminus

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值