Richard
Richard

Reputation: 167

Sending files with java and ports

I was reading the answer to this question: How to transfer files from one computer to another over the network using Java?

and I didn't understand part of jjnguy's answer. What should the LISTENING_PORT be? How do you decide what port to use? And what is CONNECTION_ADDRESS?

I realize these are very basic networking questions, so maybe someone can point me in the direction of a good tutorial?

Upvotes: 0

Views: 1039

Answers (2)

Paul Sullivan
Paul Sullivan

Reputation: 2875

The Socket call has this signature

public Socket(String host, int port)

See documentation

CONNECTION_ADDRESS is a string (probably) of the quad unsigned byte form (for IPV4) i.e. 192.168.0.2

LISTENING_PORT can be any number between 0 and 65535 though numbers < 1024 or so are assigned to well known services (read up on IANA)

Here is an example:

If you put this in your browser address bar: 74.125.132.94:80 you should see google.

note that in this case it is CONNECTION_ADDRESS:LISTENING_PORT (there is a separating : )

This says connect to 74.125.132.94 on port 80 (HTTP)

Basically every machine has an IP address (CONNECTION_ADDRESS) and the application you are creating will listen on a specific LISTENING_PORT. Dependant on what you application is doing you would assign EITHER an appropriate IANA service port or an arbitrary port number above the IANA range so...

If you were creating a web server application for example then you would set the application to listen on port 80 as that is the standard HTTP web port

OR

Lets say you are creating a random game application that is serving people in your game the you would just choose a random number above 1024 i.e. 12345

Then you would create your client and have it connect to port 12345 / 80 on what ever IP the server is located at (you can work out that server IP by running IPCONFIG (Windows) or whatever command is applicable for your servers OS environment.

Upvotes: 1

martijno
martijno

Reputation: 1783

Any port, i.e. a number between 0 and 65535. The IP address or name of the server accepting the connection. Try the Wikipedia article explaining TCP or this Sun/Oracle tutorial.

Upvotes: 0

Related Questions