Meaningless Drivel is fun!*
The moose likes Sockets and Internet Protocols and the fly likes java.net Socket port number question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "java.net Socket port number question" Watch "java.net Socket port number question" New topic
Author

java.net Socket port number question

Mat Matthew
Greenhorn

Joined: May 07, 2005
Posts: 3
Hi folks,
New to socket programming here. Please ignore if its too stupid. In sun site for What is a socket ?

On the client-side: The client knows the hostname of the machine on which the server is running and the port number to which the server is connected. To make a connection request, the client tries to rendezvous with the server on the server's machine and port. If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to a different port. It needs a new socket (and consequently a different port number) so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.

<<<<<<
But if we print out the port number of the socket side on the server, we get the same port number we requested the connection. I mean upon acceptance of the request from client, the server does not bind the socket to a new port. The socket is still bound with the orginal port on the server. Could some body explain please?

Thanks in advance
Mat
[ May 08, 2005: Message edited by: Mat Matthew ]
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Mat,
I'll explain what I can. Any other readers, please correct me if I go wrong.


If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to a different port.


That is not true. The server does not get a new Socket bound to a different port. It continues to listen to the client on the same port. I think you are referring to the Socket which is created when a client is accepted. Now this is what I think :

Most probably that Socket holds the information to the client's IP Address and port number from which it connected to the Server and it also contains the information as to from which port the Server accepted the Client.
The rest of the transaction takes place through the same port.


With Best Regards,
Shyam Prasad Murarka
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
That quote that I gave its true. I just wanted to say that you must have got the meaning of the statment wrong.
Mat Matthew
Greenhorn

Joined: May 07, 2005
Posts: 3
Just for clarification, for the code below
// Server is listening to port 2223
ServerSocket server = new ServerSocket(2223);
//accepting the client request on the given port(2223)
Socket client = server.accept();

This 'client' socket has two ports associated with it, thats my understanding one local port and one remote port. Remote port is the port on the client. The local port on the server is 2223 or some other port?? What I understood from sun's explanation is, this port is different from port 2223 ??

Thanks
[ May 08, 2005: Message edited by: Mat Matthew ]
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209

Just for clarification, for the code below
// Server is listening to port 2223
ServerSocket server = new ServerSocket(2223);
//accepting the client request on the given port(2223)
Socket client = server.accept();

This 'client' socket has two ports associated with it, thats my understanding one local port and one remote port. Remote port is the port on the client. The local port on the server is 2223 or some other port?? What I understood from sun's explanation is, this port is different from port 2223 ??


Yes, the local port will be 2223. And most probably when Sun said that the port will be different it might have been referring to the client's port (which obviously will be different.)
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225


in this case the Server listens on port 2223. the Server's port remains the same, ie, 2223. but the client may connect to this server port using any port. hence the client port will be different.

Consider this:

Your office has a phone line. so, suppose you want to take a leave because you had gone vacationing and could not come back in time, you will call your office on the number the office has published from any number available to you. maybe your cell phone or your hotel phone or whatever. So, wherever you call from(Client socket - finds a free port and connects), you ALWAYS call in to the published office number(this is the ServerSocket always listening on the same port).

i hope i am right and could explain it better.
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Hey Neeraj,
That was a good example. I really like it. And now coming to think of it I came across an article on the net which helped me understand Sockets properly. Hope it helps others too.

SOCKET PROGRAMMING USING JAVA
If you find this tutorial good enough, please vote for me (use the Your Vote! section at the end of this page)
So you want to start doing some network programming with Java... You've come to the right place. I'll introduce you to the interesting world of Java sockets. By the end of this tutorial, you should be able to understand what sockets are and how to build simple Java applications using sockets.

Whats a socket?

Don't tell me you've never chatted on those instant messengers like yahoo, msn and aol. But its OK if you never understood what goes on behind the scene; after all, thats what we are here for. Lets say you've installed one of those instant messengers on your computer. After you run it and enter your user name and password, the messenger tries to connect to its server (say, the yahoo server). What exactly does this 'connect' mean?

Every computer on a network has an IP address. This address is like your house address, something that identifies your computer uniquely, allowing others to communicate with your computer. I wont go much into IP addresses, but let me just tell you that an IP address looks something like this - 64.104.137.158 - a set of numbers separated with dots. However, some computers with rich owners will also choose to have a domain name in addition to this sick looking number, so that people can easily identify them. A domain name looks far more sane - like www.yahoo.com. There are some special computers on the Internet, whose sole purpose in life is to translate the domain name to IP address and vice versa.

Now, you know that many programs can run on the same computer, don't you? Lets say that, there are some 10 programs running on a certain computer. To add to the confusion, lets say all of them are waiting for other computers to contact them. Imagine it like this - 10 of you share a big office space and a single telephone - and all of you expect calls from your own clients. How will you handle this? Perhaps appoint one person who'll hand over the call to the right person. Possible, but an undeniable menace. This will mean, when one of you take the call, other clients will not be able to reach the rest of you. Besides, its a pain to have a person route the calls to the right people. You must have guessed what I'm heading at - if all those programs running on a single computer proudly ask their clients to contact them on a certain IP address, their clients are not going to be pleased. The idea is... having a separate IP address per program, right? WRONG. Thats out of question. Its like asking for a separate office for each of you. Wont separate phone numbers suffice? Yes. In networking parlance, we call these 'separate phone numbers' as ports. A port is just a simple number - each program running on the same computer can choose to have a unique port number to identify itself to the outside world. REMEMBER - these ports are not slots on your computer hardware - dont think you can find them if you try hard enough. They are just logical numbers. Now the point should be clear. We have an IP address that lets the other computers look for a certain computer on the network. And we have a port number that'll identify a certain program running on that computer. Understand that, two programs running on different computers CAN use the same port number. Two houses on different streets can have the same house number, can't they? So, finally, we are almost there - just to scare you a bit, lets derive a formula -

An IP address = uniquely identifies a computer on the network. A port number = uniquely identifies a program running on a computer.

Adding the above equations,

An IP address + A port number = _______

In other words, A _____ = uniquely identifies a program on the network

If you guessed it right, thanks, my effort didn't go waste. If you didn't, no problem, go back and read from the beginning, or google for a better tutorial. The ____ is... SOCKET!

To summarize, a socket is a combination of an IP address and a port. A socket address lets other computers on the network locate a certain program running on a certain computer. You may represent a socket address like 64.104.137.58:80, where 64.104.137.58 is the IP address and 80 is the port number.

How to program

Enough of theory talk. Lets get into some action now. We are going to write some very simple Java code, that'll demonstrate the use of sockets. Here is what is going to happen -

1) One Java program will try to connect to another Java program (which is desperately waiting for someone to contact it). Lets call the first program as Client, and the second as Server.
2) Once connected, the client program is going to accept whatever you type, and send it dutifully to the server program.
3) The server is going to send back the same text to the client, just to show that it is least interested in doing such an uninteresting thing.
4) The client, after getting back the same text from the server, is going to throw it on your face, showing you what the server thinks about you.

Ready? Lets get started. Note that, I wont be teaching you Java programming from scratch, I'll explain only the socket-related portions of the code.

Create 2 fresh Java programs and call them Server.java and Client.java. I'll paste the code below, but don't be scared, I'll explain.

Server.java

import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] ar) {
int port = 6666; // just a random port. make sure you enter something between 1025 and 65535.
try {
ServerSocket ss = new ServerSocket(port); // create a server socket and bind it to the above port number.
System.out.println("Waiting for a client...");
Socket socket = ss.accept(); // make the server listen for a connection, and let you know when it gets one.
System.out.println("Got a client ... Finally, someone saw me through all the cover!");
System.out.println();
// Get the input and output streams of the socket, so that you can receive and send data to the client.
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
// Just converting them to different streams, so that string handling becomes easier.
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
String line = null;
while(true) {
line = in.readUTF(); // wait for the client to send a line of text.
System.out.println("The dumb client just sent me this line : " + line);
System.out.println("I'm sending it back...");
out.writeUTF(line); // send the same line back to the client.
out.flush(); // flush the stream to ensure that the data reaches the other end.
System.out.println("Waiting for the next line...");
System.out.println();
}


} catch(Exception x) {
x.printStackTrace();
}
}
}

Client.java

import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] ar) {
int serverPort = 6666; // make sure you give the port number on which the server is listening.
String address = "127.0.0.1"; // this is the IP address of the server program's computer. // the address given here means "the same computer as the client".


try {
InetAddress ipAddress = InetAddress.getByName(address); // create an object that represents the above IP address.
System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?");
Socket socket = new Socket(ipAddress, serverPort); // create a socket with the server's IP address and server's port.
System.out.println("Yes! I just got hold of the program.");
// Get the input and output streams of the socket, so that you can receive and send data to the client.
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
// Just converting them to different streams, so that string handling becomes easier.
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
// Create a stream to read from the keyboard.
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String line = null;
System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks.");
System.out.println();
while(true) {
line = keyboard.readLine(); // wait for the user to type in something and press enter.
System.out.println("Sending this line to the server...");
out.writeUTF(line); // send the above line to the server.
out.flush(); // flush the stream to ensure that the data reaches the other end.
line = in.readUTF(); // wait for the server to send a line of text.
System.out.println("The server was very polite. It sent me this : " + line);
System.out.println("Looks like the server is pleased with us. Go ahead and enter more lines.");
System.out.println();
}


} catch(Exception x) {
x.printStackTrace();
}
}
}

Compile it with

javac Server.java Client.java
Open two command windows (DOS prompts). In one of those, enter

java Server
and in the other,

java Client
(in that order!)

Type something on the client window and press enter. Observe both windows and see what happens. Finally, press ctrl-C to kill the programs.

Explanation

Lets delve into the code now. You must have got some idea looking at the comments, still, lets try to analyze a few critical lines.

The server code has the lines

ServerSocket ss = new ServerSocket(port);
Socket socket = ss.accept();

A ServerSocket class is slightly different from the Socket class. The Socket class is exactly what you think.. well, it represents a socket. The ServerSocket class is mainly for allowing a program to listen for connections from clients. You create it by assigning it a port number on which it can work on. Once created, you need to call its accept() method. This method will make the program listen on the given port for connections. It hangs there until it gets a client. Once a client gets in touch, it creates a normal Socket object, and hands it over to you so that you can start doing all the socket operations you want. Note that, this Socket object returned by accept() method, represents the other end of the connection. After all, if you want to send some data to the client, you can't write it to your own socket!

Next is the Socket class. You create the Socket object by passing an IP address and a port number. Java gives you the InetAddress class to represent an IP address, so you better go their way and use it. To create an InetAddress object that represents an IP address, you can use this method -

InetAddress ipAddress = InetAddress.getByName(address);

Note that in our program, we have 127.0.0.1 for the address. This address is a special address called loopback address. Don't panic, it is just an address that represents the local computer. If you intend to run the client and server on different machines, use the correct IP address of the server.

Once the InetAddress is created, we create the Socket, Socket socket = new Socket(ipAddress, serverPort);

Once you have the Socket object, you can get the input and output streams of the socket. The input stream will let you read from the socket and the output stream lets you write to the socket.

InputStream sin = socket.getInputStream(); OutputStream sout = socket.getOutputStream();

The below lines are just for converting the above to different types of streams. So that it becomes easy for us to deal with String objects. This has got nothing to do with networking.

DataInputStream in = new DataInputStream(sin); DataOutputStream out = new DataOutputStream(sout);

The rest is easy. Because it just deals with the stream objects you created, and not with sockets. You can use your favorite stream, call your favorite methods, somehow ensure the data reaches the other end. Read up on streams if you are not comfortable.

I hope I enlightened you all by introducing the fabulous world of network programming. Feel free to shoot your questions, I'll answer them if I can. You can also give me your valuable feedback, suggestions, or the mistakes you found in this tutorial.
Srini Swami
Greenhorn

Joined: May 14, 2005
Posts: 2
Hi,

I have the same doubt as Mat Matthew. In Sun's Java tutorial, they have specifically said about the new port the server uses.

"If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to a different port. It needs a new socket (and consequently a different port number) so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client."

But, the socket created by the server's accept() prints out the same port number. Why is this ?

Thanks
Srini
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java.net Socket port number question
 
Similar Threads
Question about ServerSocket port number and Socket port number
socket.close
To know termenology
server socket and accept call
Socket on server side