Java socket Programming (top) (2)

Source: Internet
Author: User
Tags socket port number
Socket class
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

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.