import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
public class MulticastServer2 {
public static void main(String[] args) throws java.io.IOException{
new MulticastServerThread().start();
}
}
class MulticastServerThread extends QuoteServerThread2{
private long FIVE_SECONDS = 5000;
public MulticastServerThread() throws IOException{
super("MulticastServerThread");
}
boolean moreQuotes = true;
String dString = null;
public void run(){
while(moreQuotes){
try{
byte[] buf = new byte[256];
Date date = null;
date = new Date();
if(dString == null){
dString = new String(date.toString());
} else {
dString = getNextQuote();
}
buf = dString.getBytes();
InetAddress group = null;
try {
group = InetAddress.getByName("230.0.0.1");
} catch (UnknownHostException e) {
e.printStackTrace();
}
DatagramPacket packet = new DatagramPacket(buf,buf.length,group,4446);
socket.send(packet);
try{
sleep((long)(Math.random()*FIVE_SECONDS));
}catch(InterruptedException e){
e.getMessage();
}
}catch(IOException e){
e.printStackTrace();
moreQuotes = false;
}
}
socket.close();
}
}
class QuoteServerThread2 extends Thread{
protected DatagramSocket socket = null;
protected BufferedReader in = null;
protected boolean moreQuotes = true;
public QuoteServerThread2() throws IOException{
this("QuoteServerThread");
}
public QuoteServerThread2(String name) throws IOException{
super(name);
socket = new DatagramSocket(4446);
try{
in = new BufferedReader(new FileReader("one-liners.txt"));
}catch(FileNotFoundException e){
System.err.println("Could not open quote file.Serving time instead.");
}
}
public void run(){
while(moreQuotes){
try{
byte[] buf = new byte[256];
//receive reques
DatagramPacket packet = new DatagramPacket(buf,buf.length);
socket.receive(packet);
//figure out response
String dString = null;
if(in == null){
dString = new Date().toString();
}else{
dString = getNextQuote();
}
buf = dString.getBytes();
//send the response to the client at "address" and "potr"
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buf,buf.length,address,port);
socket.send(packet);
}catch(IOException e){
e.printStackTrace();
moreQuotes = false;
}
}
socket.close();
}
protected String getNextQuote(){
String value = null;
try{
if((value = in.readLine())==null){
in.close();
moreQuotes = false;
value="No more quotes.Goodbye.";
}
}catch(IOException e){
value = "IOException occurred in server";
}finally{
System.out.println("getNextQuote "+value);
}
return value;
}
}
客户服务器聊天和IP地址抓取
需积分: 10 5 浏览量
2009-01-05
12:07:03
上传
评论
收藏 6KB RAR 举报
chst606
- 粉丝: 0
- 资源: 2
最新资源
- 探索tecreate:软件开发的未来之星.zip
- 打标机项目C#源码连接扫码
- 基于SSM的房屋租赁系统的设计与实现
- xyctf:从入门到精通的实用指南.zip
- mmqrcode1714153659780.png
- Screenshot_2024-04-27-06-08-58-486_com.baidu.xin.aiqicha.jpg
- 基于Javaweb+Tomcat+MySQL的大学生公寓管理系统+sql文件.zip
- 实训作业基于javaweb的订单管理系统源码+数据库+实训报告.zip
- 多机调度问题贪心算法基于最小堆和贪心算法求解多机调度问题.zip
- 基于同态加密技术的匿名电子投票系统源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈