Connection-based Java Network Programming

Source: Internet
Author: User

TCP-based Java Socket programming is implemented. The function is simple: the client outputs a "connect" to the server ", the server receives and sends a message "Hello" to the console. The client receives and outputs the message.

1. Server Side

Java code
  1. Package e.net. socket;
  2. Import java. io. DataInputStream;
  3. Import java. io. DataOutputStream;
  4. Import java. io. IOException;
  5. Import java. io. InputStream;
  6. Import java. io. OutputStream;
  7. Import java.net. ServerSocket;
  8. Import java.net. Socket;
  9. Public class TCPServer {
  10. Public static void main (String [] args ){
  11. Try {
  12. ServerSocket serverSocket = new ServerSocket (8888 );
  13. Socket socket = serverSocket. accept ();
  14. // Read client data
  15. InputStream info = socket. getInputStream ();
  16. DataInputStream dis = new DataInputStream (info );
  17. System. out. println (dis. readUTF ());
  18. // Output data to the client
  19. OutputStream OS = socket. getOutputStream ();
  20. DataOutputStream dos = new DataOutputStream (OS );
  21. Dos. writeUTF ("Hello! ");
  22. Dos. flush ();
  23. } Catch (IOException e ){
  24. E. printStackTrace ();
  25. }
  26. }
  27. }

 

 

2. Client:

Java code
    1. Package e.net. socket;
    2. Import java. io. DataInputStream;
    3. Import java. io. DataOutputStream;
    4. Import java. io. IOException;
    5. Import java. io. InputStream;
    6. Import java. io. OutputStream;
    7. Import java.net. Socket;
    8. Import java.net. UnknownHostException;
    9. Public class TCPClient {
    10. Public static void main (String [] args ){
    11. Try {
    12. Socket socket = new Socket ("192.168.1.123", 8888 );
    13. // Send data to the server
    14. OutputStream OS = socket. getOutputStream ();
    15. DataOutputStream bos = new DataOutputStream (OS );
    16. Bos. writeUTF ("Connect ");
    17. Bos. flush ();
    18. // Receives server data
    19. InputStream is = socket. getInputStream ();
    20. DataInputStream dis = new DataInputStream (is );
    21. System. out. println (dis. readUTF ());
    22. } Catch (UnknownHostException e ){
    23. E. printStackTrace ();
    24. } Catch (IOException e ){
    25. E. printStackTrace ();
    26. }
    27. }
    28. }

Related Article

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.