Java Network Programming (ii) Some personal thoughts about sockets

Source: Internet
Author: User

1, how to communicate between the sockets?
1.1 Communication is going to be done between 22, so there should be at least one client and one server side (server), typically multiple C-end pairs of a s-end---c\s


1.2 On client: Can create socket object: Socket socket = new socket (IP address, port number);


1.3 On the server side: First, the server is not hardware, but a software. The "server" that you normally see is just a hardware device that runs on the server software. The class that identifies the server is: serversocket! ServerSocket ss = new ServerSocket (port number); So a write, hehe, a server to get it done.


1.4 How does the client find the server? Yes, by IP address and port number.


Why is 1.4.1 serve something slogan? Through IP address knowledge can find a computer, but in a computer, there are many applications Ah, then you are looking for the server where? This time will be through the port number to find, because the port number is the key to distinguish between different applications on a computer; For example, you go to your classmates, you know your classmates in XX class, then you went to XX class, and then you are based on your classmates look to find him. Your classmate's "look" is similar to the "port" of the program. You find your classmates through your classmates in the class and your classmates, then you connect to the server is you find the server on the computer and then through the port connected to the server you want.


1.4.2 Server side Why not write IP address, the reason is very simple, the server is to let other programs to find him. As long as the S-side sets its own port (the port cannot have a burst), the client will find the server on this computer.


1.5 Connect between the client and the server: the connection between the socket and the socket, the client sends data: Socket.send (); server-side receive data: socket s = serversocket.accept (); It is generally the first server that waits for the data sent by the receiving client. Once the client and server-side connection, then the two will form a "two-way pipeline", the client and the server side of the data is through the pipeline for the transmission, since it is bidirectional, then the C terminal can send data to the S end, S-end can also be sent to the C-side data


1.6 What form of data is sent? ———— Stream! Socket.getinputstrem () and Socket.getoutputstream () Form a momentum. I'm starting to wonder why these two streams can be transferred like this. Because the C-and S-ends are connected through the socket, the sending and receiving are just the sockets at both ends, and the two streams do not flow between two established pipelines, and where can they flow? Finally, just what you want to do with the received stream.


More than 1.7 clients connect to a server: if there are multiple clients, it means that there are multiple client sockets, so you have to establish multiple sockets on the server side and the C-side sockets to connect to form the respective C-and s-end pipelines. In this case, the S-end will call the Accept () method many times.

Finish

Java Network Programming (ii) Some personal thoughts about sockets

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.