Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Socket problem, please help

Thread view: 
pieterblomme - 23 Dec 2005 09:42 GMT
Hi,
I'm currently working on a Chat Program, and i'm having problems with
my sockets.  Alltough I'm sure I use the right port and IP adress, the
socket still can't connect.
What could cause this problem?
Could it be because of my router or because of proxy settings?
Gordon Beaton - 23 Dec 2005 11:27 GMT
> I'm currently working on a Chat Program, and i'm having problems
> with my sockets. Alltough I'm sure I use the right port and IP
> adress, the socket still can't connect. What could cause this
> problem? Could it be because of my router or because of proxy
> settings?

Many things could cause the problem. Start by posting the exact stack
trace you get when the connection fails.

Then try using a tool like telnet to connect to the same address and
port number and see what happens. If you get a similar error, you can
assume that this particular problem isn't caused by the client.

Normally if you get "Connection refused" it indicates that the server
isn't listening at the given address:port, or you aren't connecting
where you think you are.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Robert M. Gary - 23 Dec 2005 17:13 GMT
Start with a ping to see if you can get to the remote machine...

=> ping remotemachine

If that works, do a netstat -a on the remote machine to see if the
socket is in a listening state..

=>netstat -a |grep 1234
zero - 23 Dec 2005 18:52 GMT
> Hi,
> I'm currently working on a Chat Program, and i'm having problems with
> my sockets.  Alltough I'm sure I use the right port and IP adress, the
> socket still can't connect.
> What could cause this problem?
> Could it be because of my router or because of proxy settings?

I always have problems with my socks too.  Can't ever seem to find a
matching pair.

Could be your router, could be proxy settings, could be a firewall, could
be a dns problem, ...  What is the exact error message?  Does it work on
localhost?  Is the server running (and listening) before the client tries
to connect?  Some more information - and testing - would be helpful.

Signature

Beware the False Authority Syndrome

pieterblomme - 23 Dec 2005 20:01 GMT
  /**
    * Connects this Radio to another user or the main server.  The
Radio stops listening
    *
    * @param  host The address of the other user
    * @return     true if succes
    */
   public boolean connect(String host)
   {
       //still have to find out how to add the none status with an or
statement
       if(status == "none" | status == "listening")
       {
           try
           {
               //create an inner class for a thread that will run the
socket, since otherwise, the main thread will block
               class SocketThread extends Thread
               {
                   //instance variables
                   private Socket socket;
                   private int port;
                   private String host;
                   //constructor
                   public SocketThread(Socket socket, int port, String
host)
                   {
                       this.socket = socket;
                       this.port = port;
                       this.host = host;
                   }
                   //run method
                   public void run()
                   {
                       try
                       {
                          Socket socket = new Socket(host, port);
                       }
                       catch(IOException e)
                       {
                           System.out.println(e.getMessage());
                       }
                   }
               }
               //create a socket for the connection
               SocketThread socketThread = new SocketThread(socket,
port, host);
               socketThread.start();
               //Announce that you are connected
               System.out.println("Connected");
               //specifie the input and output streams
               input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
               output =  new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
               //change the status
               status = "talking";
               //return that the action was succesfull
               return true;
           }
           //catch possible IOExceptions
           catch(IOException e)
           {
               System.out.println(e.getMessage());
               return false;
           }
       }
       //if the Radio is busy, it cannot connect, so it returns false
       else
       {
           return false;
       }
   }

That's the code of the connect method of my chat system
pieterblomme - 23 Dec 2005 20:02 GMT
  /**
    * Connects this Radio to another user or the main server.  The
Radio stops listening
    *
    * @param  host The address of the other user
    * @return     true if succes
    */
   public boolean connect(String host)
   {
       //still have to find out how to add the none status with an or
statement
       if(status == "none" | status == "listening")
       {
           try
           {
               //create an inner class for a thread that will run the
socket, since otherwise, the main thread will block
               class SocketThread extends Thread
               {
                   //instance variables
                   private Socket socket;
                   private int port;
                   private String host;
                   //constructor
                   public SocketThread(Socket socket, int port, String
host)
                   {
                       this.socket = socket;
                       this.port = port;
                       this.host = host;
                   }
                   //run method
                   public void run()
                   {
                       try
                       {
                          Socket socket = new Socket(host, port);
                       }
                       catch(IOException e)
                       {
                           System.out.println(e.getMessage());
                       }
                   }
               }
               //create a socket for the connection
               SocketThread socketThread = new SocketThread(socket,
port, host);
               socketThread.start();
               //Announce that you are connected
               System.out.println("Connected");
               //specifie the input and output streams
               input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
               output =  new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
               //change the status
               status = "talking";
               //return that the action was succesfull
               return true;
           }
           //catch possible IOExceptions
           catch(IOException e)
           {
               System.out.println(e.getMessage());
               return false;
           }
       }
       //if the Radio is busy, it cannot connect, so it returns false
       else
       {
           return false;
       }
   }

That's the code of the connect method of my chat system
pieterblomme - 24 Dec 2005 06:54 GMT
I think the problem should be in my code, but it could allways be the
fault of my network too.
I'm testing the system on a local network with two linuxes and a
windows.

If I print e.getMessage(), I get Socket not connected because i try to
get the input- and outputstream of the socket, and a few minutes later
Connection timed out.
Alun Harford - 25 Dec 2005 09:38 GMT
>I think the problem should be in my code, but it could allways be the
> fault of my network too.
[quoted text clipped - 4 lines]
> get the input- and outputstream of the socket, and a few minutes later
> Connection timed out.

Post the server code and we might be able to help you (or at least the code
that handles listening for connections)

Alun Harford


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.