Java single-threaded communication program based on TCP protocol

Source: Internet
Author: User
Tags event listener gettext

This is a small program written in the Sophomore Java Foundation, by setting the host IP and port in the program to achieve communication between the two hosts. Since the specification of Java programming was not very good at the time,

So the program interface design code and the communication module code are placed in the same class.

Class One: server-side code

Import Java.awt.BorderLayout;
Import Java.awt.Container;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import Javax.swing.JScrollPane;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.JTextArea;
Import Javax.swing.JTextField;

public class MyServer {
JFrame frame;
JButton but;
JButton but1;
JTextField TF;
JTextArea Ta;
JPanel pane;
BufferedReader BR;
BufferedWriter bw;
Public MyServer () {
Frame=new JFrame ("server");
But=new JButton ("send");
But1=new JButton ("close");
Tf=new JTextField (15);
Ta=new JTextArea (5,15);
JPanel pane=new JPanel ();
Pane.add (But);
Pane.add (BUT1);
Container C=frame.getcontentpane ();
C.setlayout (New BorderLayout ());
C.add (New JScrollPane (TA), Borderlayout.north);
C.add (Pane,borderlayout.center);
C.add (Tf,borderlayout.south);
Frame.setsize (300, 200);
Frame.setvisible (TRUE);
Ta.setenabled (FALSE);
/*

The above section is the code of the interface design

*/
But.addactionlistener (new ActionListener () {//Add event listener to send button
public void actionperformed (ActionEvent event) {
try {
if (Tf.gettext () ==null| | Tf.gettext () = = "")//Determine if the user has entered text in the text box
{bw.newline ();//This is a newline character, because a stream of characters with a cache (for example, BufferedWriter) is a marker that ends with a newline character as each line of data, so

Without this line of code, the client cannot receive the information sent by the user until the server end runs.
Bw.flush ();//This code means emptying the cache, the role is to enable the user to write the information can be sent out in time, because the user did not enter text, so the client received a space
Display ("Myself:" + tf.gettext ());//Displays the sent information in the display box
}
Else
{
Bw.write (Tf.gettext ());
Bw.newline ();
Bw.flush ();
Display ("Myself:" + tf.gettext ());

}
Tf.settext ("");
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}
});

But1.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent arg0) {
System.exit (0);

}

});
}

public void display (String s) {
Ta.append (s + "\ n");
Ta.setcaretposition (Ta.gettext (). Length ());
Tf.requestfocusinwindow ();
}
public void Run () throws exception{
ServerSocket server=new ServerSocket (5000);//listening on port 5000
Socket s=server.accept ();//Start monitoring service
while (true) {
Bw=new BufferedWriter (New OutputStreamWriter (S.getoutputstream ()));
Br=new BufferedReader (New InputStreamReader (S.getinputstream ()));
while (Br.read ()!=-1) {
Display ("Client:" + br.readline ());
}
S.close ();
}

}
public static void Main (string[] args) throws exception{
MyServer app=new MyServer ();
App.run ();

}

}

Class Two: The client code, the client code and server-side code is basically the same, but the server side more ServerSocket server=new ServerSocket (5000);
Socket s=server.accept (); These two lines of code are used to provide a listening service

Import Java.awt.BorderLayout;
Import Java.awt.Container;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
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.JTextArea;
Import Javax.swing.JTextField;

public class MyClient extends thread{
JFrame frame;
JButton but;
JTextField TF;
JTextArea Ta;
JPanel pane;
BufferedReader BR;
BufferedWriter bw;
Private JButton but1;
Public MyClient () {
Frame=new JFrame ("Client");
But=new JButton ("send");
But1=new JButton ("close");
Tf=new JTextField (15);
Ta=new JTextArea (5,15);
JPanel pane=new JPanel ();
Pane.add (But);
Pane.add (BUT1);
Container C=frame.getcontentpane ();
C.setlayout (New BorderLayout ());
C.add (New JScrollPane (TA), Borderlayout.north);
C.add (Pane,borderlayout.center);
C.add (Tf,borderlayout.south);
Frame.setsize (300, 200);
Frame.setvisible (TRUE);
Ta.setenabled (FALSE);

But.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent event) {
try {
if (Tf.gettext () ==null| | Tf.gettext () = = "")
{Bw.newline ();
Bw.flush ();
Display ("Myself:" + tf.gettext ());
}
Else
{
Bw.write (Tf.gettext ());
Bw.newline ();
Bw.flush ();
Display ("Myself:" + tf.gettext ());
}
Tf.settext ("");
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}
});

But1.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent arg0) {
System.exit (0);

}

});
}

public void display (String s) {
Ta.append (s + "\ n");
Ta.setcaretposition (Ta.gettext (). Length ());
Tf.requestfocusinwindow ();
}
public void Run () {

Socket s;
try {
s = new Socket ("localhost", 5000);
while (true) {
Bw=new BufferedWriter (New OutputStreamWriter (S.getoutputstream ()));
Br=new BufferedReader (New InputStreamReader (S.getinputstream ()));
while (Br.read ()!=-1) {
Display ("Server:" + br.readline ());
}
S.close ();
}

} catch (Unknownhostexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}


}
public static void Main (string[] args) throws exception{
MyClient app=new myclient ();
//
App.start ();//app.run ();
}

}

--------------------------------------------------------------------------------------------------------------- -------------------

Since this is the code written a year ago, now there is only a general impression of the relevant knowledge, the description of the details may be biased, you are welcome to correct

Java single-threaded communication program based on TCP protocol

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.