Java Basics---Network programming (TCP programming)

Source: Internet
Author: User

TCP Programming

Using the socket (socket) in Java to complete the development of TCP programs, using this class can easily establish a reliable, bidirectional, continuous, point-to-point communication connection.

In the socket program development, the server side uses ServerSocket to wait for the client connection, for the Java Network program, each client uses a socket object representation.

In the Java Network program, the client can receive the server as long as it conforms to the connected communication protocol.

The ServerSocket class is primarily used in the development of server-side programs to receive connection requests from clients.

The socket will use the Aceept () method to wait for a client connection each time the server side runs, and after this method executes, the server side goes into a blocking state until the client connects until the program can continue down, and the return value type of this method is socket, Each socket represents a client object.

Accept () Returns a socket instance object each time

TCP Programs

 PackageCom.oumyye. Network programming;Importjava.net.* ;ImportJava.io.* ; Public classhelloclient{ Public Static voidMain (String args[])throwsException {//all exceptions ThrownSocket client =NULL;//represents the clientClient =NewSocket ("localhost", 8080) ; BufferedReader buf=NULL;//One-time reception completeBUF =NewBufferedReader (NewInputStreamReader (Client.getinputstream ())); String Str=Buf.readline (); System.out.println ("Server-side output content:" +str);        Buf.close ();    Client.close (); }};

After you have finished writing the server, you can test it directly with the Telnet command.

Open localhost 8080

Java Basics---Network programming (TCP programming)

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.