开发者

What is socket bind and how to bind an address?

In Java, I need to know what is the bind operation:

ServerSocket.bind()

From Javadoc:

Binds the ServerSocket to a specific address (IP address and port number).

I know what is bind and EJB (from example) to a name. Is this similar?

How to bind a local address to a server socket?

I am using:

providerSocket.bind(new InetSocketAddress("192.168.0.1", 0));

An开发者_运维问答d I got Already Bound error!


A connection requires a client and a server.

For a client to send data to the server, the client must have the server's address and port number. Similarly, for the server to send data to the client, the server must have the client's address and port number.

Binding a socket means assigning an address and port number to the socket.

When you do:

providerSocket.bind(new InetSocketAddress("192.168.0.1", 0));

You get Already Bound error because providerSocket already has an address and port number, and assigning a new address / port number is not allowed. Once a ServerSocket is created, it is bound (unless it uses the parameterless constructor java.net.ServerSocket.ServerSocket()).


you have to leave ServerSocket() blank not ServerSocket(666,9) you should not do the second example or else it wont work. Inside the Parenthesis of the ServerSocket you type nothing.


You have to Initialize your 'providerSocket' as new ServerSocket() and not new ServerSocket(port)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜