ServerSocket

Ola,

How do I have the ServerSocket class accept incomming connections on a specific IP address and port?
please provide example
LVL 2
DaFouAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
If for some reason you can bind the listening socket to more than one address, specify the address you need using this ctor:

http://java.sun.com/j2se/1.5.0/docs/api/java/net/ServerSocket.html#ServerSocket(int, int, java.net.InetAddress)
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

petmagdyCommented:
to Specify a specific host and port u can do this:

ServerSocket socServer = new ServerSocket(9999, 0, InetAddress.getByName("192.168.0.12") );

this specify that port is 999 and IP is 192.168.0.12
0
petmagdyCommented:
miss typoo offcourse specify port 9999 not 999
0
DaFouAuthor Commented:
now when i use:
ServerSocket lServerSocket = new ServerSocket(9999, 0, InetAddress.getByName( "192.168.1.165" ) );
I get this:
unreported exception java.net.UnknownHostException; must be caught or declared to be thrown

although my local IP address is 192.168.1.165
0
petmagdyCommented:
try first ur machine name
0
DaFouAuthor Commented:
i want the user of my program to be able to specify at what ip address and at what port the serversocket should listen for connections. SO working with a machine name is not an option as some machines have more then 1 ip address ( more then 1 ethernet card )
0
DaFouAuthor Commented:
when trying this with only a port number it also fails.
ServerSocket lServerSocket = new ServerSocket( 9999 );
unreported exception java.io.IOException; must be caught or declared to be thrown
0
petmagdyCommented:
u must catch like this:

import  java.io.*;

....

try
{
ServerSocket lServerSocket = new ServerSocket( 9999 );

}
catch(IOException ex)
{
ex.printStackTrace();
}
0
petmagdyCommented:
or to specify host and port:

u must catch like this:

import  java.io.*;

....

try
{
ServerSocket lServerSocket = new ServerSocket(9999, 0, InetAddress.getByName( "192.168.1.165" ) );

}
catch(IOException ex)
{
ex.printStackTrace();
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.