Java Basic Learning Notes Java socket+swing design simple communication

Source: Internet
Author: User

In the original book "Java from Beginner to proficient (3rd edition)", the client can only send data once, I modified a little bit on this basis, and realized the one-way communication of sending data multiple times.

1. Server-side
Package Tcp_ip;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.inputstreamreader;import Java.net.serversocket;import Java.net.socket;import Java.sql.date;import Java.sql.time;import Java.text.simpledateformat;import Java.util.simpletimezone;import Com.sun.org.apache.bcel.internal.generic.NEW; Import Javafx.beans.binding.when;import Io_test.bufferread;public class My_Server {private BufferedReader reader; Private ServerSocket ser_sock;private Socket socket;void my_server () {//TODO auto-generated constructor Stubtry {SER_SOC K = new ServerSocket (8766); System.out.println ("Waiting for Customer Connection"); Socket = ser_sock.accept (); reader = new BufferedReader (New InputStreamReader (Socket.getinputstream ())); while (reader! =null) {SimpleDateFormat forma = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); SYSTEM.OUT.PRINTLN ("Connection succeeded:" +forma.format (New Java.util.Date ())); SYSTEM.OUT.PRINTLN ("Get Client Information for:" +reader.readline ());} Reader.close ();} catch (IOException e) {//TODO auto-generated catch blocke.printstAcktrace ();} }public static void Main (string[] args) {//TODO auto-generated method Stubmy_server sv = new My_Server (); Sv. My_Server ();}}

2. Client
Package Tcp_ip;import Java.awt.borderlayout;import Java.awt.container;import java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.io.ioexception;import Java.io.printwriter;import Java.net.Socket;import Java.net.unknownhostexception;import Javax.swing.jframe;import Javax.swing.jscrollpane;import Javax.swing.jtextarea;import Javax.swing.jtextfield;import Javax.swing.border.bevelborder;public class My_Client Extends Jframe{socket socket;private jtextarea ta = new JTextArea ();p rivate jtextfield tf = new JTextField ();p rivate Print Writer writer; Container c;public my_client (String titl) {//TODO auto-generated constructor Stubsuper (TITL); setdefaultcloseoperation (exit_on_close); c = Getcontentpane (); final JScrollPane Scollpane = new JScrollPane (); Scollpane.setborder (new BevelBorder (bevelborder.raised)); Getcontentpane (). Add (Scollpane, borderlayout.center); Scollpane.setviewportview (TA); C.add (TF, "South"); Ta.append ("Attempt to connect \ n"); try {socket = new socket ("127.0.0.1", 8766);} catCH (unknownhostexception E1) {//Todo auto-generated catch Blocke1.printstacktrace ();} catch (IOException E1) {//Todo Aut O-generated catch Blocke1.printstacktrace ();} Ta.append ("Connected, please enter send message \ n"); Tf.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {// TODO Auto-generated method Stubtry {writer = new PrintWriter (Socket.getoutputstream (), true); Writer.println (Tf.gettext ()); Ta.append (Tf.gettext () + "\ n"), Ta.setselectionend (Tf.gettext (). Length ()); Tf.settext ("");} catch (IOException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();}});} public static void Main (string[] args) {//TODO auto-generated method Stubmy_client client = new My_client ("Connection Server"); . SetSize (+); client.setvisible (True);}}

3. Operation effect








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Basic Learning Notes Java socket+swing design simple communication

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.