One of the Java Networking Technologies TCP

Source: Internet
Author: User

Recently suddenly on the Java network programming this piece is very interested in, so find a lot of information, a little bit of trying, below is their own little insight, do not like to spray, welcome correction.

First of all, talk about the client and server side,

If it is a Web page, the client sends a request to the server through a link to the Web page and the server makes a processing return to the customer.

If it is an app, then if you write the server side, it is the user through the client operation, want to send the server side request, and then the server will respond accordingly.

The principle is the same.

The code below. Attention. If it is inside Eclipse, run the server code first and then the client code.

Server code:

 PackageCom.sun;ImportJava.io.BufferedReader;ImportJava.io.DataInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classTCPServer { Public Static voidMain (string[] args)throwsIOException {System.out.println ("Here is the server!" "); //6666 port number for the serverServerSocket Server =NewServerSocket (10000); System.out.println ("Server is starting ..."); Socket Client= Server.accept ();//listens for ports and receives requests from clients//second, get the client's input stream (note that the client's output is actually output to the server, that is, the relationship of the request response)InputStreamReader InputStreamReader =NewInputStreamReader (Client.getinputstream ());//gets the input byte streambufferedreader BR=NewBufferedReader (InputStreamReader);//reads the resulting byte stream into the buffer//third, get the client output stream, want the output of the client to be streamed into the dataPrintWriter PW =NewPrintWriter (Client.getoutputstream (),true);//get a response stream for a clientString line = "";  while(Line! =NULL) { line=Br.readline (); FileOutputStream file=NewFileOutputStream ("Person.txt");//here is a text to receive, write, represent the server//data is received at the end of the/*byte[] b = new byte[1024];             b = line.getbytes (); File.write (b);*/Pw.println ("The server Received:" +Line );                      File.flush ();         } br.close ();    Pw.close (); }}

Client code:

 PackageCom.sun;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.BufferedReader;ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.PrintStream;ImportJava.io.PrintWriter;Importjava.net.InetAddress;ImportJava.net.ServerSocket;ImportJava.net.Socket;Importjava.net.SocketException; Public classTcpClient {/**     * @paramargs *@throwsIOException*/     Public Static voidMain (string[] args)throwsIOException {//TODO auto-generated Method StubInetAddress host =Inetaddress.getlocalhost (); Socket Client=NewSocket (host,10000);//connect a server based on a port number and IP//First: Get a client's output stream,PrintWriter PW =NewPrintWriter (Client.getoutputstream (),true); //stream data through the output stream to the server sideBufferedReader wt=NewBufferedReader (NewInputStreamReader (system.in)); String Str=Wt.readline (); Pw.println ("You have entered =" +str); //fourth. Customer received the server to transmit the output stream into the input stream (received the response data)//data returned by the receiving serverBufferedReader br =NewBufferedReader (NewInputStreamReader (Client.getinputstream ())); String Line=Br.readline (); System.out.println ("Customer Side output =" +Line );        Br.close ();        Pw.close ();    Client.close (); }    }

The output is:

Nihao Client output = Server Received: You have entered =nihao

Such a simple socket is done.

One of the Java Networking Technologies TCP

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.