networking problem  
Author Message
nialltimpson





PostPosted: 2005-3-23 18:24:00 Top

java-programmer, networking problem 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?

 
Sudsy





PostPosted: 2003-12-1 2:12:00 Top

java-programmer >> networking problem JONESEY
> Im trying to get a text file from a server and load it into a 2
> dimensional array. So i came up with the follwoing method. The file is
> a table using "|" to indicate the end of a cell and "? to indicate
> the end of a row. I use if statements to find these but the if
> statements done seem to to execute!! Any ideas?
>
> J
>
> void getFile(String url) throws IOException {
> int x = 0;
> int y = 0;
> StreamConnection c = null;
> InputStream s = null;
> StringBuffer b = new StringBuffer();
> TextBox t = null;
> String temp;
> try {
> c = (StreamConnection)Connector.open(url);
> s = c.openInputStream();
> int ch;
> while((ch = s.read()) != -1) {
> temp = String.valueOf((char)ch);
> if (temp == "|"){
> fileArray[x][y] = b.toString();

Some interesting code! First you have to decide whether you're
going to using Strings or chars. Based on your coercion of an
int into a String containing a single character, it appears
that you're going the String route. But then you do the oddest
comparison! If you're checking for String equivalance then you
should use temp.equals( "|" ). Your code just checks whether the
strings share the same memory location. (They don't)
That being said, have you considered using char instead? That
would make the if statement look like this:
if( '|' == (char) ch ) {
A bit less overhead than what you've got currently.

 
Sudsy





PostPosted: 2003-12-1 4:11:00 Top

java-programmer >> networking problem JONESEY
> Yeah sorry it is intresting code. I have now got it doing it via the
> char comparison but its not doing anything!! The program just stops!!
> Was there anything e;se that jumped out at you

On second visit, it appears that the very first time you encounter
a character which isn't special (i.e. the NOT or the OR) you create
a new TextBox. I don't know where the code for setVisible( true )
is located but it looks like that constructor doesn't belong INSIDE
the loop. Shouldn't it only be instantiated AFTER the loop, i.e.
after you've read the entire input?

 
 
Sudsy





PostPosted: 2003-12-1 5:04:00 Top

java-programmer >> networking problem JONESEY
> the problem lies with the b = null; Is there anyway to delete all teh
> characters from a String buffer?
>

b.setLength( 0 );

 
 
Matt Humphrey





PostPosted: 2005-3-23 21:15:00 Top

java-programmer >> networking problem
"nialltimpson" <email***@***.com> wrote in message
news:email***@***.com...
> 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 email***@***.com http://www.iviz.com/


 
 
nialltimpson





PostPosted: 2005-3-23 21:35:00 Top

java-programmer >> networking problem 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 email***@***.com http://www.iviz.com/

 
 
Matt Humphrey





PostPosted: 2005-3-24 3:43:00 Top

java-programmer >> networking problem
"nialltimpson" <email***@***.com> wrote in message
news:email***@***.com...
> 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 email***@***.com http://www.iviz.com/


 
 
nialltimpson





PostPosted: 2005-3-24 5:59:00 Top

java-programmer >> networking problem 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 email***@***.com http://www.iviz.com/

 
 
Matt Humphrey





PostPosted: 2005-3-24 6:13:00 Top

java-programmer >> networking problem
"nialltimpson" <email***@***.com> wrote in message
news:email***@***.com...
> 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 email***@***.com http://www.iviz.com/


 
 
nialltimpson





PostPosted: 2005-3-24 6:23:00 Top

java-programmer >> networking problem 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?



 
 
Esmond Pitt





PostPosted: 2005-3-24 7:46:00 Top

java-programmer >> networking problem > 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().
 
 
nialltimpson





PostPosted: 2005-3-24 17:03:00 Top

java-programmer >> networking problem 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().