socket programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpuser123
    New Member
    • Dec 2009
    • 108

    socket programming

    I just started socket programming in java.I read a few tutorial and came up with the following script

    import java.net.*;

    public class socket {

    /**
    * @param args
    */
    public static void main(String[] args) throws Exception {
    String sentence;String modifiedSentenc e;




    Socket clientSocket = new Socket("hostnam e", 8123);
    System.out.prin tln(clientSocke t.getPort());
    }

    }


    Now,when I run the codes i get the following exceptiions

    Exception in thread "main" java.net.Unknow nHostException: hostname
    at java.net.PlainS ocketImpl.conne ct(Unknown Source)
    at java.net.SocksS ocketImpl.conne ct(Unknown Source)
    at java.net.Socket .connect(Unknow n Source)
    at java.net.Socket .connect(Unknow n Source)
    at java.net.Socket .<init>(Unknow n Source)
    at java.net.Socket .<init>(Unknow n Source)
    at socket.main(soc ket.java:15)

    I hava no idea what it is exactly and the pproblem with the codes here..I will be really obliged to those who can enlighten me a bit

    Thanks in adv ...
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    which IP-Address is assignet to "hostname" on your system?
    You can look that up in .../etc/hosts file.
    Or with "ping hostname".

    I guess you haven't assigned any IP-address to the name of the host there.
    So the command "new Socket("hostnam e", 8123); " won't run.
    Instead of String "hostname", try to give its ip-address here. Maybe the DNS-server doesn't work.
    But first, try to ping with this IP-address. If you can't ping, you (most likely) can't connect to this server over sockets. You have to check with the network people then why your access to this server is blocked.

    Comment

    Working...