Java Network Programming from entry to mastery (13): Use the Socket class to receive and send data

Source: Internet
Author: User
Tags domain name server

Network applications are divided into two parts: client and server, while SocketClass is responsible for processing Client CommunicationJavaClass. This class can be used to connect to the specifiedIPOr the Domain Name Server, and can send and accept data with the server. It will be discussed in detail in this article and several subsequent articlesSocketClass usage, includingSocketCategories, various connection methods,GetAndSetMethod, timeout during connection, and network disconnection.

In this article, we will discuss how to useSocketClass. Generally, the network client program must perform the following three steps when connecting to the service program.

1.Connect to the server

2.Send and receive data

3.Disable network connection

I,Connect to the server

The client can connect to the server in two ways, one is throughIPTo connect to the server, while the other is to connect to the server through the domain name.

In fact, these two methods are essentially one. At the underlying clientIPBut there are some differences between the two methods.IPMethod to connect the server program, the client simply accordingIPTo connect to the server through the domain name, the client mustDNSResolve a domain nameIPAnd thenIP.

In many programming languages or development tools (suchC/C ++,DelphiYou must resolve the domain nameIPAnd then passIPInJavaThe domain name resolution function has been included inSocketClass. Therefore, we only needIPUse the same domain name.

PassSocketThe most common method for connecting a class to a server program isSocketClass constructor willIPOr pass in the domain name and port number as parametersSocketClass.SocketClass constructor has many overload forms. In this section, we only discuss one of the most common forms:Public Socket (String host, int port). From the definition of this constructor, you only needIPOr directly input the domain name and port number to the constructor. The following codeIs an example program connecting to the server program:

Package Mysocket;

Import Java.net. * ;

Public   Class MyConnection
{
Public   Static   Void Main (String [] args)
{
Try
{
If (Args. length >   0 )
{
Socket socket =   New Socket (args [ 0 ], 80 );
System. out. println (args [ 0 ] +   " Connection successful! " );
}
Else
System. out. println ( " Please specify IP or domain name! " );
}
Catch (Exception e)
{
System. err. println ( " Error message: "   + E. getMessage ());
}
}
}

Above.IPOr pass in the program through the Domain NameSocket socket

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.