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

📄 singleudpserver.java

📁 这是一个用java编写的关于WEB应用编程的简单的聊天系统的样例
💻 JAVA
字号:
package UDP;

import java.io.*;
import java.net.*;
public class SingleUDPServer {
	public static void main(String[] args) throws IOException {
		new QuoteServerThread().start();
	}
}
class QuoteServerThread extends Thread {
	protected DatagramSocket socket = null;
	private String[] quotes = {
	"Life is wonderful. Without it we'd all be dead.",
			"Home is where you hang your @",
			"The E-mail of the species is more deadly than the mail.",
			"Great groups from little icons grow.",
			"Speak softly and carry a cellular phone.",
			"Don't put all your hypes in one home page.",
			"Virtual reality is its own reward.", "Modulation in all things.",
			"A user and his leisure time are soon parted." };

	protected boolean moreQuotes = true;
	public QuoteServerThread() throws IOException {
		this("QuoteServerThread");
	}
	public QuoteServerThread(String name) throws IOException {
		super(name);
		socket = new DatagramSocket(6666);
	}

	public void run() {
		try {
			byte[] buf = new byte[256];
			DatagramPacket packet = new DatagramPacket(buf, buf.length);
			socket.receive(packet); 
			buf = packet.getData(); 
			String info = new String(buf,0,1);
			int demand = 0;
			if (info != null) {
				try {
					demand = Integer.parseInt(info);
				} catch (NumberFormatException nfe) {
					demand = 0;
					nfe.printStackTrace();
				}
			}
			
			buf = quotes[demand].getBytes();
			InetAddress address = packet.getAddress();
			int port = packet.getPort();
			packet = new DatagramPacket(buf, buf.length, address, port);
			socket.send(packet);
			System.out.println("Client host IP is:" + address.getHostAddress()
					+ "; name is:" + address.getHostName() + "; port is:"
					+ port);
		} catch (IOException e) {
			e.printStackTrace();
		}
		socket.close();
	}

}

⌨️ 快捷键说明

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