We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Couldn't connect to 195.221.155.146 with origine from file

thibal
thibal asked
on
Medium Priority
218 Views
Last Modified: 2010-03-30
I've found a java server program in "java in a nutshell".
I want to try it and adapt it for my own program.
So I run the server program on a local server (NT) but the client applet cannot connect to this IP.

Could you explain this?
Is the  Socket's declaration wrong?(See classes at the end)
Perhaps the server class must be inserted in the project...

I'm looking for code's exemples, server and client.

Here is my code. Do you see any mistakes with it?
The applet send a string, the server reverse it and return it.

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import StreamListener;

public class Exemple3 extends Applet
{
   public static final int Port=1548;
   Socket s;
   DataInputStream in;
   PrintStream out;
   TextField inputfield;
   TextArea outputarea;
   StreamListener listener;

   public void init()
   {
      try
      {
         s = new Socket("195.221.155.146",Port);
         in = new DataInputStream(s.getInputStream());
         out = new PrintStream(s.getOutputStream());
         inputfield = new TextField();
         outputarea = new TextArea();
         outputarea.setEditable(false);
         this.setLayout(new BorderLayout());
         this.add("North",inputfield);
         this.add("Center",outputarea);
            
         listener = new StreamListener(in, outputarea);
      
         this.showStatus("Connnected to "
         + s.getInetAddress().getHostName()
         + " : " +s.getPort());
         }
         catch (IOException e)
         {
            this.showStatus(e.toString());
         }
      }

   public boolean action(Event e, Object what)
   {
       if (e.target==inputfield)
           {
         out.println((String)e.arg);
         inputfield.setText("");
         return true;
         }
         return false;
   }
}
________________________

import java.io.*;
import java.awt.*;
class StreamListener extends Thread
{
      DataInputStream in;
      TextArea output;

      public StreamListener(DataInputStream in, TextArea output)
      {
            this.in = in;
            this.output = output;
            this.start();
      }

      public void run()
      {
            String line;
            try
            {
                  for(;;)
                  {
                        line = in.readLine();
                        if (line==null)
                              break;
                        output.setText(line);
                  }
            }
            catch(IOException e)
            {
                  output.setText(e.toString());
            }
            finally
            {
                  output.setText("Connexion closed by server.");
            }
      }
}
_______________________________
import java.io.*;
import java.net.*;
import Connection;

public class server2 extends Thread
{
      public final static int Default_Port=1548;
      protected int port;
      protected ServerSocket listen_socket;

      public static void fail(Exception e, String msg)
      {      
            System.out.println(msg + " ça ne marche pas :"+ e);
            System.exit(1);
      }

      public server2(int port)
      {
            if(port==0)
                  port=Default_Port;

            this.port=port;

            try
            {
                  listen_socket= new ServerSocket(port);
            }
            catch (IOException e)
            {
                  fail(e,"Exception creating server socket");
            }
            System.out.println("Server : listening on port " +port);
            this.start();
      }

      public void run()
      {
            try
            {
                  while(true)
                  {
                        Socket client_socket = listen_socket.accept();
                        Connection c = new Connection(client_socket);
                  }
            }
            catch(IOException e)
            {
                  fail(e,"Exception while listening for connections");
            }
      }

      public static void main(String[] args)
      {
            int port=0;
            if (args.length==1)
            {
                  try
                  {
                        port = Integer.parseInt(args[0]);
                  }
                  catch(NumberFormatException e)
                  {
                        port=0;
                  }
            }
            new server2(port);
      }
}
____________________________
import java.io.*;
import java.net.*;

class Connection extends Thread
{
      protected Socket client;
      protected DataInputStream in;
      protected PrintStream out;

      public Connection(Socket client_socket)
      {
            client = client_socket;
            try
            {
                  in=new DataInputStream(client.getInputStream());
                  out=new PrintStream(client.getOutputStream());
            }
            catch(IOException e)
            {
                  try
                  {
                        client.close();
                  }
                  catch(IOException e2)
                  {
                  }
                  System.err.println("Exception while getting socket streams : " +e);
                  return;
            }
            this.start();
      }

      public void run()
      {
            String line;
            StringBuffer revline;
            int len;
            try
            {
                  for(;;)
                  {
                        line = in.readLine();
                        if(line ==null)
                              break;
                        len=line.length();
                        revline= new StringBuffer(len);
                        for(int i=len-1;i>=0;i--)
                              revline.insert(len-1-i,line.charAt(i));
                        out.println(revline);
                  }
            }
            catch(IOException e)
            {
            }
            finally
            {
                  try
                  {
                        client.close();
                  }
                  catch(IOException e2)
                  {
                  }
            }
      }
}
Comment
Watch Question

Author

Commented:
Edited text of question
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Commented:
Maybe that the link between the FAQ comment and the Socket class is not clear in my
answer. The code below is copied from the source of Socket.java:

    public Socket(String host, int port)
      throws UnknownHostException, IOException
    {
      this(InetAddress.getByName(host), port, null, 0, true);
    }

As you see, the constructor makes a call to InetAddress.getByName(host)...

Author

Commented:
Fontaine
I have replace the IP adress with what you suggest me.
This exception doesn't appear!
Thank's a lot.

But...
A new mistake is appearing so I will search and I will come again in several days.
load:Exemple3 can't be instantiated
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.