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 to realize the Network Communication Project by java

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

Share

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

This article mainly shows you "java how to achieve network communication project", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "java how to achieve network communication project" this article.

I. problems solved by network communication

Data exchange between different machines and different programs

II. The concept of computer network

Computer network course refers to the connection of multiple computers and external devices with independent functions in different geographical locations through communication lines, under the management and coordination of network operating system, network management software and network communication protocols, a computer system that realizes resource sharing and information transmission.

III. IP

Ip address: InetAddress

A computer on a network can be uniquely located through an ip address

127.0.0.1Compact 0.0.0.0 native localhost, native and own ip are all this

Ip address classification: IPV4/IPV6

IPV4 127.0.0.1 IPV4 4 byte groups, 32bit, 4 billion has been exhausted.

IPV6 128bit. 8 unsigned integers, such as:

2001:0bb2:aaaa:0015:0000:0000:1aaa:1312

IP address in the local area network: other machines in the same network can use this IP to access your machine

Multiple computers in the same area constitute local area network-> multiple local area networks constitute metropolitan area network-> multiple metropolitan area networks constitute wide area network

IV. Domain name

Domain name such as: www.baidu.com

Because the ip address is difficult to remember and is changed for a long time, it is convenient to remember the domain name instead of the ip address.

DNS root server

DNS (Domain Name System, domain name system), a distributed database on the Internet that maps domain names and IP addresses, makes it easier for users to access the Internet without having to remember the IP strings that can be read directly by the machine.

Port

A port represents a program or process on a computer

Different processes have different port numbers to distinguish between software

Port numbers under a single protocol cannot conflict.

Port separation

Public port 0-1023

HTTP:80

HTTPS:43

FTP:21

Telent:23

Program registration port: 1024-49141, assign users or programs

Tomcat:8080

MySQL:3306

Oracle:1521

Dynamic, private: 49152-65535

Port common instructions

Netstat-ano # View all ports netstat-ano | "5900" # View the specified port tasklist | findstr "8986" # View the process of the specified port VI. Use telnet to complete simple network communication

Double-ended communication CCMARS architecture, one for the client and one for the server

Socket is an object that wraps TCP connections in Java

The code is as follows

Package com.lding.net;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket / * * @ program: Net * @ description: Telnet for information transmission * @ author: Wang Ding * @ date: 2021-09-15 22:45 * * / public class TelnetServer {public static void main (String [] args) throws IOException {/ / create a Socket,Socket packaged tcp connection / / designated port / / port: 65536 0-1024 ports try not to use collision avoidance ServerSocket ssc=new ServerSocket (9999) / / listen on the client's Socket connection System.out.println ("server open: ip:" + ssc.getInetAddress (). GetHostAddress () + "port number:" + ssc.getLocalSocketAddress ()); System.out.println ("waiting for the predestined person."); / / block listening until a client connection comes up Socket socketClient = ssc.accept () System.out.println ("client connected:" + socketClient.getInetAddress ()); System.out.println ("client port" + socketClient.getPort ()); / / transfer data IO stream byte stream character stream / / the server sends a message to the client: get an output stream from the client's Socket and write data to this output stream as message content OutputStream output = socketClient.getOutputStream () Output.write (97); output.write ("Hellogramme Clientkeeper!" .getBytes ()); / / the server receives messages from the client: get an input stream from the client's Socket and read the data from the input stream InputStream input=socketClient.getInputStream (); while (true) {int readnum=input.read (); System.out.println ((char) readnum) Output.write (("to_:" + (char) readnum) .getBytes ();}}

Run the process:

The server waits for the connection after running, because the accept method is a blocking listener.

Connect the port using telnet

Command format: telnet+ space + ip address + port number

When the connection is successful, the blocking ends

The data sent by telnet can be received in real time on the idea running port, which enables the client to send the data to the server, and the server can also send the received data back to the server.

The above is all the contents of the article "how to realize the Network Communication Project in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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