📄 client.java
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.JOptionPane;
class Client implements WindowListener,ActionListener
{
Frame f;
Label lb1;
Label lb2;
Label lb3;
Label lb4;
Label portLabel;
Panel p1;
Panel p2;
Container p3;
Panel p31;
Panel p32;
Button bt1;
Button btMember;
String name;
TextField tf1;
TextField tf2;
TextArea ta;
TextField tf;
TextField portField;
Button bt;
int port;
String hostname;
String ip;
DatagramSocket receiveSocket, sendSocket;
DatagramPacket receivePacket ,sendPacket;
public static void main(String[] args)
{ JOptionPane.showMessageDialog(null,"欢迎来到我们的聊天系统!");
Client client=new Client();
client.creatwindow();
client.start();
client.receiveMessage();
}
void creatwindow()
{
f=new Frame("聊天程序客户端");
p1=new Panel();
p2=new Panel();
p3=new Panel(new GridLayout(2,1));
p31=new Panel();
p32=new Panel();
lb1=new Label("对话框");
lb2=new Label("发送消息");
lb3=new Label("服务器IP");
lb4=new Label("你的姓名:");
portLabel=new Label("请填写端口号(5000-6000)");
portField=new TextField(5);
tf1=new TextField(30);
tf2=new TextField(10);
bt1=new Button("确定");
btMember=new Button("本组成员列表");
ta=new TextArea(15,70);
tf=new TextField(50);
bt=new Button("发送");
ta.setEditable(false);
p1.add(portLabel);
p1.add(portField);
p1.add(lb3);
p1.add(tf1);
p1.add(bt1);
p2.add(lb1);
p2.add(ta);
p32.add(lb4);
p32.add(tf2);
p31.add(lb2);
p31.add(tf);
p32.add(bt);
p3.add(p31);
p3.add(p32);
p32.add(btMember);
f.addWindowListener(this);
btMember.addActionListener(this);
bt.addActionListener(this);
bt1.addActionListener(this);
f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.CENTER);
f.add(p3,BorderLayout.SOUTH);
f.setSize(600,300);
f.setVisible(true);
f.setLocation(500,300);
ta.append("注意,请分别输入姓名,IP地址和端口号:5000-6000");
ta.append("\n");
}
public void windowClosing(WindowEvent e)
{
receiveSocket.close();
sendSocket.close();
System.exit(0);
}
public void windowClosed(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void start()
{
try
{
sendSocket=new DatagramSocket(5000);//客户机发送端口
System.out.println(port);
}
catch(Exception e)
{
ta.append(e+"\n");
}
}
public void receiveMessage()//客户机发送消息
{
try
{
receiveSocket=new DatagramSocket(3000);//客户机接受端口
while(true)
{
byte[] buf=new byte[200];
receivePacket=new DatagramPacket(buf,buf.length);
receiveSocket.receive(receivePacket);
if(receivePacket.getLength()==0)
{
ta.append("空消息"+"\n");
continue;
}
ByteArrayInputStream bin=new ByteArrayInputStream(receivePacket.getData());
BufferedReader read=new BufferedReader(new InputStreamReader(bin));
ta.append("服务器:"+read.readLine());
ta.append("\n");
read.close();
bin.close();
}
}
catch(Exception e)
{
ta.append(e+"sendmessage error\n");
}
}
public void sendMessage()
{
try{
String s=tf.getText();
name=tf2.getText();
int nameLength=name.length();
if(nameLength==0)
{
JOptionPane.showMessageDialog(null,"请输入姓名,对方不能确定收到谁的数据!");
}
s=name+"发送的数据是:"+s;
tf.setText("");
ta.append("客户机:"+s);
ta.append("\n");
ByteArrayOutputStream out=new ByteArrayOutputStream();
PrintStream pout=new PrintStream(out);
pout.print(s);
byte[] buf=out.toByteArray();
sendPacket=new DatagramPacket(buf,buf.length,InetAddress.getByName(ip),9000);
sendSocket.send(sendPacket);
buf=null;
}
catch(Exception e)
{
ta.append(e+"\n");
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt)
{
sendMessage();
}
else if(e.getSource()==bt1)
{
ip=tf1.getText();
tf1.setText("");
}
if(e.getSource()==btMember)
{
Mem members=new Mem();
members.setSize(250,233);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -