?
Solved

ServerSocket

Posted on 2004-11-27
11
Medium Priority
?
618 Views
Last Modified: 2010-05-18
Ola,

How do I have the ServerSocket class accept incomming connections on a specific IP address and port?
please provide example
0
Comment
Question by:DaFou
  • 6
  • 3
  • 2
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12686652
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12686657
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12686672
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 13

Expert Comment

by:petmagdy
ID: 12686681
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12686690
miss typoo offcourse specify port 9999 not 999
0
 
LVL 2

Author Comment

by:DaFou
ID: 12687058
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12687064
try first ur machine name
0
 
LVL 2

Author Comment

by:DaFou
ID: 12687074
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
 
LVL 2

Author Comment

by:DaFou
ID: 12687088
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12687383
u must catch like this:

import  java.io.*;

....

try
{
ServerSocket lServerSocket = new ServerSocket( 9999 );

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

Accepted Solution

by:
petmagdy earned 2000 total points
ID: 12687470
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question