⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sender.java

📁 《J2ME实用教程》 源文件下载 本书没有仅仅局限于J2ME单机游戏的开发
💻 JAVA
字号:
package datagram;

import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
public class Sender extends Thread
{
private DatagramConnection dc;
private String address;
private String message;
public Sender(DatagramConnection dc)
{
this.dc = dc;
start();
}
public synchronized void send(String addr, String msg)
{
address = addr;
message = msg;
notify();
}
public synchronized void run()
{
while (true)
{
// If no client to deal, wait until one connects
if (message == null)
{
try
{
wait();
} catch (InterruptedException e)
{
}
}
try
{
byte[] bytes = message.getBytes();
Datagram dg = null;
// Are we a sender thread for the client ? If so then there's
 
// no address parameter
if (address == null)
{
dg = dc.newDatagram(bytes, bytes.length);
} else
{
dg = dc.newDatagram(bytes, bytes.length, address);
System.out.println(address);
}
dc.send(dg);
} catch (Exception ioe)
{
ioe.printStackTrace();
}
// Completed client handling, return handler to pool and
// mark for wait
message = null;
}
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -