Java socket programming instance based on TCP protocol _java

Source: Internet
Author: User
Tags readline

This article describes the Java based on TCP protocol socket programming method, share for everyone to reference. The specific analysis is as follows:

The following is a one-to-one communication programming implementation that will continue to learn about the implementation of a server listening to multiple clients.

The main steps used here are as follows:

First step: Create a new socket object with a specific port (for example, 4800)
Step two: Construct the BufferedReader object with the system input device, which is used to receive the characters of the system keyboard input
The third step: using socket object to get output stream to construct PrintWriter
Step Fourth: Use the socket object to get the input stream to construct the corresponding BufferedReader object, which is used to receive information sent from the server side.

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

The following is the code implementation of the client:

Copy Code code as follows:
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 issue customer request
BufferedReader br=new BufferedReader (New InputStreamReader (system.in));
Constructing BufferedReader objects from System standard input devices
PrintWriter write=new PrintWriter (Socket.getoutputstream ());
The output stream is obtained from the socket object and the PrintWriter object is constructed
BufferedReader in=new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
The input stream is obtained from the socket object and the corresponding BufferedReader object is constructed
String ReadLine;
Readline=br.readline (); Read a string from System standard input
while (!readline.equals ("End")) {
Stop the loop if the string read from the standard input is "end"
Write.println (ReadLine);
Output the read string from system standard input to Server2
Write.flush ();
Refreshes the output stream so that the server receives the string immediately
System.out.println ("Client:" +readline);
Print read-in string on 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 System standard input
}//Continue cycle
Write.close (); Turn off the socket output stream
In.close (); Close the socket input stream
Socket.close (); Close socket
}catch (Exception e) {
System.out.println ("Can not listen to:" +e);//error, print error message
}
}

}

Service-Side code implementation:

Copy Code code as follows:
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 to monitor client requests on port 4800
}catch (Exception e) {
System.out.println ("Can not be listen to:" +e);
Error, print error message
}
Socket Socket=null;
try{
Socket=server.accept ();
Use Accept () to block waiting for a customer request with a customer
When the request arrives, a socket object is generated and continues to execute
}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 from the socket object and the corresponding BufferedReader object is constructed
PrintWriter writer=new PrintWriter (Socket.getoutputstream ());
The output stream is obtained from the socket object and the PrintWriter object is constructed
BufferedReader br=new BufferedReader (New InputStreamReader (system.in));
Constructing BufferedReader objects from System standard input devices
System.out.println ("Client:" +in.readline ());
Print a string read from the client on standard output
Line=br.readline ();
Read a string from standard input
while (!line.equals ("End")) {
If the string is "Bye", Stop the Loop
Writer.println (line);
Output the string to the client
Writer.flush ();
Refreshes the output stream so that the client receives the string immediately
System.out.println ("Server:" +line);
Print read-in string on system standard output
System.out.println ("Client:" +in.readline ());
Reads a string from the client and prints it to the standard output
Line=br.readline ();
Read a string from System standard input
}//Continue cycle
Writer.close (); Turn off the socket output stream
In.close (); Close the socket input stream
Socket.close (); Close socket
Server.close (); Close ServerSocket
}catch (Exception e) {//error, print error message
System.out.println ("Error.") +E);
}
}
}

I hope this article will help you with your Java programming.

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.