TCP Summary in Java

Source: Internet
Author: User

First look at a picture, the painting is very frustrated, will see.

When the TCP client communicates with the server, it is the server that gets the client's socket to communicate.

TCP is equivalent to a previous landline, has an earpiece and a microphone, a speaks with a microphone, and B listens with the earpiece.

Let's talk about the use of TCP in Java and the steps.

TCP Client steps:

1. Set up the socket service and identify the IP and port

2. Get the input stream or output stream through the socket service.

3. Pass-through input or output stream operation data.

Steps for the TCP service side:

1. Use ServerSocket to monitor port

2. Get the client socket with accept

3. Pass the client socket to get the input and output streams.

4. Transfer via data stream.

Demonstrate with a simple example:

Package Com.core.net;import Java.io.inputstream;import Java.io.outputstream;import java.net.serversocket;import Java.net.socket;class tcpclient{public static void Main (string[] args) throws Exception {socket s = new socket ("localhost" , 10004); OutputStream out = S.getoutputstream (); Out.write ("TCP Come ...". GetBytes ()); S.close ();}} Class Tcpserver{public static void Main (string[] args) throws Exception {ServerSocket ss = new ServerSocket (10004); Socket s = ss.accept (); String IP = s.getinetaddress (). gethostaddress (); System.out.println (ip+ "... connect"); InputStream in = S.getinputstream (); byte[] buf = new Byte[1024];int len = In.read ( BUF); System.out.println (New String (Buf,0,len)); S.close (); Ss.close ();}} public class Tcpdemo {public static void main (string[] args) {}}

This example is only the customer service sends the data, the server receives the data.

Here is an example of sending data from the customer service side and sending the data to the server.

package com.core.net;import java.io.inputstream;import Java.io.OutputStream; Import java.net.serversocket;import java.net.socket;class client2{public static void Main (string[] args) throws Exception {SOCKET socket = new socket ("localhost", 10004); OutputStream out = Socket.getoutputstream (); Out.write ("Hello, server. ". GetBytes ()); InputStream in = Socket.getinputstream (); byte[] buf = new Byte[1024];int len = In.read (BUF); System.out.println (New String (Buf,0,len)); Socket.close ();}} Class Server2{public static void Main (string[] args) throws Exception {ServerSocket ss = new ServerSocket (10004); Socket s = ss.accept (); InputStream in = S.getinputstream (); byte[] buf = new Byte[1024];int len = In.read (BUF); System.out.println (New String (Buf,0,len)); OutputStream out = S.getoutputstream (); Thread.Sleep (10000); Out.write ("Good customer service, hello". getBytes ()); S.close (); Ss.close ();}} public class TcpDemo2 {public static void main (string[] args) {}} 

The last example is the service side is the uppercase converter, the customer service input data, the server will convert the input data uppercase.

Package Com.core.net;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.InputStreamReader; Import Java.io.outputstreamwriter;import java.net.serversocket;import Java.net.socket;class TransClient{public static void Main (string[] args) throws Exception {socket socket = new socket ("localhost", 10005);//keyboard input BufferedReader Read ER = new BufferedReader (new InputStreamReader (system.in));//socket input stream BufferedReader Bufin = new BufferedReader (new InputStreamReader (Socket.getinputstream ()))//socket output stream BufferedWriter bufout = new BufferedWriter (new OutputStreamWriter (Socket.getoutputstream ())); String line = null;while (line = Reader.readline ())!=null) {if (' over '. Equals (line)] Break;bufout.write (line); Bufout.newline ();//Pay attention to "\ r \ n" Bufout.flush (); String str = bufin.readline (); SYSTEM.OUT.PRINTLN ("server:" +str);} Reader.close (); Socket.close ();}}     Class Transserver{public static void Main (string[] args) throws Exception {ServerSocket ss = new ServerSocket (10005); Socket s = ss.accept();     String IP = s.getinetaddress (). gethostaddress (); System.out.println (ip+ "... connection"); BufferedReader reader = new BufferedReader (New InputStreamReader (S.getinputstream ())); BufferedWriter out = new BufferedWriter (New OutputStreamWriter (S.getoutputstream ())); String line = null; while (line = Reader.readline ())!=null) {System.out.println (line); Out.write (Line.touppercase ()); Out.newline (); Out.flush (); } s.close (); Ss.close ();}} public class Transdemo {public static void main (string[] args) {}}

TCP Summary in Java

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.