networking problem

N

nialltimpson

Im connecting an applet to my own simeple server useing
connection = new Socket( getCodeBase().getHost(), 2001, true );

But no matter where I open the applet, it allways say's the same ip is
connecting, the ip the simple server is running on. any idea's?
 
M

Matt Humphrey

nialltimpson said:
Im connecting an applet to my own simeple server useing
connection = new Socket( getCodeBase().getHost(), 2001, true );

But no matter where I open the applet, it allways say's the same ip is
connecting, the ip the simple server is running on. any idea's?

Well, the code you have above shows that the applet is always connecting to
your simple server which will presumably always have the same IP. I think
you're looking for the IP of the client, but you haven't shown us whether
you're trying to find that out on the client itself or on the server or what
code you're using to do it. Where are you trying to find the IP of the
client and what code are you using?

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
N

nialltimpson

Thats the code im using to connect to applet i.e.

connection = new Socket( getCodeBase().getHost(), 2001, true );

Im Accepting the lient by means of:


public ServerSocket getServer() {
if (server==null) {
try{
server = new ServerSocket(2001,10);
System.err.println("Server activated on : "+(new Date()));
}
catch (IOException ioe) {}
}
return server;
}

public void run(){
if (getServer()!=null) {
Socket client=null;
while (isServerUp()){
try {
client = getServer().accept();

Im geting the impression Im missing something here? I assumed the server
would distinguish the clients IP when they connected? Sorry about the
indentation,

Niall

Matt Humphrey Wrote:

Well, the code you have above shows that the applet is always connecting
to
your simple server which will presumably always have the same IP. I
think
you're looking for the IP of the client, but you haven't shown us whether
you're trying to find that out on the client itself or on the server or
what
code you're using to do it. Where are you trying to find the IP of the
client and what code are you using?

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
M

Matt Humphrey

nialltimpson said:
Thats the code im using to connect to applet i.e.

connection = new Socket( getCodeBase().getHost(), 2001, true );

Im Accepting the lient by means of:


public ServerSocket getServer() {
if (server==null) {
try{
server = new ServerSocket(2001,10);
System.err.println("Server activated on : "+(new Date()));
}
catch (IOException ioe) {}
}
return server;
}

public void run(){
if (getServer()!=null) {
Socket client=null;
while (isServerUp()){
try {
client = getServer().accept();

Im geting the impression Im missing something here? I assumed the server
would distinguish the clients IP when they connected? Sorry about the
indentation,

Your code is a bit messy, but the idea is fine. Your accept method will
block until the client connects. Here's what's missing. Your server should
then take the client socket and perform whatever service is required. For a
trivial server you can just answer one request and then close the
connection, but for anything more you should consider sending the request to
a separate thread. The reason for this is that ideally, at the same time,
the server (via the remainder of the while loop) would go back to try to
pickup another client.

Clients are distinguished by more than just their respective IP addresses (a
tcp virtual circuit is identified by the IP and port addresses of both ends)
which allows the same IP address to have multiple client connections and all
of them to have the same server end point. What's key here is that the same
server socket accepts all the client connections, one at a time and you have
to manage what those client connections do.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
N

nialltimpson

Cheers for your input I really appreceate it, but I actually do create a
thread for each newly connected client and within that cread create I/O
streams to it, but for some reason its allways giving me the same IP as
the host pc the server is on.

I should also mention that I have tested the I/O from a different pc
connecting to my server and it worked, even though it was giving the
server IP as the newly connected client!!

Thanks Niall

Matt wrote:
Your code is a bit messy, but the idea is fine. Your accept method will
block until the client connects. Here's what's missing. Your server
should
then take the client socket and perform whatever service is required.
For
a
trivial server you can just answer one request and then close the
connection, but for anything more you should consider sending the request
to
a separate thread. The reason for this is that ideally, at the same
time,
the server (via the remainder of the while loop) would go back to try to
pickup another client.

Clients are distinguished by more than just their respective IP addresses
(a
tcp virtual circuit is identified by the IP and port addresses of both
ends)
which allows the same IP address to have multiple client connections and
all
of them to have the same server end point. What's key here is that the
same
server socket accepts all the client connections, one at a time and you
have
to manage what those client connections do.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
M

Matt Humphrey

nialltimpson said:
Cheers for your input I really appreceate it, but I actually do create a
thread for each newly connected client and within that cread create I/O
streams to it, but for some reason its allways giving me the same IP as
the host pc the server is on.

I should also mention that I have tested the I/O from a different pc
connecting to my server and it worked, even though it was giving the
server IP as the newly connected client!!

Thanks Niall

Java networking is used successfully in many, many places. Like I said
initially, if you don't show the code for retrieving the client IP, no one
can tell what the problem is.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
N

nialltimpson

public ServerSocket getServer() {
if (server==null) {
try{
server = new ServerSocket(2001,10);
System.err.println("Server activated on : "+(new Date()));
}
catch (IOException ioe) {}
}
return server;
}

public void run(){
if (getServer()!=null) {
Socket client=null;
while (isServerUp()){
try {
client = getServer().accept();
GUI.displayMessage("\nIP"+ client.getLocalAddress().getHostName()
+ " connected on : "+(new Date()));// display client details

new Setup_Thread(client);
}

Sorry for not posting all the code initialy but I didnt expect anyone to
actually read it.
would the client.getLocalAddress().getHostName()
notprovide the exact information I'm looking for?
 
E

Esmond Pitt

client = getServer().accept();
GUI.displayMessage("\nIP"+ client.getLocalAddress().getHostName()
+ " connected on : "+(new Date()));// display client details

new Setup_Thread(client);

getLocalAddress() gives the IP address of the server's end of the
socket, so of course it is always the same. Use getInetAddress().
 
N

nialltimpson

Thanks you very much I know it was a blantely obvious mistake, but I would
put money on it I wouldn't of noticed it. Thanks again for taking time to
answer.

niall


Esmond wrote:
getLocalAddress() gives the IP address of the server's end of the
socket, so of course it is always the same. Use getInetAddress().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top