我是 Java 多线程和套接字编程的新手。我想知道什么是实现 2 个线程的最佳方法 - 一个用于接收套接字,一个用于发送套接字。如果我正在尝试做的事情听起来很荒谬,请告诉我为什么!代码很大程度上是从 Sun 的在线教程中获得灵感的。我想使用多播套接字,以便我可以使用多播组。
class Server extends Thread
{
static protected MulticastSocket socket = null;
protected BufferedReader in = null;
public InetAddress group;
private static class Receive implements Runnable
{
public void run()
{
try
{
byte[] buf = new byte[256];
DatagramPacket pkt = new DatagramPacket(buf,buf.length);
socket.receive(pkt);
String received = new String(pkt.getData(),0,pkt.getLength());
System.out.println("From server@" + received);
Thread.sleep(1000);
}
catch (IOException e)
{
System.out.println("Error:"+e);
}
catch (InterruptedException e)
{
System.out.println("Error:"+e);
}
}
}
public Server() throws IOException
{
super("server");
socket = new MulticastSocket(4446);
group = InetAddress.getByName("239.231.12.3");
socket.joinGroup(group);
}
public void run()
{
while(1>0)
{
try
{
byte[] buf = new byte[256];
DatagramPacket pkt = new DatagramPacket(buf,buf.length);
//String msg = reader.readLine();
String pid = ManagementFactory.getRuntimeMXBean().getName();
buf = pid.getBytes();
pkt = new DatagramPacket(buf,buf.length,group,4446);
socket.send(pkt);
Thread t = new Thread(new Receive());
t.start();
while(t.isAlive())
{
t.join(1000);
}
sleep(1);
}
catch (IOException e)
{
System.out.println("Error:"+e);
}
catch (InterruptedException e)
{
System.out.println("Error:"+e);
}
}
//socket.close();
}
public static void main(String[] args) throws IOException
{
new Server().start();
//System.out.println("Hello");
}
}
最佳答案
首先是第一件事:根据 Java Naming Conventions,您的类(class)应以大写字母开头:
Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
第二: 尝试将代码分解成连贯的部分,并围绕您正在处理的一些常见功能来组织它们……也许是围绕您正在编程的功能或模型。
服务器的(基本)模型是它所做的唯一事情是接收套接字连接……服务器依赖处理程序来处理这些连接,仅此而已。如果您尝试构建该模型,它将看起来像这样:
class Server{
private final ServerSocket serverSocket;
private final ExecutorService pool;
public Server(int port, int poolSize) throws IOException {
serverSocket = new ServerSocket(port);
pool = Executors.newFixedThreadPool(poolSize);
}
public void serve() {
try {
while(true) {
pool.execute(new Handler(serverSocket.accept()));
}
} catch (IOException ex) {
pool.shutdown();
}
}
}
class Handler implements Runnable {
private final Socket socket;
Handler(Socket socket) { this.socket = socket; }
public void run() {
// receive the datagram packets
}
}
第三:我建议您查看一些现有示例。
- 多线程客户端/服务器应用程序:
http://www.ase.md/~aursu/ClientServerThreads.html - 道格·李:
http://www.developer.com/java/ent/article.php/3645111/Java-5s-BlockingQueue.htm (感谢约翰)
http://gee.cs.oswego.edu/dl/cpj/index.html(仍然找不到确切的例子,但它就在某个地方......如果你有勇气查看他的 allcode.java 文件)。 - 并发实践示例:
http://www.javaconcurrencyinpractice.com/listings.html - Java 并发教程:
http://java.sun.com/docs/books/tutorial/essential/concurrency/
根据评论更新:
好的 Ravi,您的代码存在一些大问题和一些小问题:
我假设
Receive
类是您的客户端...您应该将它作为一个单独的程序(有自己的主类)拉出来,然后运行您的服务器和多个客户端同时。为您发送的每个新 UDP 包从您的服务器生成一个新的“客户端线程”是一个令人不安的想法(大问题)。当您制作客户端应用程序时,您应该让它在自己的
while
循环中运行接收代码(次要 问题),例如:public class Client extends Thread { public Client(/*..*/) { // initialize your client } public void run() { while(true) { // receive UDP packets // process the UDP packets } } public static void main(String[] args) throws IOException { // start your client new Client().start(); } }
每个客户端只需要一个线程,每个服务器只需要一个线程(从技术上讲,您甚至不需要一个单独的线程,因为 main 有自己的线程),所以您可能找不到
ExecutorService
那个有用。
否则您的方法是正确的...但我仍然建议您查看一些示例。
关于Java:多线程和 UDP 套接字编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2687238/