Java Network Programming

Source: Internet
Author: User

1. Establish a TCP/IP connection
The constructor socket (string host, int port) in the socket class is used to create a socket and connect to the specified host and port. Due to restrictions on the Java Applet security mechanism, the host name cannot be specified arbitrarily, but should be the same as the name of the host where the called applet is located. Therefore, the getcodebase method can first obtain the URL of the class file currently read by the browser, and then return the host name represented by a string from the URL through the gethost method. Example from http://person.zj.cninfo.net /~ In this URL, you can return the host name "person.zj.cninfo.net" represented by a string ".
The value of the standard port address is generally: SMTP is 25, POP3 is 110, FTP is 21, and so on. Of course, some hosts use non-standard ports, before using it, you 'd better confirm it first.
2. sending and receiving data
Use the getoutputstream method in the socket class to obtain the output data stream corresponding to the current socket. Use the writebytes method in the dataoutputstream class to write data to the output data stream one by one.
Use the getinputstream method in the socket class to obtain the input data stream corresponding to the current socket. Use the Readline method in the datainputstream class to read data from the input data stream row by row.
3. Disable TCP/IP connection
You can use the close method in the socket class to close the TCP/IP connection.

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.