📄 multicastserver.java
字号:
import java.io.*; //import other classes
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.awt.List;
class mtServer implements Runnable //this is used to fulfill receiving the message from the client
{
public mtServer()
{
ServerListen(); //listen to the stable port
}
protected void processWindowEvent(WindowEvent e) //close the dialog
{
if(e.getID()==WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
public final static int DEFAULT_PORT=1236;
protected ServerSocket listen_socket;
Thread th;
java.util.Vector clients;
public void ServerListen() //listen
{
try
{
listen_socket=new ServerSocket(DEFAULT_PORT);//creat ServerSocket
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("Listenning:"+DEFAULT_PORT);
clients =new java.util.Vector();
th=new Thread(this);
th.start();
}
public void run()
{
try
{
while(true)
{
Socket client_socket = listen_socket.accept(); //create Socket
conn c= new conn(client_socket,this);
clients.add(c);
System.out.println("conneted success");
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
class conn extends Thread //this is used to fulfill multiple thread
{
protected Socket client;
protected BufferedReader in;
protected PrintWriter out;
mtServer server;
public conn(Socket clien_socket,mtServer server_frame)
{
client = clien_socket;
server = server_frame;
try
{ // get message from client
in=new BufferedReader(new InputStreamReader(client.getInputStream()));
out=new java.io.PrintWriter(client.getOutputStream());
}
catch (IOException e)
{
try
{
client.close();
}
catch (IOException e2)
{
e.printStackTrace();
}
}
this.start();
}
public void run()
{
String line;
int len;
try
{
for(;;)
{
line=receiveMsg();
System.out.println(line);
File ff=new File("H:\\网络课程设计\\课程设计报告\\PASSjava\\aaa.txt");
FileWriter fw=new FileWriter(ff);
fw.write(line);
fw.close(); //write the message received into "aaa.txt"
if(line==null)break;
}
}
catch (IOException e){}
finally
{
try
{
client.close();
}
catch (IOException e2){}
}
}
public String receiveMsg() throws IOException // get message from client
{
String msg =new String();
try
{
msg=in.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return msg;
}
}
class QuoteServerThread extends Thread //as MulticastServerThead's father ,dispose some other matters
{ static int m;
protected DatagramSocket socket = null;
protected BufferedReader in = null;
protected boolean moreQuotes = true;
public QuoteServerThread() throws IOException
{
this("QuoteServerThread");
}
public QuoteServerThread(String name) throws IOException
{
super(name);
socket = new DatagramSocket(4445);
try
{
in = new BufferedReader(new FileReader("aaa.txt")); //read "aaa.txt"
}
catch (FileNotFoundException e)
{
System.err.println("Could not open quote file. Serving time instead.");
}
}
static int Multic(int n)
{
m=n;
System.out.println("Quote"+"m="+m+"n="+n);
return m;
}
public void run() //cover the method run() in Thread
{
while (moreQuotes)
{
try
{
byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String dString = null;
if (in == null)
dString = new Date().toString();
else
dString = getNextQuote();
buf = dString.getBytes();
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);System.out.println("Quote run"+m);
}
catch (IOException e)
{
e.printStackTrace();
moreQuotes = false;
}
}
socket.close();
}
protected String getNextQuote() //continue to read
{
String returnValue = null;
try
{
if ((returnValue = in.readLine()) == null) //if no message to be sended
{
in.close();
moreQuotes = false;
returnValue = "No more quotes. Goodbye.";
}
}
catch (IOException e)
{
returnValue = "IOException occurred in server.";
}
return returnValue;
}
}
class MulticastServerThread extends QuoteServerThread //fulfill sending message to all clients
{
static int m=0;
static int tt;
private long FIVE_SECONDS = 5000; //define a random time
public MulticastServerThread() throws IOException
{
super("MulticastServerThread");
}
static int Multi(int n) //import the control parameter with "send"
{
m=n;
System.out.println("Multi"+"m="+m+"n="+n);
return m;
}
static int Multica(int t) //import the control parameter with "read"
{
tt=t;
System.out.println("Multi"+"tt="+tt+"t="+t);
return m;
}
public void run() //cover the method in QuoteThead
{
while (moreQuotes)
{
try
{
byte[] buf = new byte[256];
String dString = null;
if (in == null)
dString = new Date().toString();
else
dString = getNextQuote();
buf = dString.getBytes();
InetAddress group = InetAddress.getByName("226.0.0.4");//mulitcast address
MulticastSocket multiSocket=new MulticastSocket(4446); //create multicast
multiSocket.joinGroup(group); //join the group of the multicast
DatagramPacket multipacket=new
DatagramPacket(buf,buf.length,group, 4446); //create datagrampacket
String send = new String(multipacket.getData());
System.out.println(send.trim());
//Serverdialog aServerdialog=new Serverdialog(Serverdlg(send.trim()));
if(tt==1)
{
Serverdialog.Serverdlg(send.trim()); //show the message
System.out.println(send.trim());
tt=0;
}
System.out.println("Multi run"+m);
if(m==1)
{//socket.send(packet);
multiSocket.send(multipacket); //send the packet
System.out.println(send.trim());
m=0;
}
try
{
sleep((long)(Math.random() * FIVE_SECONDS)); //wait a random time
}
catch (InterruptedException e) { }
}
catch (IOException e)
{
e.printStackTrace();
moreQuotes = false;
}
}
socket.close();
}
}
class Serverdialog extends JFrame //fulfill visible dialog
{ static String sMsg;
static int n=0;
static int t=0;
Font myFont = new Font("Dialog", Font.BOLD, 24);
JPanel contentPane;
JTextField txtInput =new JTextField();
JTextField txturl =new JTextField();
JButton btnSend =new JButton();
JButton btnRead =new JButton();
List lstMsg =new List();
static String Serverdlg(String s) //fulfill the communication with MulticastServerThead
{
sMsg=s;
return sMsg;
}
public Serverdialog() throws Exception //dialog
{
InetAddress addr=InetAddress.getLocalHost();
txtInput.setText("Local IP:"+addr.getHostAddress());
txtInput.setBounds(new Rectangle(20,200,200,30));
txturl.setText("Msg needed sending(or time istead)...");
txturl.setBounds(new Rectangle(20,160,200,30));//show the message needed to be send
contentPane=(JPanel)this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(450,280));
this.setTitle("Message Server");
btnSend.setText("send"); //button send
btnSend.setBounds(new Rectangle(240,200,150,30));
btnSend.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnSend_actionPerformed(e);
}
});
btnRead.setText("Read Msg needed sending"); //button read
btnRead.setBounds(new Rectangle(240,160,150,30));
btnRead.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnRead_actionPerformed(e);
}
});
lstMsg.setBounds(new Rectangle(20,20,400,135));
contentPane.add(txtInput,null); //add different
contentPane.add(txturl,null);
contentPane.add(btnSend,null);
contentPane.add(btnRead,null);
contentPane.add(lstMsg,null);
lstMsg.setFont(myFont);
}
void btnSend_actionPerformed(ActionEvent e) //send the message
{
if(sMsg.trim()!="")
{
n=1; //the parameter is able to control "send"
QuoteServerThread.Multic(n);
MulticastServerThread.Multi(n); //refresh
try{
this.lstMsg.add(sMsg);
new MulticastServerThread().run();}
catch(Exception ee)
{System.out.println(ee.toString());}
}
}
void btnRead_actionPerformed(ActionEvent e) //read the message needed to be send
{
try{
t=1; //the parameter is able to control "read"
MulticastServerThread.Multica(t);
new MulticastServerThread().run(); //refresh
this.txturl.setText(sMsg);}
catch(Exception eee)
{System.out.println(eee.toString());}
}
}
public class MulticastServer
{
public static void main(String[] args) throws java.io.IOException
{
mtServer server =new mtServer(); //create a receive server
new MulticastServerThread().start(); //start the multicastserver
try{
Serverdialog c=new Serverdialog(); //start the dialog
c.show();}
catch(Exception e)
{System.out.println(e.toString());}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -