JAVA網絡編程Socket實現信息傳輸

實現功能:【發送】、【清屏】、【記錄】、【震動】

java代碼:

package com.wrb;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Panel;
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.awt.event.WindowListener;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author Administrator
*
*/
public class Demo4_GUI_Chat extends Frame {
\t
\t
\tprivate TextField tf;
\tprivate Button send;
\tprivate Button clear;
\tprivate Button log;
\tprivate Button shake;
\tprivate TextArea viewText;
\tprivate TextArea sendText;
\tprivate DatagramSocket socket;
\tprivate BufferedWriter bw;
\tpublic static void main(String[] args) {
\t\tnew Demo4_GUI_Chat();
\t\t
\t}
\t
\tpublic Demo4_GUI_Chat(){
\t\ttry {
\t\t\tinit();
\t\t} catch (IOException e) {
\t\t\te.printStackTrace();
\t\t}
\t\tsouthPanel();
\t\tcenterPanel();
\t\tevent();
\t}
\t
\tprivate void event(){
\t\tthis.addWindowListener(new WindowAdapter() {
\t\t\tpublic void WindowClosing(WindowEvent e){
\t\t\t\ttry {
\t\t\t\t\tsocket.close();
\t\t\t\t\tbw.close();
\t\t\t\t\tSystem.exit(0);
\t\t\t\t} catch (IOException e1) {
\t\t\t\t\te1.printStackTrace();
\t\t\t\t}
\t\t\t}
\t\t});
\t\tsend.addActionListener(new ActionListener() {
\t\t\t
\t\t\t@Override
\t\t\tpublic void actionPerformed(ActionEvent e) {
\t\t\t\ttry {
\t\t\t\t\tsend();
\t\t\t\t} catch (IOException e1) {
\t\t\t\t\te1.printStackTrace();
\t\t\t\t}
\t\t\t}
\t\t});
\t\t
\t\tclear.addActionListener(new ActionListener() {
\t\t\t
\t\t\t@Override
\t\t\tpublic void actionPerformed(ActionEvent e) {
\t\t\t\tviewText.setText("");
\t\t\t}
\t\t});
\t\t
\t\tlog.addActionListener(new ActionListener() {
\t\t\t
\t\t\t@Override
\t\t\tpublic void actionPerformed(ActionEvent e) {
\t\t\t\tlogFile();
\t\t\t}
\t\t});
\t\t
\t\tshake.addActionListener(new ActionListener() {
\t\t\t
\t\t\t@Override
\t\t\tpublic void actionPerformed(ActionEvent e) {
\t\t\t\ttry {
\t\t\t\t\tshake();
\t\t\t\t} catch (InterruptedException e1) {
\t\t\t\t\te1.printStackTrace();
\t\t\t\t}
\t\t\t}
\t\t});
\t\tsendText.addKeyListener(new KeyAdapter() {
\t\t\tpublic void keyReleased(KeyEvent e){
\t\t\t\tif(e.getKeyCode()== KeyEvent.VK_ENTER)
\t\t\t\t\ttry {
\t\t\t\t\t\tsend();
\t\t\t\t\t} catch (IOException e1) {
\t\t\t\t\t\te1.printStackTrace();
\t\t\t\t\t}
\t\t\t}
\t\t});
\t}
\t
\t
\tprotected void shake() throws InterruptedException {
\t\tint x = this.getLocation().x;
\t\tint y = this.getLocation().y;
\t\tthis.setLocation(x+20,y+20);
\t\tThread.sleep(10);
\t\tthis.setLocation(x+20,y-20);
\t\tThread.sleep(10);
\t\tthis.setLocation(x-20,y+20);
\t\tThread.sleep(10);
\t\tthis.setLocation(x-20,y-20);
\t\tThread.sleep(10);
\t\tthis.setLocation(x,y);
\t\t
\t}
\tprotected void logFile() {
\t\ttry {
\t\t\tbw.flush();
\t\t\tFileInputStream fis = new FileInputStream("config.txt");
\t\t\tByteArrayOutputStream baos = new ByteArrayOutputStream();
\t\t\tbyte[] arr = new byte[8192];
\t\t\tint len ;
\t\t\t
\t\t\twhile((len=fis.read(arr))!=-1){
\t\t\t\tbaos.write(arr,0,len);
\t\t\t}
\t\t\tfis.close();
\t\t\tString message = baos.toString();
\t\t\tviewText.setText(message);
\t\t} catch (IOException e) {
\t\t\te.printStackTrace();
\t\t}
\t}
\tprivate void send() throws IOException{
\t\tString ip = tf.getText();
\t\tString message = sendText.getText();
\t\tDatagramPacket packet = new DatagramPacket(message.getBytes(), message.getBytes().length,
\t\t\t\tInetAddress.getByName(ip),6666);
\t\tsocket.send(packet);
\t\tString time = getCurrentTime();
\t\t
\t\tString str = time+" 我對"+ip+"說:\\r\\n"+message+"\\r\\n\\r\\n";
\t\tviewText.append(str);
\t\tbw.write(str);
\t\tbw.flush();
\t\tsendText.setText("");
\t}
\t
\tprivate String getCurrentTime() {
\t\tDate d = new Date();
\t\tSimpleDateFormat sdf = new SimpleDateFormat("yyy年MM月dd日 HH:mm:ss");
\t\treturn sdf.format(d);
\t}
\tprivate void centerPanel(){
\t\tPanel center = new Panel();
\t\tviewText = new TextArea();
\t\tsendText = new TextArea(5,1);
\t\tcenter.setLayout(new BorderLayout());
\t\t
\t\tviewText.setEditable(false);
\t\tviewText.setBackground(new Color(255,255,255));
\t\t
\t\tviewText.setFont(new Font("xxx",Font.PLAIN,15));
\t\tsendText.setFont(new Font("yyy",Font.PLAIN,15));
\t\tcenter.add(sendText,BorderLayout.SOUTH);
\t\tcenter.add(viewText,BorderLayout.CENTER);
\t\t
\t\tthis.add(center,BorderLayout.CENTER);
\t}
\t
\tprivate void southPanel(){
\t\tPanel south = new Panel();
\t\ttf = new TextField(15);
\t\ttf.setText("127.0.0.1");
\t\tsend = new Button("發送");
\t\tclear = new Button("清屏");
\t\tlog = new Button("記錄");
\t\tshake = new Button("震動");
\t\t
\t\tsouth.add(tf);
\t\tsouth.add(send);
\t\tsouth.add(clear);
\t\tsouth.add(log);
\t\tsouth.add(shake);
\t\tthis.add(south,BorderLayout.SOUTH);
\t}
\tpublic void init() throws IOException{
\t\tthis.setSize(400,600);
\t\tthis.setLocation(500,50);
\t\tnew Receive().start();
\t\tsocket = new DatagramSocket();
\t\tbw = new BufferedWriter(new FileWriter("config.txt",true));
\t\tthis.setVisible(true);
\t}
\t
\tprivate class Receive extends Thread{
\t\tpublic void run(){
\t\t\ttry {
\t\t\t\tDatagramSocket socket = new DatagramSocket(6666);
\t\t\t\tDatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
\t\t\t\twhile(true){
\t\t\t\t\tsocket.receive(packet);
\t\t\t\t\tbyte[] arr = packet.getData();
\t\t\t\t\tint len = packet.getLength();
\t\t\t\t\tString message = new String(arr,0,len);
\t\t\t\t\tString time = getCurrentTime();
\t\t\t\t\tString ip = packet.getAddress().getHostAddress();
\t\t\t\t\t
\t\t\t\t\tString str = time+" "+ip+" 對我說:\\r\\n"+message+"\\r\\n\\r\\n";
\t\t\t\t\tviewText.append(str);
\t\t\t\t\tbw.write(str);
\t\t\t\t\tbw.flush();
\t\t\t\t}
\t\t} catch (SocketException e) {
\t\t\t\te.printStackTrace();
\t\t\t} catch (IOException e) {
\t\t\t\te.printStackTrace();
\t\t\t}
\t\t}
\t}
}
JAVA網絡編程Socket實現信息傳輸


分享到:


相關文章: