重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
//时间关系,粗略的做了一下,一个模拟UDP协议的测试,图标,IP,以及端口都可以设成
公司主营业务:成都网站设计、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出罗山免费做网站回馈大家。
//发送者端的电脑参数!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.Serializable;
import java点虐 .DatagramPacket;
import java点虐 .DatagramSocket;
import java点虐 .InetAddress;
import java点虐 .SocketException;
import java点虐 .UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Day03_A extends JFrame implements Serializable {
private static final long serialVersionUID = 57L;
private JTextArea txtRece, txtSend;// 接受与发送文本域!
private JPanel jp1, jp2;// 面板!
private JScrollPane jsp;
private JLabel jab;// 标签
private JButton jb;// 按钮
private JMenuBar jmb;// 窗体状态栏
private JMenuItem a=null,b=null;
private DatagramSocket dsend = null, drece;
private DatagramPacket dp1, dp2;
private byte[] by1, by2;
private String[][][] menArr= {{{"语言"},{"中文","英文"}}, {{"字体"},{"隶书","彩云",}},{{"辅助"},{"放大","缩小"}}, {{"功能"},{"计算","闹钟"}}};
Day03_A() {
by2 = new byte[1024];
this.setTitle("山寨QQ测试");// 窗体标题
this.setBounds(300, 300, 400, 500);// 位置及大小!
this.setResizable(false);
this.setIconImage(new ImageIcon("E:/Java_Worker/Day_41学习_Gui/src/com/djw/swing01/qq.png").getImage());
this.setLayout(new GridLayout(2, 1));// 网格模式2行1列
init();
this.setJMenuBar(jmb);
this.setVisible(true);
}
private void init() {
jmb = new JMenuBar();
for(int i=0;imenArr.length;i++) {
for(int j=0;jmenArr[i].length;j++) {
for(int x=0;xmenArr[i][j].length;x++) {
if(j==0) {
a=new JMenu(menArr[i][j][x]);
}else {
b=new JMenuItem(menArr[i][j][x]);
a.add(b);
}
}
jmb.add(a);
}
}
jp1 = new JPanel();
jp1.setLayout(new BorderLayout());
txtRece = new JTextArea();
jsp = new JScrollPane(txtRece);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(jsp);
jp2 = new JPanel();// 面板
jp2.setBackground(new Color(107, 197, 127));
jp2.setLayout(new BorderLayout());
txtSend = new JTextArea();
jab = new JLabel();
jab.setText("[ALT+S]/[回车+CTRL]或者[点击发送按钮]:发送信息");
jab.setHorizontalAlignment(JLabel.CENTER);// 标签文本居中
jb = new JButton("发送");
jp2.add(jab, BorderLayout.NORTH);// 指定到面板北面
jp2.add(txtSend, BorderLayout.CENTER);// 文本放中间
jp2.add(jb, BorderLayout.SOUTH);
this.add(jp2);
myeve();
}
private void myeve() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);// 窗体可关闭
try {
dsend = new DatagramSocket();// 发送端套接字
drece = new DatagramSocket(10008);
} catch (SocketException e) {
e.printStackTrace();
}
re();// 启动无限循环侦听!
txtSend.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent k) {
if (k.isControlDown() k.getKeyCode() == KeyEvent.VK_ENTER) {
se();
}
if (k.isAltDown() k.getKeyCode() == KeyEvent.VK_S) {
se();
}
}
});
jb.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
se();
txtSend.requestFocus();
}
});
}
private void se() {
try {
by1 = txtSend.getText().getBytes();
dp1 = new DatagramPacket(by1, by1.length, InetAddress.getByName("192.168.2.4"), 10008);
dsend.send(dp1);// 推送数据
txtSend.setText(null);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void re() {
new Thread() {
public void run() {
while (true) {
try {
String time = new SimpleDateFormat("MM-dd/HH:mm").format(new Date());
dp2 = new DatagramPacket(by2, by2.length);
drece.receive(dp2);
String ip = dp2.getAddress().getHostAddress();
txtRece.append("用户IP:" + ip + " 时间:");
txtRece.append(time + ": " + System.getProperty("line.separator"));
txtRece.append(new String(by2, 0, dp2.getLength()) + System.getProperty("line.separator"));
txtRece.setCaretPosition(txtRece.getText().length());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
}
public static void main(String[] args) {
new Day03_A();
}
}
传统的进程间通信的方式有大致如下几种:
(1) 管道(PIPE)
(2) 命名管道(FIFO)
(3) 信号量(Semphore)
(4) 消息队列(MessageQueue)
(5) 共享内存(SharedMemory)
(6) Socket
Java如何支持进程间通信。我们把Java进程理解为JVM进程。很明显,传统的这些大部分技术是无法被我们的应用程序利用了(这些进程间通信都是靠系统调用来实现的)。但是Java也有很多方法可以进行进程间通信的。
除了上面提到的Socket之外,当然首选的IPC可以使用Rmi,或者Corba也可以。另外Java nio的MappedByteBuffer也可以通过内存映射文件来实现进程间通信(共享内存)。
这个是我们练习是做的,下面的分别为两个客户端,写的有点固定,你可以自行的在修改一下
1.
package com.yl.testqq;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java点虐 .ServerSocket;
import java点虐 .Socket;
import java点虐 .UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;
public class Zjh extends JFrame implements Runnable {
private final JTextArea txtRes;
private final JTextArea txtSend;
private Socket client;
private static final String IP = "localhost";
private PrintStream ps;
public static final int PORT = 6666;
private ServerSocket server;
private BufferedReader br;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Zjh frame = new Zjh();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public Zjh() {
super();
setBackground(new Color(0, 0, 0));
setTitle("Cxr to Zjh");
getContentPane().setBackground(new Color(0, 0, 0));
getContentPane().setLayout(null);
setBounds(100, 100, 300, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txtRes = new JTextArea();
txtRes.setBorder(new BevelBorder(BevelBorder.LOWERED));
txtRes.setLineWrap(true);
txtRes.setBackground(new Color(255, 255, 255));
txtRes.setEditable(false);
txtRes.setBounds(10, 10, 270, 150);
getContentPane().add(txtRes);
txtSend = new JTextArea();
txtSend.setBorder(new BevelBorder(BevelBorder.LOWERED));
txtSend.addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent ke) {
if(ke.getKeyCode()==KeyEvent.VK_ENTER ke.isControlDown()){
sendMess();
}
}
});
txtSend.setLineWrap(true);
txtSend.setBackground(new Color(255, 255, 255));
txtSend.setBounds(10, 166, 270, 115);
getContentPane().add(txtSend);
final JButton btnSend = new JButton();
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
sendMess();
}
});
btnSend.setText("发送...");
btnSend.setBounds(184, 287, 84, 28);
getContentPane().add(btnSend);
//
new Thread(this).start();
}
private void sendMess(){
try {
client = new Socket(Zjh.IP,Cxr.PORT);
ps = new PrintStream(client.getOutputStream());
ps.println(txtSend.getText());
txtSend.setText("");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
server = new ServerSocket(Zjh.PORT);
while(true){
client = server.accept();
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
txtRes.insert("Cxr say:"+br.readLine()+"\r\n", 0);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.
package com.yl.testqq;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java点虐 .ServerSocket;
import java点虐 .Socket;
import java点虐 .UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;
public class Cxr extends JFrame implements Runnable {
private final JTextArea txtRes;
private final JTextArea txtSend;
public static final int PORT = 5555;
private ServerSocket server;
private BufferedReader br;
private static final String IP = "localhost";
private PrintStream ps;
private Socket client;
// private Socket client;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cxr frame = new Cxr();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public Cxr() {
super();
setTitle("Zjh to Cxr");
getContentPane().setBackground(new Color(176, 224, 230));
getContentPane().setLayout(null);
setBounds(100, 100, 300, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txtRes = new JTextArea();
txtRes.setBorder(new BevelBorder(BevelBorder.LOWERED));
txtRes.setLineWrap(true);
txtRes.setBackground(new Color(255, 245, 238));
txtRes.setEditable(false);
txtRes.setBounds(10, 10, 270, 150);
getContentPane().add(txtRes);
txtSend = new JTextArea();
txtSend.setBorder(new BevelBorder(BevelBorder.LOWERED));
txtSend.addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent ke) {
if(ke.getKeyCode()==KeyEvent.VK_ENTER ke.isControlDown()){
sendMess();
}
}
});
txtSend.setLineWrap(true);
txtSend.setBackground(new Color(255, 245, 238));
txtSend.setBounds(10, 166, 270, 115);
getContentPane().add(txtSend);
final JButton btnSend = new JButton();
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
sendMess();
}
});
btnSend.setText("发送...");
btnSend.setBounds(205, 287, 75, 28);
getContentPane().add(btnSend);
// final JButton button = new JButton();
// button.addActionListener(new ActionListener() {
// public void actionPerformed(final ActionEvent arg0) {
// txtSend.setForeground(Color.RED);
// txtRes.setForeground(Color.RED);
// }
// });
// button.setText("红字体");
// button.setBounds(20, 287, 106, 28);
// getContentPane().add(button);
//
new Thread(this).start();
}
private void sendMess(){
try {
client = new Socket(Cxr.IP,Zjh.PORT);
ps = new PrintStream(client.getOutputStream());
ps.println(txtSend.getText());
txtSend.setText("");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
server = new ServerSocket(Cxr.PORT);
while (true){
client = server.accept();
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
txtRes.insert("Zjh say:"+br.readLine()+"\r\n", txtRes.getRows());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务端
package test;
import java点虐 .*;
import java.io.*;
public class Server {
private ServerSocket ss;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public Server() {
try {
ss = new ServerSocket(10000);
socket = ss.accept();
int i=0;
while (true) {
i++;
System.out.println("server "+i);
String RemoteIP = socket.getInetAddress().getHostAddress();
String RemotePort = ":" + socket.getLocalPort();
System.out.println("+A client come in!IP:" + RemoteIP
+ RemotePort);
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
String line = in.readLine();
System.out.println("++Cleint send is :" + line);
out = new PrintWriter(socket.getOutputStream(), true);
out.println("+++Your Message Received!");
// out.close();
// in.close();
// socket.close();
}
} catch (IOException e) {
out.println("++++wrong");
}
System.out.println("服务端不会结束");
}
public static void main(String[] args) {
new Server();
}
};
客户端
package test;
import java.io.*;
import java点虐 .*;
public class Client {
Socket socket;
BufferedReader in;
PrintWriter out;
public Client() {
try {
System.out.println("-Try to Connect to 127.0.0.1:10000");
socket = new Socket("127.0.0.1", 10000);
System.out.println("--The Server Connected!");
System.out.println("---Please enter some Character:");
BufferedReader line;
int i=0;
while(true){
i++;
System.out.println("client "+i);
line = new BufferedReader(new InputStreamReader(
System.in));
out = new PrintWriter(socket.getOutputStream(), true);
out.println(line.readLine());
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
System.out.println("----"+in.readLine());
// out.close();
// in.close();
// socket.close();
}
} catch (IOException e) {
out.println("-----Wrong");
}
System.out.println("客户端不会结束");
}
public static void main(String[] args) {
new Client();
}
};