Java TCP protocol Socket Programming learning

Source: Internet
Author: User

The following is a one-to-two communication programming implementation, and will continue to learn a server to listen to the implementation of multiple clients.

The main steps I've learned

First step: Create a new socket object with a specific port (e.g. 4800)
Step two: Construct the BufferedReader object with the system input device, which is used to receive the characters of the system keyboard input
Step three: Construct the PrintWriter by getting the output stream from the socket object
Fourth step: Get the input stream from the socket object to construct the corresponding BufferedReader object, which is used to receive the information sent by the server side .

I understand the closure of the socket: the first open after closing, the socket is finally closed.

The following is the code implementation of the client:

Package com.fan.socket;

Import java.io.*;
Import Java.net.Socket;

public class Socketclient {
public static void Main (string[] args) throws ioexception{

try{
Socket socket=new Socket ("127.0.0.1", 4800);
System.out.println ("Client start ...");
//Shanben 4800 port to make customer request
BufferedReader br=new BufferedReader (New InputStreamReader (system.in));
//BufferedReader object constructed by system standard input device
printwriter write=new PrintWriter (Socket.getoutputstream ());
//The output stream is obtained by the socket object, and the PrintWriter object is constructed
BufferedReader in=new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
//The input stream is obtained by the socket object and the corresponding BufferedReader object is constructed
String ReadLine;
readline=br.readline ();//Read a string from the system standard input
While (!readline.equals ("End")) {
//Stop looping if the string read in from standard input is "end"
write.println (readline);
//Output A string read from the system standard input to Server2
Write.flush ();
//Refresh the output stream so that the server receives the string immediately
System.out.println ("Client:" +readline);
//Print the read-in string on the system standard output
System.out.println ("Server:" +in.readline ());
//Read a string from server and print to standard output
readline=br.readline ();//Read a string from the system standard input
}//Resume loop
write.close ();//close socket output stream
in.close ();//close socket input stream
socket.close ();//Close Socket
}catch (Exception e) {
System.out.println ("Can not listen to:" +e);//error, print error message
}
}

}

Server-side code implementation:

Package com.fan.socket;

Import java.io.*;
Import Java.net.ServerSocket;
Import Java.net.Socket;

Public class Socketservice {
Public static void Main (string[] args) throws ioexception{
Socketservice socketservice = new Socketservice ();
socketservice.oneserver ();
}

Public void Oneserver () {
try{
ServerSocket server=null;
try{
server=new ServerSocket (4800);
System.out.println ("Server Start is ok ...");
//Create a serversocket on port 4800 to listen for customer requests
}catch (Exception e) {
System.out.println ("Can not listen to:" +e);
//error, print error message
}
Socket socket=null;
try{
socket=server.accept ();
//Use the Accept () block to wait for customer requests, with customer
//Request arrives to generate a socket object and continue execution
}catch (Exception e) {
System.out.println ("Error.") +E);
//error, print error message
}
String Line;
BufferedReader in=new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
//The input stream is obtained by the socket object and the corresponding BufferedReader object is constructed
printwriter writer=new PrintWriter (Socket.getoutputstream ());
//The output stream is obtained by the socket object, and the PrintWriter object is constructed
BufferedReader br=new BufferedReader (New InputStreamReader (system.in));
//BufferedReader object constructed by system standard input device
System.out.println ("Client:" +in.readline ());
//Print a string read from the client on standard output
line=br.readline ();
//Read into a string from standard input
While (!line.equals ("End")) {
//If the string is "Bye", the loop is stopped
writer.println (line);
//Output The string to the client
Writer.flush ();
//Refresh the output stream so that the client receives the string immediately
System.out.println ("Server:" +line);
//Print the read-in string on the system standard output
System.out.println ("Client:" +in.readline ());
//Read a string from the client and print to the standard output
line=br.readline ();
//Read into a string from the system standard input
}//Resume loop
writer.close ();//close socket output stream
in.close ();//close socket input stream
socket.close ();//Close Socket
server.close ();//Close ServerSocket
}catch (Exception e) {//error, printing error message
System.out.println ("Error.") +E);
}
}
}

Java TCP protocol Socket Programming learning

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.