Hello Members,

Is it possible to write a Client/Server program between two PCs in a LAN? If yes, besides the IP address of the two PCs, what other information would I need?

Thank you!!

Recommended Answers

All 6 Replies

Very possible. In fact the Java API has all the things you need to do this without too much trouble. Have a look at he Server Socket and the Socket classes.
Basically you open a ServerSocket on an arbitrary port the server, the client then opens a Socket connection to the server's IP/port, and you can then use ordinary streams to send raw data or whole objects back & forth.
Only tricky bit can be getting the server's IP address . If you don't mind typing it in that's OK, otherwise look at JmDMS (google it) which is an open source 100% Java version of the protocol used by (among others) Apple's products to find servers on the LAN.

Hello James,

Thanks a lot for your answer!

So, can any PC on the LAN be a Client and any other PC on the LAN be a Server as long as I know their IP addresses?

Does the Server.java start first and then wait for a Client request? The Client.java is then started and the communication is established between Client/Server using streams?

Does the above make sense?

Kindly let me know. I will look into JmDMS.

Thank you!!

Yes any PC can be either or both.
The client needs to know the server's IP address and port, that's all.
The server starts first, the client then connects to it, then either side can open an output stream to write to the other side over that socket connection.

Hello James,

Thanks a lot!! That helps to get started.

-sciprog1

Glad to help. If you decide you need JmDNS come back here and we can swap notes. I found it very confusing (no documentation worth speaking of) and spent some time hacking together a simple cover that gave me a dead simple interface fror just locating a simple server. I'd be happy to share that with you (or anyone else listening) in exchange for as promise of feedback and a copy of any enhancements you may make.

Hello James,

I certainly will!

Thank you!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.