Java--socket Communication (bidirectional, interface) __java

Source: Internet
Author: User

Service side:

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;
	Display the text box of the content, enter the text box of the content, send the content button private JScrollPane Pane_showwindow;
	Private JScrollPane Pane_inputwindow;
	Private JTextArea Area_showwindow;
	
	Private JTextArea Area_inputwindow;

	Private JButton btn_send;
		Private Dimension dimension;//is used to set Area_showwindow drag size//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 Side");
		int width = (int) toolkit.getdefaulttoolkit (). Getscreensize (). GetWidth ();
		int height = (int) toolkit.getdefaulttoolkit (). Getscreensize (). GetHeight ();
		Frame.setbounds (WIDTH/2, HEIGHT/2, 400, 450);
	Frame.setvisible (TRUE);
		}//Content display text box and input text box private void Initchattextarea () {//Get view Focus Pane_showwindow.getviewport (). Add (Area_showwindow);
		Pane_inputwindow.getviewport (). Add (Area_inputwindow);
		Set the display text field to not editable area_showwindow.seteditable (false); Set the size of the display text field to drag pAne_showwindow.setminimumsize (dimension);
	Frame.add (Pane_center, Borderlayout.center);
		//Send the file, send the content button public void Initbutton () {pane_buttom.add (btn_send);
	Frame.add (Pane_buttom, Borderlayout.south); private void Btn_send () {Btn_send.addactionlistener (new ActionListener () {@Override public void Actionperfo
				Rmed (ActionEvent e) {String info = Area_inputwindow.gettext ();
				Area_showwindow.append ("Service side:" +info+ "\ r \ n");
				PW.PRINTLN (info);
			Area_inputwindow.settext ("");
	}
		});
		private void socket () {ServerSocket ss;
			try {ss = new ServerSocket (9988);
			Wait for connection client Socket s=ss.accept ();
			InputStreamReader isr=new InputStreamReader (S.getinputstream ());
			BufferedReader br=new BufferedReader (ISR);

			PrintWriter must have a close relationship with the socket pw=new PrintWriter (S.getoutputstream (), true);
				Read information sent from the client while (true) {//Read the message from the client String info=br.readline ();
			Display area_showwindow.append in the text bar ("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;
	Display the text box of the content, enter the text box of the content, send the content button private JScrollPane Pane_showwindow;
	Private JScrollPane Pane_inputwindow;
	Private JTextArea Area_showwindow;

	Private JTextArea Area_inputwindow;

	Private JButton btn_send;
		Private Dimension dimension;//is used to set Area_showwindow drag size//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.getdefaulttoolkit (). Getscreensize (). GetWidth ();
		int height = (int) toolkit.getdefaulttoolkit (). Getscreensize (). GetHeight ();
		Frame.setbounds (WIDTH/2, HEIGHT/2, 400, 450);
	Frame.setvisible (TRUE);
		}//Content display text box and input text box private void Initchattextarea () {//Get view Focus Pane_showwindow.getviewport (). Add (Area_showwindow);
		Pane_inputwindow.getviewport (). Add (Area_inputwindow);
		Set the display text field to not editable area_showwindow.seteditable (false); Sets the size of the display text field to drag Pane_shoWwindow.setminimumsize (dimension);
	Frame.add (Pane_center, Borderlayout.center);
		//Send the file, send the content button public void Initbutton () {pane_buttom.add (btn_send);
	Frame.add (Pane_buttom, Borderlayout.south); private void Btn_send () {Btn_send.addactionlistener (new ActionListener () {@Override public void actionperfor
				Med (actionevent e) {String info = Area_inputwindow.gettext ();
				Area_showwindow.append ("Client:" +info+ "\ r \ n");
				PW.PRINTLN (info);
			Area_inputwindow.settext ("");
	}
		});
			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 the information sent from the server side String Info=br.readline ();
			Area_showwindow.append ("Service side:" +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.