(3)java网络---半双工、全双工
1.半双工
1.1半双工原理:半双工即Half duplex Communication,是指在通信过程的任意时刻,信息既可由A传到B,又能由B传A,但只能有一个方向上的传输存在。采用半双工方式时,通信系统每一端的发送器和接收器,通过收/发开关转接到通信线上,进行方向的切换,因此,会产生时间延迟。收/发开关实际上是由软件控制的电子开关。
1.2工程结构
1.3服务器端
/**
* 服务器端,在9999端口监听,可以通过控制台,回送给客户端信息
*/
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class myserver2 {
public myserver2 () {
try {
//监听端口,9999
ServerSocket ss=new ServerSocket(9999);
//阻塞,等待连接
Socket s=ss.accept();
//先接受到客户端发来的信息
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
InputStreamReader isr2=new InputStreamReader(System.in);
BufferedReader br2=new BufferedReader(isr2);
while(true){
String infoFromClient=br.readLine();
System.out.println("客户端:"+infoFromClient);
if(infoFromClient.equals("bye")){
s.close();
break;
}
//接受从控制台输入的信息
System.out.println("请输入您希望对客户端说的话:");
String re=br2.readLine();
//把从控制台接受的信息,回送给客户端
pw.println(re);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new myserver2();
}
}
1.4客户端
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class myclient2 {
public myclient2 () {
try {
//连接服务器端
Socket s=new Socket("127.0.0.1",9999);
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
InputStreamReader isr2=new InputStreamReader(s.getInputStream());
BufferedReader br2=new BufferedReader(isr2);
while(true){
System.out.println("请输入您要对服务器要说的话:");
//客户端先从控制台接受
String info=br.readLine();
pw.println(info);
if(info.equals("bye")){
System.out.println("通话结束!");
s.close();
break;
}
//接受从服务器发来的信息
String res=br2.readLine();
System.out.println("服务器端:"+res);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new myclient2();
}
}
2.全双工
全双工:全双工(Full Duplex)是通讯传输的一个术语。通信允许数据在两个方向上同时传输,它在能力上相当于两个单工通信方式的结合。全双工指可以同时(瞬时)进行信号的双向传输(A→B且B→A)。指A→B的同时B→A,是瞬时同步的。
2.1工程目录结构:
/** * 服务器端 */package com.test;
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;
import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.WindowConstants;
public class myserver3 extends JFrame implements ActionListener, KeyListener {PrintWriter pw=null;JTextArea jt_aArea=new JTextArea();JTextField jTextField=new JTextField(10);JButton btn_send=new JButton("发送");JPanel jp1=new JPanel();JScrollPane jsp=new JScrollPane(jt_aArea);public myserver3 () {jp1.add(jTextField);jp1.add(btn_send);btn_send.addActionListener(this);jTextField.addKeyListener(this);this.add(jsp,"Center");this.add(jp1,"South");this.setTitle("简易服务器端");this.setSize(300, 300);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);try {//服务器监听ServerSocket ss=new ServerSocket(9999);//等待客户端连接Socket s=ss.accept();InputStreamReader isr=new InputStreamReader(s.getInputStream());BufferedReader br=new BufferedReader(isr);pw=new PrintWriter(s.getOutputStream(),true);//读取从客户端发送来的信息while(true){String info =br.readLine();jt_aArea.append("客户端:"+info+"\r\n");}} catch (Exception e) {// TODO: handle exception}}@Overridepublic void keyTyped(KeyEvent e) {// TODO Auto-generated method stubif(e.getSource()==jTextField){//System.out.println(e.getKeyCode());if(e.getKeyCode()==KeyEvent.VK_ENTER){String info=jTextField.getText();jt_aArea.append("服务端:"+info+"\r\n");pw.println(info);//清空内存jTextField.setText("");}}}@Overridepublic void keyReleased(KeyEvent e) {// TODO Auto-generated method stub}@Overridepublic void keyPressed(KeyEvent e) {// TODO Auto-generated method stub}public void actionPerformed(ActionEvent arg) {//如果用户按下发送信息按钮if(arg.getSource()==btn_send){String info=jTextField.getText();jt_aArea.append("服务器:"+info+"\r\n");pw.println(info);//清空内存jTextField.setText("");}}public static void main(String[] args) {// TODO Auto-generated method stubnew myserver3();}
}///////////////////////////////////////////////////////////////////////////////////////////////////////////////客户端:
package com.test;
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;
import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.WindowConstants;
public class myclient3 extends JFrame implements ActionListener, KeyListener{JTextArea jt_aArea=new JTextArea();JTextField jTextField=new JTextField(10);JButton btn_send=new JButton("发送");PrintWriter pw=null;JPanel jp1=new JPanel();JScrollPane jsp=new JScrollPane(jt_aArea);public myclient3 () {jp1.add(jTextField);jp1.add(btn_send);btn_send.addActionListener(this);jTextField.addKeyListener(this);this.add(jsp,"Center");this.add(jp1,"South");this.setTitle("简易客户端");this.setSize(300, 300);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);try {Socket s=new Socket("127.0.0.1",9999);InputStreamReader isr=new InputStreamReader(s.getInputStream());BufferedReader br=new BufferedReader(isr);pw=new PrintWriter(s.getOutputStream(),true);while(true){//不停的读取String info=br.readLine();jt_aArea.append("服务器端:"+info+"\r\n");//换行//jt_aArea.setText(info);}} catch (Exception e) {// TODO: handle exception}}public void keyTyped(KeyEvent e) {// TODO Auto-generated method stubif(e.getSource()==jTextField){//System.out.println(e.getKeyCode());if(e.getKeyCode()==KeyEvent.VK_ENTER){String info=jTextField.getText();jt_aArea.append("客户端:"+info+"\r\n");pw.println(info);//清空内存jTextField.setText("");}}}@Overridepublic void keyReleased(KeyEvent e) {// TODO Auto-generated method stub}@Overridepublic void keyPressed(KeyEvent e) {// TODO Auto-generated method stub}public void actionPerformed(ActionEvent arg) {//如果用户按下发送信息按钮if(arg.getSource()==btn_send){String info=jTextField.getText();jt_aArea.append("客户端:"+info+"\r\n");pw.println(info);//清空内存jTextField.setText("");}}public static void main(String[] args) {// TODO Auto-generated method stubnew myclient3();}
}
先启动服务端,在启动客户端运行效果: