Java-socket communication (two-way, with interface)

Source: Internet
Author: User

 

Server:

 

Import Java. AWT. borderlayout; import Java. AWT. dimension; import Java. AWT. toolkit; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; 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. jsplitpane; import javax. swing. jtextarea; public class chatframeserver {private printwriter PW; private jframe frame; private jpanel pane_buttom; private jsplitpane pane_center; // text box for displaying content and text box for inputting content, send content buttons: Private jscrollpane pane_showwindow; private jscrollpane pane_inputwindow; private jtextarea area_showwindow; private jtextarea area_inputwindow; private jbutton btn_send; private dimension; // used to set the drag size of area_showwindow // initialize public chatframeserver () {frame = new jframe (); pane_buttom = new jpanel (); pane_showwindow = new jscrollpane (); pane_inputwindow = new jscrollpane (); area_showwindow = new jtextarea (); area_inputwindow = new jtextarea (); pane_center = new jsplitpane (jsplitpane. vertical_split, false, pane_showwindow, pane_inputwindow); btn_send = new jbutton ("send"); dimension = new dimension (50,300);} // call method display window public void showframe () {initframe (); initchattextarea (); initbutton (); btn_send (); socket () ;}// main form public void initframe () {frame. settitle ("server"); int width = (INT) toolkit. getdefatooltoolkit (). getscreensize (). getwidth (); int Height = (INT) toolkit. getdefatooltoolkit (). getscreensize (). getheight (); frame. setbounds (width/2, height/2,400,450); frame. setvisible (true);} // The content display text box and the input content text box private void initchattextarea () {// obtain the view focus pane_show1_getviewport (). add (area_showwindow); pane_input?#getviewport (). add (area_inputwindow); // set the display text field to uneditable area_show00000000seteditable (false); // set the size of the display text field to be dragged pane_show00000000setminimumsize (dimension); frame. add (pane_center, borderlayout. center);} // send the file. The send content button is public void initbutton () {pane_buttom.add (btn_send); frame. add (pane_buttom, borderlayout. south);} private void btn_send () {listener (New actionlistener () {@ overridepublic void actionreceivmed (actionevent e) {string info = area_input0000gettext (); area_show00000000append ("server: "+ info +" \ r \ n "); PW. println (Info); area_input00000000settext ("") ;}}) ;}private void socket () {serversocket SS; try {Ss = new serversocket (9988 ); // wait for the client to connect socket S = ss. accept (); inputstreamreader ISR = new inputstreamreader (S. getinputstream (); bufferedreader BR = new bufferedreader (ISR); // The printwriter must be closely related to the socket PW = new printwriter (S. getoutputstream (), true); // read the information sent from the client method while (true) {// read the information sent from the client string info = BR. readline (); // display area_show00000000append ("client:" + info + "\ r \ n") ;}} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {chatframeserver chat = new chatframeserver (); chat. showframe ();}}

 

Client:

 

Import Java. AWT. borderlayout; import Java. AWT. dimension; import Java. AWT. toolkit; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. io. printwriter; import java.net. socket; import java.net. unknownhostexception; import javax. swing. jbutton; import javax. swing. jframe; import javax. swing. jpanel; import javax. swing. jscrollpane; import javax. swing. jsplitpane; import javax. swing. jtextarea; public class chatframe {private printwriter PW; private jframe frame; private jpanel pane_buttom; private jsplitpane pane_center; // text box for displaying content, text box for inputting content, send content buttons: Private jscrollpane pane_showwindow; private jscrollpane pane_inputwindow; private jtextarea area_showwindow; private jtextarea area_inputwindow; private jbutton btn_send; private dimension; // used to set the size of area_showwindow to be dragged // initialize public chatframe () {frame = new jframe (); pane_buttom = new jpanel (); pane_showwindow = new jscrollpane (); pane_inputwindow = new jscrollpane (); area_showwindow = new jtextarea (); area_inputwindow = new jtextarea (); pane_center = new jsplitpane (jsplitpane. vertical_split, false, pane_showwindow, pane_inputwindow); btn_send = new jbutton ("send"); dimension = new dimension (50,300);} // call method display window public void showframe () {initframe (); initchattextarea (); initbutton (); btn_send (); socket () ;}// main form public void initframe () {frame. settitle ("client"); int width = (INT) toolkit. getdefatooltoolkit (). getscreensize (). getwidth (); int Height = (INT) toolkit. getdefatooltoolkit (). getscreensize (). getheight (); frame. setbounds (width/2, height/2,400,450); frame. setvisible (true);} // The content display text box and the input content text box private void initchattextarea () {// obtain the view focus pane_show1_getviewport (). add (area_showwindow); pane_input?#getviewport (). add (area_inputwindow); // set the display text field to uneditable area_show00000000seteditable (false); // set the size of the display text field to be dragged pane_show00000000setminimumsize (dimension); frame. add (pane_center, borderlayout. center);} // send the file. The send content button is public void initbutton () {pane_buttom.add (btn_send); frame. add (pane_buttom, borderlayout. south);} private void btn_send () {listener (New actionlistener () {@ overridepublic void actionreceivmed (actionevent e) {string info = area_input0000gettext (); area_show00000000append ("client: "+ info +" \ r \ n "); PW. println (Info); area_input00000000settext ("") ;}});} private void socket () {try {socket S = new socket ("127.0.0.1", 9988 ); inputstreamreader ISR = new inputstreamreader (S. getinputstream (); bufferedreader BR = new bufferedreader (ISR); PW = new printwriter (S. getoutputstream (), true); While (true) {// continuously reads information sent from the Server String info = BR. readline (); area_show00000000append ("server:" + info + "\ r \ n") ;}} catch (unknownhostexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {chatframe chat = new chatframe (); chat. showframe ();}}

 

 

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.