Java sockets for full-duplex communication

Source: Internet
Author: User
Tags gettext

Java sockets for full-duplex communication

Simplex, half-duplex, and full-duplex definitions

If at any point in the communication process, the information can only be transmitted by a Party A to the other party B, it is called Simplex.

If at any time, the information can be transmitted from a to B, but also by a B, but only in one direction of the transmission exists, called half-duplex transmission.

If at any point there is a bidirectional signal transmission of A to B and B to a on the line, it is called full duplex.

A simple interface with swing enables full-duplex communication between client and server.

Server-side code:

Package Cn.cblue.net1;import Java.awt.borderlayout;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.ioexception;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;/** * @Description: Service side * @author HUANGZJB [email protected] * @Company Digital China * @date 2014-6-4 pm 02:18:06 * @versio N 1.0 */public Class MyServer extends JFrame implements ActionListener {JTextArea JTA = null; JTextField JTF = null; JButton JB = null; JPanel JP1 = null; JScrollPane jsp = null; PrintWriter pw = null;public static void Main (string[] args) {new MyServer ();} Public MyServer () {JTA = new JTextArea (); jsp = new JScrollPane (JTA); JTF = new JTextField (10);Register Carriage return Event Jtf.addkeylistener (new KeyListener () {@Overridepublic void keytyped (KeyEvent e) {} @Overridepublic void keyreleased (KeyEvent e) {} @Overridepublic void keypressed (KeyEvent e) {if (e.getkeycode () = = Keyevent.vk_enter) { SendMessage ();}}); JB = new JButton ("send"); Jb.addactionlistener (this);//Register a listener event JP1 = new JPanel (); Jp1.add (JTF); Jp1.add (JB); This.add (JSP , borderlayout.center); This.add (JP1, Borderlayout.south); This.settitle ("Chat server"); This.setsize (300, 200); This.setvisible (TRUE);//Receive messages sent over from the client try {serversocket SS = new ServerSocket (2222); Socket s = ss.accept (); BufferedReader in = new BufferedReader (New InputStreamReader (S.getinputstream ()));p w = new PrintWriter ( S.getoutputstream (), true);//write PW = new PrintWriter (S.getoutputstream ()) There will be a problem while (true) {String info = in.readline (); String str = null;if (jta.gettext () = = NULL | | "". Equals (Jta.gettext ())) {str = "Server:" + info;} else {str = Jta.gettext () + "\r\nserver:" + info;} Jta.settext (str); Jta.setcaretposition (jta.getdocumenT (). GetLength ());}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Send information to client @overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated method Stubif (e.getsource () = = JB) {sendMessage ();}} /** * @Description: Send message to client * @throws */public void SendMessage () {String info = jtf.gettext ();p w.println (info);p W.flush ( ); Jtf.settext (""); if (jta.gettext () = = NULL | | "". Equals (Jta.gettext ())) {Jta.append ("Client:" + info);} else {jta.append ("\r\nclient:" + info);} Jta.setcaretposition (Jta.getdocument (). GetLength ());//Set scroll bar auto-scroll}}

Client-side code:

Package Cn.cblue.net1;import Java.awt.borderlayout;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.ioexception;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;/** * @Description: Client * @author HUANGZJB [email protected] * @Company Digital China * @date 2014-6-4 PM 02:18:24 * @version 1.0 */public Class Myclien T extends JFrame implements ActionListener {JTextArea JTA = null; JTextField JTF = null; JButton JB = null; JPanel JP1 = null; JScrollPane jsp = null; PrintWriter pw = null;public static void Main (string[] args) {new myclient ();} Public myclient () {JTA = new JTextArea (); jsp = new JScrollPane (JTA); JTF = new JTextField (10);//Register Carriage return event Jtf.addkeylistener (nEW KeyListener () {@Overridepublic void keytyped (KeyEvent e) {} @Overridepublic void keyreleased (KeyEvent e) {}@ overridepublic void keypressed (KeyEvent e) {if (e.getkeycode () = = Keyevent.vk_enter) {sendMessage ();}}); JB = new JButton ("send"); Jb.addactionlistener (this);//Register a listener event JP1 = new JPanel (); Jp1.add (JTF); Jp1.add (JB); This.add (JSP , borderlayout.center); This.add (JP1, Borderlayout.south); This.settitle ("chat client"); This.setsize (300, 200); This.setvisible (TRUE);//Receive a message sent from the server try {socket s = new socket ("127.0.0.1", 2222); BufferedReader in = new BufferedReader (New InputStreamReader (S.getinputstream ()));p w = new PrintWriter ( S.getoutputstream (), true); while (true) {String info = in.readline (); String str = null;if (jta.gettext () = = NULL | | "". Equals (Jta.gettext ())) {str = "Server:" + info;} else {str = Jta.gettext () + "\r\nserver:" + info;} Jta.settext (str); Jta.setcaretposition (Jta.getdocument (). GetLength ());}} catch (IOException e) {e.printstacktrace ();}} Give the message to the server side @overridepublic void actioNperformed (ActionEvent e) {if (e.getsource () = = JB) {sendMessage ();}} /** * @Description: Send message to server side * @throws */public void SendMessage () {String info = jtf.gettext ();p w.println (info);p W.flush (); Jtf.settext (""); if (jta.gettext () = = NULL | | "". Equals (Jta.gettext ())) {Jta.append ("Client:" + info);} else {jta.append ("\r\nclient:" + info);} Jta.setcaretposition (Jta.getdocument (). GetLength ());//Set scroll bar auto-scroll}}

Java socket for full-duplex communication

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.