Tcp program design-the client obtains the input and output streams of the server, and the tcp Program Design

Source: Internet
Author: User

Tcp program design-the client obtains the input and output streams of the server, and the tcp Program Design
Tcp Program Design -- the client obtains the input and output streams of the server.

Ideas:

Step 1: instantiate a ServerSocket object (server socket) to wait for requests on the Network (that is, the socket waiting for connection)

Step 2: Call the accept () method to return a socket object connected to the client socket object

Step 3: the output stream obtained by the server socket object using the getOutputStream method will point to the input stream obtained by the client socket object using getInputStream, and vice versa.

Server code:

1 public class Server {2 public static void main (String [] args) {3 try {4 ServerSocket ss = new ServerSocket (8001); 5 while (! Ss. isClosed () {6 Socket s = ss. accept (); 7 OutputStream ops = s. getOutputStream (); 8 String str = "Welcome to program \ n write TCP server program," 9 + "Create a ServerSocket" 10 + "Object waiting on port 8001, after receiving a connection request from a client, the "11 +" program obtains the input and output "12 +" stream from the Socket object connected to the client. Sends information to the client through the output. "; 13 ops. write (str. getBytes (); // convert the string into a byte array and write it to 14 ops. close (); 15 s. close (); 16} 17 ss. close (); 18} catch (IOException e) {19 e. printStackTrace (); 20} 21} 22}

Client code:

1 public class Client extends JFrame {2 3 private static final long serialVersionUID = 1L; 4 private JTextArea textArea; 5 private JTextField portField; 6 private JTextField hostField; 7 8 public static void main (String args []) {9 10 Client frame = new Client (); 11 frame. setVisible (true); 12} 13 14 public Client () {15 super (); 16 setBounds (100,100,500,212); 17 setdefaclocloseoperation (JFrame. EXIT _ ON_CLOSE); 18 19 final JPanel = new JPanel (); 20 panel. setLayout (new BoxLayout (panel, BoxLayout. x_AXIS); 21 getContentPane (). add (panel, BorderLayout. NORTH); 22 23 final JLabel label = new JLabel (); 24 label. setText ("connection HOST:"); 25 panel. add (label); 26 27 hostField = new JTextField (); 28 hostField. setText ("localhost"); 29 panel. add (hostField); 30 31 final JLabel label_1 = new JLabel (); 32 label_1.setText ("Port:"); 33 panel. add (label_1); 34 35 portField = new JTextField (); 36 portField. setText ("8001"); 37 panel. add (portField); 38 39 final JButton button = new JButton (); 40 button. addActionListener (new ActionListener () {41 public void actionreceivmed (final ActionEvent e) {42 final String hostName = hostField. getText (); 43 String portNum = portField. getText (); 44 final int port = Integer. parseInt (portNum); 45 t Ry {46 final InetAddress host = InetAddress. getByName (hostName); // instantiate the InetAddress object 47 Socket socket = new Socket (host, port) of the corresponding host; // instantiate socket48 final InputStream is = socket. getInputStream (); // getoutputstream49 InputStreamReader reader = new InputStreamReader (is); 50 int data = 0; 51 while (data = reader. read ())! =-1) {52 textArea. append (char) data + ""); 53} 54} catch (Exception e1) {55 textArea. append (e1.toString (); 56 e1.printStackTrace (); 57} 58} 59}); 60 button. setText ("connection"); 61 panel. add (button); 62 63 final JScrollPane scrollPane = new JScrollPane (); 64 getContentPane (). add (scrollPane, BorderLayout. CENTER); 65 66 textArea = new JTextArea (); 67 textArea. setLineWrap (true); // automatically wrap 68 scrollPane. setViewportView (textArea); 69} 70}

The result is as follows:

Note:

BoxLayout box Layout
BoxLayout can arrange the controls horizontally or vertically in sequence, which is determined by the X_AXIS and Y_AXIS parameters. X_AXIS indicates horizontal arrangement, while Y_AXIS indicates vertical arrangement. The constructor of BoxLayout has two parameters: one parameter defines the container using this BoxLayout, And the other parameter specifies whether BoxLayout is horizontally or vertically arranged.

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.