cool hit counter Socket Programming in Java_Intefrankly

Socket Programming in Java


Now I am no longer as enchanted, and I no longer want to share the beauty of faraway places and my own joy with those I love; my heart is no longer in spring, my heart is in summer. I am more introspective, more profound, more washed up, and I am more grateful than I was then. I'm lonely, but not stuck with loneliness. I was happy to let the sun ripen, my eyes were content with the food I saw, I learned to see, and the world became beautiful. -- Herman Hesse

Socket Programming in Java

Socket is based on the transport layer of the network exists, and which includes the TCP protocol, UDP protocol, may be depending on the needs of our application, we will also be based on different protocols, for security we use TCP, for efficiency we use UDP.

Today we will talk about programming based on the TCP protocol. A socket, often called a "socket", is used to describe an IP address and a port, and is the handle to a communication chain. Applications usually send requests to the network or answer network requests via "sockets".

For message transfer or file transfer, we divide them into server side and client side. We can see two classes in the API about them are: ServerSocket and Socket,different types, we create different objects respectively.

API for Socket.

java.net.Socket inherits from java.lang.Object and has eight constructors, while the three methods we use for more are, respectively

. Accept method is used to generate a "block" until a connection is accepted and an instance of the client's Socket object is returned. "Blocking" means that it causes the program to run temporarily "stuck" in this place until a session is generated, and then the program continues, and we usually "block" by a loop.

. getInputStreammethod gets the connection input and also returns an instance of an InputStream object.

. getOutputStream method gets the other end of the connection will get the input, while returning an instance of an OutputStream object.

ServerSocket API.

ServerSocket(int port) ~ Creates a server socket bound to a specific port.

So we can understand that we first create ServerSocket object on the server side, while passing in the parameters we want to bind, next we use this object to call the method Accept to receive, want to connect with us the client object, and then use the accepted object to use IO-related knowledge for other transfers such as the above methods are important and commonly used.


Recommended>>
1、Deloitte report smartphone penetration in developed countries to reach 90 percent by 2023
2、Secret kissing scene connoisseur training machines to recognize kissing
3、The controversial EOS
4、Blockchain trends are gradually taking shape as speculative coin mining returns to normalization Is it appropriate to have both hands
5、Thinking about how enterprises can dispel myths about hybrid cloud

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号