Welcome to Dream.In.Code
Become a Java Expert!

Join 150,130 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,121 people online right now. Registration is fast and FREE... Join Now!




How to check for listening server on entire LAN

 
Reply to this topicStart new topic

How to check for listening server on entire LAN

saumya9888
16 Aug, 2008 - 04:36 AM
Post #1

New D.I.C Head
*

Joined: 8 Aug, 2008
Posts: 22


My Contributions
Hi, i was trying to make a chat and file transfer software which can perform these tasks on LAN(not internet). I have already tested these tasks separately by making two files Server.java and Client.java
Now, I want to design a program which automatically detects if it is running on another computer on LAN and connect to it and report ip address to me.
I used an approach that it will check all other computers on lan if there is any server listening and join it.And if not,make my own machine server and wait for other people to connect using same program.

CODE

for(i=1;i<255;){
            try {                
                hostip = "10.19.41."+i;
                skt = new Socket(hostip, 9999);
                connectionstatus = true;
                jLabel1.setText("Connected to " + hostip);
                break;
            } catch (IOException ex) {
                i++;                
            }
        }

This is the code i'm using to look for listening server on network.
But i do not get desired result as in this program doesn't connect to a server i've started on same machine.it was giving a connection refused error.but now ive done"i++" in catch block so everything should go well but it acts like it cannot connect and doesnt even give any error.Please help...

This is just a raw fragment i have planned to put all these in a fine manner...
Thanks in advance.

This post has been edited by saumya9888: 16 Aug, 2008 - 07:22 AM
User is offlineProfile CardPM
+Quote Post

Gloin
RE: How To Check For Listening Server On Entire LAN
16 Aug, 2008 - 08:20 AM
Post #2

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
When you work with sockets, you can use different transport protocols among which TCP and UDP are the most common types. I'm sure you've heard of TCP, especially in the context TCP/IP. Basically, TCP is a reliable connection where a number of built in methods will help making sure your data is delivered, however TCP requires that you follow a certain protocol. UDP instead is an unreliable, best-effort service that doesn't care what happens to your data. It's much simpler to use but once the data leaves the host, you can only pray to god (or the smurfs) that it gets to its destination.

And currently, youre using... TCP.. Which demands that you start off communication with a three-way-handshake (which you don't).

First of all I think you should try to print the exception by typing System.err.println(e); in your catch so you can try to determine what's going wrong. It could be that the server is not set to recieve data on port 9999.
It could also be that unless you send some data, the server closes the connection after some timout. Did you write the server side application as well?

This post has been edited by Gloin: 16 Aug, 2008 - 08:23 AM
User is offlineProfile CardPM
+Quote Post

saumya9888
RE: How To Check For Listening Server On Entire LAN
16 Aug, 2008 - 09:58 AM
Post #3

New D.I.C Head
*

Joined: 8 Aug, 2008
Posts: 22


My Contributions
QUOTE(Gloin @ 16 Aug, 2008 - 09:20 AM) *

When you work with sockets, you can use different transport protocols among which TCP and UDP are the most common types. I'm sure you've heard of TCP, especially in the context TCP/IP. Basically, TCP is a reliable connection where a number of built in methods will help making sure your data is delivered, however TCP requires that you follow a certain protocol. UDP instead is an unreliable, best-effort service that doesn't care what happens to your data. It's much simpler to use but once the data leaves the host, you can only pray to god (or the smurfs) that it gets to its destination.

And currently, youre using... TCP.. Which demands that you start off communication with a three-way-handshake (which you don't).

First of all I think you should try to print the exception by typing System.err.println(e); in your catch so you can try to determine what's going wrong. It could be that the server is not set to recieve data on port 9999.
It could also be that unless you send some data, the server closes the connection after some timout. Did you write the server side application as well?



You didn't get it or did you.I am designing an application which can act both as a server and a client depending on whether there is any other server on network.I am trying to scan all local ip addresses to check whether anyone on network has a server listening or not and if it finds one it joins the server and if it doesn't, it creates a new server and start listening for connections from other machines on network in continuation of the code i've shown i'm starting a serversocket if it was unable to join any other server.

By the way, i've modified my code.Plz tell me if it will work or not.Actually in place of ipaddress i've just entered ""...

CODE
try {
            skt = new Socket([color=#FF0000]""[/color], 9999);
            jLabel1.setText(skt.getInetAddress() + "");
        } catch (IOException ex) {
            connected = false;
        }


it is working fine on my machine when i run 2 copies of this program.But i doubt that it will work on network...
I can attach my whole program if you want...

This post has been edited by saumya9888: 16 Aug, 2008 - 09:59 AM
User is offlineProfile CardPM
+Quote Post

Gloin
RE: How To Check For Listening Server On Entire LAN
16 Aug, 2008 - 11:14 AM
Post #4

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
I think if you post more of the code it will be easier to see what could be causing the problem (If there still is one).
User is offlineProfile CardPM
+Quote Post

sl4ck3r
RE: How To Check For Listening Server On Entire LAN
17 Aug, 2008 - 09:29 PM
Post #5

D.I.C Head
Group Icon

Joined: 22 Sep, 2007
Posts: 119


Dream Kudos: 25
My Contributions
you need to understand basic networking principles before you attempt a project such as this. You obviously do not because your iterating over one octet and going from 1 to 255 not 0 to 254 (the correct range).
User is offlineProfile CardPM
+Quote Post

Gloin
RE: How To Check For Listening Server On Entire LAN
18 Aug, 2008 - 12:33 AM
Post #6

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
Actually he's iterating over 1 to 254 so it is the correct range. Although, maybe a broadcast had been more convenient.
User is offlineProfile CardPM
+Quote Post

saumya9888
RE: How To Check For Listening Server On Entire LAN
22 Aug, 2008 - 10:16 AM
Post #7

New D.I.C Head
*

Joined: 8 Aug, 2008
Posts: 22


My Contributions
hi this is the only amount of code i'm working on.This is the code till now and it obviously causes no error to display instead it should increment value of "i" by 1 and loop must go on.It should break when it encounters a server at specified port listening for connection. I dont know if there is a blocking code present but the program just hangs and doesn't come out of the loop.I would be thankful if u let me know that how does skt variable behave when an IO Exception occurs ot its reusable again.

CODE
for (; i < 254;) {
            try {
                skt = new Socket("10.19.41." + i, CHAT_PORT);
                jLabel1.setText("Connected");
                connected = true;
                break;
            } catch (IOException ex) {                
                i++;
            }
        }


Plz tell me where i'm going wrong. crying.gif crying.gif
User is offlineProfile CardPM
+Quote Post

saumya9888
RE: How To Check For Listening Server On Entire LAN
22 Aug, 2008 - 11:22 AM
Post #8

New D.I.C Head
*

Joined: 8 Aug, 2008
Posts: 22


My Contributions
QUOTE(saumya9888 @ 22 Aug, 2008 - 11:16 AM) *

hi this is the only amount of code i'm working on.This is the code till now and it obviously causes no error to display instead it should increment value of "i" by 1 and loop must go on.It should break when it encounters a server at specified port listening for connection. I dont know if there is a blocking code present but the program just hangs and doesn't come out of the loop.I would be thankful if u let me know that how does skt variable behave when an IO Exception occurs ot its reusable again.

CODE
for (; i < 254;) {
            try {
                skt = new Socket("10.19.41." + i, CHAT_PORT);
                jLabel1.setText("Connected");
                connected = true;
                break;
            } catch (IOException ex) {                
                i++;
            }
        }


Plz tell me where i'm going wrong. crying.gif crying.gif

hey people all i have posted in the beginning was crap.This program is working fine.It checks for a server on entire LAN as i needed but there is a big problem.It takes around 10 seconds to check for one ip address.Can this problem be resolved.Man i'm happy that my logic has finally succeeded but sad at the same time that system is showing an unexpected behavior.Plz help
User is offlineProfile CardPM
+Quote Post

Gloin
RE: How To Check For Listening Server On Entire LAN
23 Aug, 2008 - 08:43 AM
Post #9

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
I still think it has to do with you creating a socket but then you don't follow up on it. So when you don't send an acknowledgement the server and client waits until timeout and then closes down the socket. Meanwhile, nothing happens.

This is just speculations though.
User is offlineProfile CardPM
+Quote Post

saumya9888
RE: How To Check For Listening Server On Entire LAN
23 Aug, 2008 - 09:38 AM
Post #10

New D.I.C Head
*

Joined: 8 Aug, 2008
Posts: 22


My Contributions
QUOTE(Gloin @ 23 Aug, 2008 - 09:43 AM) *

I still think it has to do with you creating a socket but then you don't follow up on it. So when you don't send an acknowledgement the server and client waits until timeout and then closes down the socket. Meanwhile, nothing happens.

This is just speculations though.


Actually i did some googling on this and found out that this is a common problem, new Socket has a default timeout of 20 sec and it takes that much of time if ip address it is trying to connect doesnt exist to show connection timed out error.BTW: ive added System.err.println(e) so i got that error message.Neways now i'm trying to use Socket.connect(SocketAddress,timeout) to set a timeout<20 seconds if u have any other way to change timeout plz tell me and also tell me if my new approach will work or not NO luck till now. i'm trying if i cannot solve it i have to use a field n which ip address can be entered.PLZ help me...
User is offlineProfile CardPM
+Quote Post

Gloin
RE: How To Check For Listening Server On Entire LAN
23 Aug, 2008 - 09:51 AM
Post #11

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
If it is as I think (still I'm only speculating), whenever a 'server' is found, the client should send an empty packet to that 'server' upon which the 'server' should respond by returning an empty packet. Now communication can commence. The third packet (second packet sent by the client) can contain information but is still part of the TCP three-way-handshake.

I think pbl somewhere posted a client/server example that you could take a look at. In the example he establishes a connection between a client and a server and then sends a string back and forth.

I guess also you should change the port number for each time you find a 'server' dunno if that is necessary though.
User is offlineProfile CardPM
+Quote Post

saumya9888
RE: How To Check For Listening Server On Entire LAN
23 Aug, 2008 - 10:00 AM
Post #12

New D.I.C Head
*

Joined: 8 Aug, 2008
Posts: 22


My Contributions
QUOTE(Gloin @ 23 Aug, 2008 - 10:51 AM) *

If it is as I think (still I'm only speculating), whenever a 'server' is found, the client should send an empty packet to that 'server' upon which the 'server' should respond by returning an empty packet. Now communication can commence. The third packet (second packet sent by the client) can contain information but is still part of the TCP three-way-handshake.

I think pbl somewhere posted a client/server example that you could take a look at. In the example he establishes a connection between a client and a server and then sends a string back and forth.

I guess also you should change the port number for each time you find a 'server' dunno if that is necessary though.



Guys Thank you all for prompt replies i've learned a lot from these posts but finally i've figured out the right way and it is working perfectly

CODE
for (; i < 254;) {            
            sktarr[i]=new Socket();
            sktaddr[i]=new InetSocketAddress("10.19.41." + i, CHAT_PORT);
            try {
                sktarr[i].connect(sktaddr[i],1);
                jLabel1.setText("Connected");
                connected = true;
                break;
            } catch (IOException ex) {                
                i++;
            }
        }


For now my problem is solved but since i'm working on a large project i may come back asking for help.Thank you all once again. smile.gif smile.gif smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:45AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month