Socket classes in the Java language

Source: Internet
Author: User
Tags character set readline socket port number

When the client program needs to communicate with the server program, the client creates a socket object on the client computer, and the socket class has several constructors. Two commonly used constructors are sockets (inetaddress addr, int port) and sockets (String host, int port), and two constructors create a stream socket based on a socket-connected server-side stream socket. For the first InetAddress subclass object obtains the server host's IP address through the addr parameter, for the second function host parameter package is assigned to the InetAddress object, if no IP address is consistent with the host parameter, Then the unknownhostexception exception object is thrown. All two functions get the port number of the server through the parameter port. Assuming that a connection has been established, the network API will bundle the client's IP address and any port number in the socket-based streaming socket, or two functions will throw a IOException object.

If you create a socket object, it may obtain input stream read transfer information from the service program by calling the getInputStream () method of the socket, or by calling the socket's Getoutputstream () Method gets the output stream to send the message. After the read-write activity completes, the client invokes the close () method to turn off the stream and stream sockets. The following code creates a socket object with a server host address of 198.163.227.6, a port number 13, and then reads the input stream from the newly created socket object and then closes the stream and the socket object.

Socket s = new Socket ("198.163.227.6", 13);
InputStream is = s.getInputStream ();
// Read from the stream.
is.close ();
s.close ();

Next we will demonstrate a Stream socket client program that will create a socket object that will access the service program running on the specified host port 10000, if the access successful client will send a series of commands to the service program and print the response of the service program. List2 allows us to create the program Ssclient source code:

Listing 2:ssclient.java


//Ssclient.java


import java.io.*;


import java.net.*;


class Ssclient


{public static void main (String [] args)


{String host = ' localhost ';


//If user specifies a command-line argument, that argument


//Represents the host name.


if (args.length = 1)


host = args [0];


BufferedReader br = null;


PrintWriter pw = null;


Socket s = null;


Try


{//Create a socket that attempts to connect to the server


//program on the host at Port 10000.


s = new Socket (host, 10000);


//Create An input stream reader this chains to the socket′s


//byte-oriented input stream. The input stream Reader


//Converts bytes read from the socket to characters. The


Conversion is based on the platform′s default character


//Set.


InputStreamReader ISR;


ISR = new InputStreamReader (S.getinputstream ());


//Create A buffered reader that chains to the input stream


//Reader. The buffered reader supplies a convenient method


//For reading entire lines of text.


br = new BufferedReader (ISR);


//Create A print writer that chains to the socket′s byte-


//oriented output stream. The print writer creates an


//Intermediate output stream writer that converts


//characters sent to the socket to bytes. The conversion


//is based on the platform′s default character set.


pw = new PrintWriter (S.getoutputstream (), true);


//Send The DATE command to the server.


pw.println ("DATE");


Obtain and print the current date/time.


System.out.println (Br.readline ());


//Send The PAUSE command to the server. This allows several


//Clients to start and verifies this server is spawning


//Multiple threads.


pw.println ("PAUSE");


//Send The DOW command to the server.


pw.println ("DOW");


//Obtain and print the current day of week.


System.out.println (Br.readline ());


//Send The DOM command to the server.


 


pw.println ("DOM");


//Obtain and print the current day of month.


System.out.println (Br.readline ());


//Send The Doy command to the server.


pw.println ("Doy");


//Obtain and print the ' current ' of year.


System.out.println (Br.readline ());


}


catch (IOException e)


{System.out.println (e.tostring ());


}


finally


{Try


{if (br!= null)


Br.close ();


if (pw!= null)


Pw.close ();


if (s!= null)


S.close ();


  }


catch (IOException e)


  {


}


} }}

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.