开发者

Connecting two clients in a network

If I have two clients which are aware of each other (ie, they 开发者_开发百科know which ports to use and their respective hostnames), can they connect to each other using sockets?

I know that one could run as a server and one could run as a client, but can they both be clients? Both clients are not behind a router or firewall and each have a unique IP.

I'm also using Java, so I'm thinking of using two Socket instances on both clients rather than a ServerSocket on one and a Socket on the other.


Someone needs to listen for a connection somewhere. A standard Socket connection connects to a host and port on which there is an active listener (ServerSocket for standard Java socket). ServerSocket does, by default, block, waiting for incoming connections.

If you go with adding a ServerSocket connection on both clients, you would want to have one thread listening for connections and another attempting to make an outgoing connections. There are some alternatives, such as using NIO which would allow you to check for an incoming and outgoing connection without blocking your entire application.

You can also look into Broadcasting, however, there are complexities with that solution as well.

One more thought: Depending on your requirements you can always look into leveraging P2P frameworks such as JXTA.


No, they only can do it by initializing a server socket at least in one of the sides. After one of the clients establishes the connection then you can implement a protocol to write/read from both sides. This is the mechanism to "wait" for connections, client sockets can't receive a connection if it has not been init by server socket first.

Idea, why don't you integrate a small HTTP server in both clients so that you know you can stick to the HTTP protocol (i.e: Jetty)


You can create a ServerSocket, accept a connect. This adds one line of code compared to just creating a socket so its not much code overhead.

On the client side

Socket s = new Socket(hostname, port);

on the server side

ServerSocket ss = new ServerSocket(port); // can be reused.

Socket s = ss.accept();

Since you only need a single connection, it may not be any more complicated that this.


No, you need a ServerSocket to listen to a port and accept a connection.


Simply put - the answer is no, you can't use both ends as clients. There is no command that lets two sockets connect to each other.

The only way to create a connection with the sockets API is for one side to listen for connections, and accept them, and for the other to connect to it.

The usual solution to the situation is to arbitrarily decide that one end is the server and the other is the client. You listen on the server end, and connect on the client end.


You can use the connection-less UDP protocol instead of TCP if reliable delivery of messages is not of concern.

TCP is a connection-oriented protocol so one of the participant will have to initiate the connection while other listens for the connection.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜