Solved

Couldn't connect to 195.221.155.146 with origine from file

Posted on 1998-06-23
4
181 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)
                  {
                  }
            }
      }
}
0
Comment
Question by:thibal
  • 2
  • 2
4 Comments
 

Author Comment

by:thibal
ID: 1223527
Edited text of question
0
 
LVL 5

Accepted Solution

by:
fontaine earned 70 total points
ID: 1223528
First of all, be sure that 195.221.155.146 is the same IP as the one of the machine that
has served the applet. If not, the applet security manager will not allow you to connect.

If this is OK, then I have the feeling that you are falling into the problem related in the Java Programmer's FAQ (http://www.afu.com/javafaq.html). I copy here the section:

-------

7.3 If I call the InetAddress.getByName() method with an IP-address-string argument, like "192.168.0.1",I get an UnknownHostException on some platforms, but not others. Code like
Socket sock = new Socket("155.152.5.1", 23); triggers the exception. Why?

A. This is a platform difference that arises out of different semantics in the underlying network libraries, and is [said to be, but  subject to confirmation] fixed in JDK 1.1. On Solaris and Windows NT, the IP address string only works for IP addresses that have an associated hostname. On Linux and Windows 95, the IP address string works in all cases.
http://www.cdt.luth.se/~peppar/java/InetAddress/ has a workaround.

When InetAddress is instantiated with an IP address, a reverse DNS lookup is done. If the IP address is not associated with a valid hostname, the instantiation will fail. This is part of anti DNS-spoofing, and in JDK 1.1 works because the reverse lookup will not occur until the hostname is asked for. So in JDK 1.1, InetAddress in = InetAddress.getByName("155.152.5.1"); [Note: this info is still to be confirmed. Net gurus?]

--------------------

The applet security manager will have to verify if you try to connect back to the server that
has served the applet. It will attempt to lookup the host name corresponding to the
IP. If it fails, the connection will be refused.

Generally speaking, writing something like the following is better:

URL serverURL = getDocumentBase();
s = new Socket(serverURL.getHost(),Port);

because the applet code is then independant of the IP of the machine that has served
the applet.

0
 
LVL 5

Expert Comment

by:fontaine
ID: 1223529
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)...
0
 

Author Comment

by:thibal
ID: 1223530
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
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array220 challenge 8 47
wordappend challenge 8 85
Java and GPO 11 47
Groovy:unable to resolve class error 2 31
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now