(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.net.ServerSocket
import java.net.Socket
import java.net.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.net.ServerSocket
import java.net.Socket
import java.net.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()
}
}
}
主要分聊天室服务器和聊天室客户端两部分。实现思路如下:聊天室服务器:
1.用户信息链表类:实现对用户信息的添加,删除和查询的功能;
2.服务器主框架类:实现人机交互,启动服务,关闭服务和发送系统信息等;
3.服务器侦听线程类:侦听客户端的连接,响应客户端的登录请求,为登录用户启动专门线程及处理与此用户相关的 *** 作;
4.服务器接收线程类:接收客户端发送来的信息,对信息进行分析,执行相应的 *** 作。其中客户端信息分为聊天信息和下线请求。
聊天室客户端:
1.客户端主框架类:实现人机交互,用户登录,用户下线和聊天戚老信息的发送;
2.客户端接收线程类:接收高山升服务器发送来的信息,对信息进行分析及执行相应的 *** 作。客户端接收到的信息分三种,分别是聊天信息和系统信息,用户列表信息和唯清服务关闭信息。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)