[Javase] Network programming (TCP server side client visits blocked)

Source: Internet
Author: User

The client sends data to the server, and after the server receives the data, it feeds back the data to the client.

Client:

Gets the Socket object,new out, constructs the parameter:the IP address of the String , The port number of int

Call the Getoutputstream () method of the Socket object to get to the outputstream Object

Call the write () method of the outputstream object , output stream output data, parameter:byte[] byte array

Call the getInputStream () method of the Socket object to get to the inputstream Object

Call the Read () method of the inputstream object , read the data to get the length of the read, parameter:byte[] byte array

Get String object,new out, construct parameter:byte[] byte array,0 start, Len length

Call the close () method of the socket object to close the socket

The client's input stream reads the read () method, which is a blocking method, where it waits for the server to return data

Service side:

Get serversocket object,new out, construct parameter:int 's port number

Call the accept () method of the serversocket object to get the Socket Object

Call the getInputStream () method of the Socket object to get the input stream object, parse the input stream

Call the Getoutputstream () method of the Socket object to get the output stream object, output data

Call the close () method of the socket object to close the socket

Call the close () method of the serversocket object to close the serversocket

The accept () method on the server side, which is the blocking method, waits for the client's connection here

Server.java

ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.net.InetAddress;ImportJava.net.ServerSocket;ImportJava.net.Socket;/*** Service Side * *@authorTaoshihan **/ Public classServer { Public Static voidMain (string[] args)throwsException {serversocket serversocket=NewServerSocket (10004);//While (true) {Socket socket =serversocket.accept (); InputStream InputStream=Socket.getinputstream (); byte[] buf =New byte[1024]; intLen =Inputstream.read (BUF); InetAddress IP=socket.getinetaddress (); System.out.println ("From" + ip.gethostaddress () + "say:" +NewString (buf, 0, Len)); Thread.Sleep (10000);//sleep for 10 seconds and the client waitsOutputStream out=Socket.getoutputstream (); Out.write ("I am the server, I have received the message". GetBytes ());            Socket.close ();        Serversocket.close (); //}    }}
Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.net.InetAddress;ImportJava.net.Socket;/*** Client *@authorTaoshihan **/ Public classClient { Public Static voidMain (string[] args)throwsException, IOException {socket socket=NewSocket (Inetaddress.getlocalhost (), 10004); OutputStream OutputStream=Socket.getoutputstream (); Outputstream.write ("I am the client, server Hello". GetBytes ()); InputStream in=Socket.getinputstream (); byte[] b=New byte[1024]; intLen=in.read (b);//this will block and wait for the service side to returnSystem.out.println (NewString (b,0, Len));    Socket.close (); }}

Client.java

[Javase] Network programming (TCP server side client visits blocked)

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.