hi everyone,

iam trying to connect to server here is the code for connecting:

public void CONNECT_TO()
   {
                    int port=200;


                    if(t1.getText()=="")
                         serv="localhost";
       else
                         serv=t1.getText();

                    try
       {
                        port = Integer.parseInt(t2.getText());

       }
                        catch (Exception e) { }

                    try
                                                  {
                                                                   c_s = new Socket(serv, port);
                                                                   l3.setText("Connected to server " +c_s.getInetAddress()+":" +c_s.getPort());
                                                                   form F=new form();
                                                                   F.show();
                                                                   F.setResizable(false);
                                                                   this.hide();
                                                  }
                                                  catch (UnknownHostException e)
                                                  {
                                                                   System.out.println(e);
                                                                   System.out.println("Error in connecting Server");
                                                                   
                                                  }
                                                  catch (IOException e)
                                                  {
                                                                   System.out.println(e);
                                                                   System.out.println("Error");
                                                                   
                                                  }
   }

It runs fine but when i enter localhost for ip address it gives me the error for the second exception, IOEXCEPTION.

anyone knows why?

thank you

ok everyone,

now i can connect to the server, but using netstat -a command which lists the ports available.

This is going to be difficult for user to find the port.

what should i do ?

thank you

you're always going to need to know both the IP address AND the port AND the protocol to successfully talk with any server (and that is assuming you also are allowed to talk to it, know the usernames and passphrases needed, etc.).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.