Java Network programming-socket programming __ algorithm

Source: Internet
Author: User

socket is the use of TCP protocol, this protocol to send data before the connection will be established to ensure the reliability of the data,

The following is a template for client-and server-side socket communications:
Package demo;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.io.PrintWriter;
Import Java.net.ServerSocket;
Import Java.net.Socket;

The server receives a Socket and initiates a threading process data transfer public
class Socketthread extends thread{
    private Socket s;
    Public Socketthread (Socket s) {
        //TODO auto-generated constructor stub
        this.s=s;
    }
    @Override public
    Void Run () {
        try {
            outputstreamwriter outw=new outputstreamwriter s.getoutputstream ( ));
            PrintWriter pw=new PrintWriter (OUTW);
            Pw.write ("welcome:i AM Server");
            Pw.close ();
            Outw.close ();
        } catch (IOException e) {
            //TODO auto-generated catch block
            e.printstacktrace ();
        }
    }
}
package demo;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.net.ServerSocket;

Import Java.net.Socket;
        Server-side starts a serversocket, uses the Accept () loop to listen for client requests: public class Servermain {public static void main (string[] args) {
            try {serversocket server=new serversocket (8080);
            Socket socket; Service-side Loop Monitor while ((Socket=server.accept ())!=null {//Receive connection, start a thread to handle new sock
            Etthread (socket). Start ();
        The catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }
    }

}
package demo;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.net.Socket;
Import java.net.UnknownHostException;

        Client connection public class Clientmain {public static void main (string[] args) {//TODO auto-generated stub
        Socket C;
                try {for (int i = 0; i < 5; i++) {c = new Socket ("localhost", 8080);
                InputStreamReader inr=new InputStreamReader (C.getinputstream ());
                BufferedReader br=new BufferedReader (INR);
                System.out.println (Br.readline ());
                Br.close ();
            Inr.close (); The catch (Unknownhostexception e) {//TODO auto-generated catch block E.printstacktrac
        E ();
        catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }

    }

}

The boot order is to start the server first and then start the client to connect. In this example, 5 clients are connected. Screenshot of Effect:

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.