Java TCP Client Service side visits

Source: Internet
Author: User

Service side:

1, create the Socket Server service, server side in order for the client to be able to connect, must provide a port, listen to a port
2, gets the client object, communicates through the client's socket stream and the corresponding client
3, gets the read stream of the client's socket stream
4, read the data and display it on the server side
5, close the resource

Package Cn.net.tcp;import Java.io.ioexception;import Java.io.inputstream;import java.io.outputstream;import Java.net.serversocket;import Java.net.socket;public class TCPServer {/** * @param args * @throws ioexception * *  public static void Main (string[] args) throws IOException {System.out.println ("Server startup ...."); ServerSocket ss = new ServerSocket (10003); Socket s = ss.accept (); InputStream in =s.getinputstream (); byte[] buf = new Byte[1024];int len = In.read (BUF); String str = new string (Buf,0,len); System.out.println (str); OutputStream out = S.getoutputstream (); Out.write ("I know you're here.") GetBytes ());         S.close ();         Ss.close ();}}

Client:

1, establish the TCP client service
Because it is connection-oriented, you must have a connection to communicate
When creating a client, you must specify the destination address and port
2, once the connection is established, there is a channel for transmitting data. can be transferred in the channel,
This transmission is actually realized through the stream. This stream is the socket IO stream.
3, as long as the socket IO to obtain the write action can be written to the socket stream to send to the server
4, close the resource.

Package Cn.net.tcp;import Java.io.ioexception;import Java.io.inputstream;import java.io.outputstream;import Java.net.socket;import Java.net.unknownhostexception;public class TcpClient {/** * @param args * @throws IOException  * @throws unknownhostexception  */public static void Main (string[] args) throws Unknownhostexception, IOException {System.out.println ("client-initiated ...."); Socket s = new socket ("127.0.0.1", 10003), outputstream out = S.getoutputstream (); Out.write ("Pay attention again, TCP comes.") ". GetBytes ()); InputStream in =s.getinputstream (); Byte[] B = new byte[1024]; int len  = In.read (b); String str = new string (B,0,len); System.out.println (str); S.close ();}}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java TCP Client Service side visits

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.