Three steps to learn Java socket programming

Source: Internet
Author: User

The first step is to fully understand the socket

1. What is a socket

The so-called socket is also commonly called "socket", used to describe the IP address and port, is a communication chain handle. The application usually sends a request to the network through a "socket" or answers a network request.

In the case of j2sdk-1.3, the socket and ServerSocket class libraries are located in the java.net package. ServerSocket is used on the server side and the socket is used when establishing a network connection. When the connection succeeds, both ends of the application produce an instance of the socket, manipulating the instance to complete the desired session. For a network connection, sockets are equal and do not differ, not because of the different levels that occur on the server or on the client side. Either the socket or the ServerSocket their work is done through the SocketImpl class and its subclasses.

Important Socket APIs:

Java.net.Socket inherits from Java.lang.Object, there are eight constructors, its methods are not many, the following describes the most frequently used three methods, other methods you can see the JDK-1.3 document.

. The Accept method is used to generate "blocking" until a connection is accepted and a client's socket object instance is returned. "Blocking" is a term that allows a program to run temporarily "stay" in this place until a session is generated, and then the program continues; usually "blocking" is generated by loops.

. The getInputStream method obtains the network connection input and returns a Iutputstream object instance.

. The other end of the Getoutputstream method connection is entered and a OutputStream object instance is returned.

Note that both the getInputStream and Getoutputstream methods produce a ioexception that must be captured because the stream object they return is usually used by another stream object.

2. How to develop a server-client model of the program

Development principle:

Server, using ServerSocket to listen to the specified port, the port can be arbitrarily specified (because the port under 1024 is usually reserved port, in some operating systems are not free to use, so it is recommended to use a port greater than 1024), waiting for customer connection request, after the client connected, the session generated , and close the connection after the session completes.

Client, use the socket to issue a connection request to a port on one of the servers on the network, open the session once the connection is successful, and close the socket after the session completes. The client does not need to specify an open port, typically temporarily, dynamically allocating more than 1024 ports.

{Establish Server}

import java.net.*;
import java.io.*;
public class Server
{
private ServerSocket ss;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public Server()
{
try
{
ss = new ServerSocket(10000);
while (true)
{
socket = ss.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
String line = in.readLine();
out.println("you input is :" + line);
out.close();
in.close();
socket.close();
}
ss.close();
}
catch (IOException e)
{}
}
public static void main(String[] args)
{
new 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.