📄 multicastclient.java
字号:
import java.applet.*; //import other classes
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.List;
import javax.swing.*;
import javax.swing.JOptionPane;
class mtClient extends JFrame implements Runnable //fulfill sending message to the server
{
Font myFont = new Font("Dialog", Font.BOLD, 24);
public mtClient()
{
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void init() //init
{
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
JPanel contentPane;
JTextField txtInput =new JTextField();
JTextField txturl =new JTextField();
JButton btnSend =new JButton();
JButton btnStart =new JButton();
List lstMsg =new List();
Socket s;
Thread th;
BufferedReader in;
PrintWriter out;
public final static int DEFAULT_PORT=1236;
boolean bConnected;
public void startConnect(String url)
{
bConnected=false;
if(url=="") url="127.0.0.1";
try
{
s=new Socket(url,DEFAULT_PORT);
bConnected=true;
this.lstMsg.add("Connect Success"); //connect with server available
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new java.io.PrintWriter(s.getOutputStream());
}
catch (Exception e)
{
e.printStackTrace();
this.lstMsg.add("Connect Failed");
}
if(th==null)
{
th=new Thread(this);
th.start();
}
}
public void run()
{
try
{
while(true)
{
th.sleep(100L);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void sendMsg(String msg) throws IOException //send
{
out.println(msg);
out.flush();
}
public void jbInit() throws Exception
{
InetAddress addr=InetAddress.getLocalHost();
txtInput.setText(addr.getHostName()+"say:");
txtInput.setBounds(new Rectangle(20,180,200,30));
txturl.setText("Input Server ip"); //ip
txturl.setBounds(new Rectangle(20,140,200,30));
contentPane=(JPanel)this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(400,290));
this.setTitle("Message Client");
btnSend.setText("send"); //button send
btnSend.setBounds(new Rectangle(240,180,140,30));
btnSend.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnSend_actionPerformed(e);
}
});
btnStart.setText("Connect Server");
btnStart.setBounds(new Rectangle(240,140,140,30));
btnStart.addActionListener(new java.awt.event.ActionListener()//button connect
{
public void actionPerformed(ActionEvent e)
{
btnStart_actionPerformed(e);
}
});
lstMsg.setBounds(new Rectangle(20,20,350,100));
contentPane.add(txtInput,null);
contentPane.add(txturl,null);
contentPane.add(btnSend,null);
contentPane.add(btnStart,null);
contentPane.add(lstMsg,null);
lstMsg.setFont(myFont);
}
void btnSend_actionPerformed(ActionEvent e) // button send dispose
{
if(txtInput.getText().length()!=0)
{
try
{ String ss=new String(this.txtInput.getText());
sendMsg(ss);
this.lstMsg.add(ss);
}
catch (Exception e2)
{
this.lstMsg.add(e2.toString());
}
}
}
void btnStart_actionPerformed(ActionEvent e) //button start dispose
{
this.startConnect(this.txturl.getText());
}
protected void processWindowEvent(WindowEvent e)//close the dialog
{
super.processWindowEvent(e);
if(e.getID()==WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
}
public class MulticastClient extends JFrame //methos main(),fulfill receiving the message from the MulticastServer
{
public static void main(String[] args) throws IOException
{
mtClient c=new mtClient();
c.show();
MulticastSocket socket = new MulticastSocket(4446);//create muliticastsocket
InetAddress address = InetAddress.getByName("226.0.0.4");
socket.joinGroup(address); //join the group
DatagramPacket packet;
for (int i = 0; i < 1000; i++) //always wait
{
byte[] buf = new byte[256];
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet); //receive the datagrampacket
String received = new String(packet.getData());
System.out.println("Quote of the Moment: " + received);
String s1=received.trim();
JOptionPane.showMessageDialog(null,s1,"Tip of the System!",1); //show the tip
}
socket.leaveGroup(address); //leave the group
socket.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -