开发者

Confusion on ServerSocket(port)

开发者 https://www.devze.com 2023-02-20 09:27 出处:网络
I have a little confusion on one of the constructor of ServerSocket开发者_运维百科. When i write ServerSocket ss=new ServerSocket(3000);,does it mean that i have requested a Connection

I have a little confusion on one of the constructor of ServerSocket开发者_运维百科. When i write ServerSocket ss=new ServerSocket(3000);,does it mean that i have requested a Connection on port number 3000 of server.


No, this means that your process will listen on port 3000 for incoming connections. This means that, provided you follow all the necessary steps in your server code, a client would be able to connect to the server on port 3000 and talk to your application.

Take a look at the following tutorial for an introduction to sockets programming in Java: Lesson: All About Sockets.


This means that your server will be bound to port 3000. To have your server socket listen for incoming connections, on port 3000, do:

ServerSocket ss = new ServerSocket(3000);
Socket connection = ss.accept();

Calling ss.accept() causes the server socket to wait and listen for incoming connections on the port it was bound to. The Socket returned from ss.accept() is what you will use to communicate with the client that has connected to your server.

0

精彩评论

暂无评论...
验证码 换一张
取 消