- package com.jwis.cos;
- import java.awt.Button;
- import java.awt.FlowLayout;
- import java.awt.Frame;
- import java.awt.TextArea;
- import java.awt.TextField;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.SocketException;
- import java.net.UnknownHostException;
- public class UDPChatFrame {
- public static void main(String[] args) {
- try {
- DatagramSocket sendSocket = new DatagramSocket();
- DatagramSocket recvSocket = new DatagramSocket(10000);
- SendMsg send = new SendMsg(sendSocket);
- ChatWindow chat = new ChatWindow();
- chat.Rec(send);
- RecvMsg recv = new RecvMsg(recvSocket,chat);
- Thread rec = new Thread(recv);
- rec.start();
- } catch (SocketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- class SendMsg implements Runnable
- {
- private DatagramSocket socket;
- private String Message;
- public void Message(String Message) {
- // TODO Auto-generated method stub
- this.Message = Message;
- }
- public SendMsg(DatagramSocket socket) {
- // TODO Auto-generated constructor stub
- this.socket = socket;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- byte [] sends = new byte[1024];
- sends = Message.getBytes();
- try {
- DatagramPacket packet =
- new DatagramPacket(sends, sends.length, InetAddress.getByName("192.168.1.104"), 10000);
- socket.send(packet);
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- }
- class RecvMsg implements Runnable
- {
- private DatagramSocket socket;
- private ChatWindow chat;
- public RecvMsg(DatagramSocket socket,ChatWindow chat) {
- // TODO Auto-generated constructor stub
- this.socket = socket;
- this.chat = chat;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- while(true)
- {
- byte [] recv = new byte[1024];
- DatagramPacket packet = new DatagramPacket(recv, recv.length);
- socket.receive(packet);
- String data =new String(packet.getData(),0,packet.getLength());
- String name =packet.getAddress().getHostAddress();
- chat.output.append(name+":\r\n"+data+"\r\n");
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- }
- class ChatWindow
- {
- private Frame frame;
- public TextArea output;
- private TextField input;
- private Button button;
- private SendMsg send= null;
- public void Rec(SendMsg send) {
- // TODO Auto-generated method stub
- this.send = send;
- }
- public ChatWindow() {
- // TODO Auto-generated constructor stub
- frame = new Frame("chatroom");
- frame.setBounds(100, 100, 450, 500);
- frame.setLayout(new FlowLayout());
- output = new TextArea(null, 20, 50,1);
- input = new TextField(30);
- button = new Button("send");
- frame.add(output);
- frame.add(input);
- frame.add(button);
- myEvents();
- frame.setVisible(true);
- }
- private void myEvents() {
- // TODO Auto-generated method stub
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- });
- button.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- String s = input.getText();
- send.Message(s);
- Thread sendMessage = new Thread(send);
- sendMessage.start();
- input.setText(null);
- }
- });
- input.addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent e)
- {
- if(e.getKeyCode()==KeyEvent.VK_ENTER)
- {
- String s = input.getText();
- send.Message(s);
- Thread sendMessage = new Thread(send);
- sendMessage.start();
- input.setText(null);
- }
- }
- });
- }
- }
复制代码
由于只有一个人无法进行测试。。。希望有人能够一起交流下 |
|