Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java create TCP Protocol

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to create TCP protocol by Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Transmission Control Protocol (TCP,Transmission Control Protocol) is a connection-oriented, reliable, byte-stream-based transport layer communication protocol defined by RFC 793of IETF. TCP is designed to adapt to a hierarchical protocol hierarchy that supports multiple network applications. Paired processes in host computers connected to different but interconnected computer communication networks rely on TCP to provide reliable communication services. TCP assumes that it can obtain simple and possibly unreliable Datagram services from lower-level protocols. In principle, TCP should be able to operate on various communication systems connected from hardwires to packet-switched or circuit-switched networks.

TCP:TCP protocol is based on request-response mode and uses io stream to realize data transmission.

Create a server

1. The designated port uses ServerSocket to create the server.

2. Blocking waiting for accept to connect, and an accept sets up a client

3. Operation: io stream

4. Release resources

Public class tcp {public static void main (String [] args) throws IOException {System.out.println ("- Server-"); / / 1, use ServerSocket to create server ServerSocket server=new ServerSocket (8888); / / 2, blocking wait connection acceptSocket client=server.accept (); / / return a Socket object System.out.println ("a client has established a connection"); / / 3, operation: io stream DataInputStream dis=new DataInputStream (client.getInputStream ()) / / input, client.getInputStream () returns a byte input stream String data=dis.readUTF (); System.out.println (data); / / 4. Release resource dis.close (); client.close (); server.close ();}}

Create a client

1. Establish a connection: use Socket to create the address and port of the client + service

2. Operation: input and output stream operation

3. Release resources

Public class tcp2 {public static void main (String [] args) throws IOException {System.out.println ("- Client-"); / / 1. Establish a connection: create the address and port Socket client2=new Socket of the client + service using Socket ("localhost", 8888); / / 2, Operation: input / output stream operation DataOutputStream dos=new DataOutputStream (client2.getOutputStream ()); / / output String data= "du Yulong is the most handsome"; dos.writeUTF (data) Dos.flush (); / / 3, release resources dos.close (); client2.close ();}} "how Java creates the TCP protocol" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report