Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Error In using socket.getOutputStream

Posted on 2000-04-17
17
372 Views
Last Modified: 2008-02-01
I am trying to write a servlet that will launch a telnet connection to a Unix server. However, I got some exceptions like this :

System.out.println==>Creating a socket to handle it
System.out.println==>Here is the data inside log_In
255|253|24|255|253| 255|253|#255|253|$
System.out.println==>Right before I get the output Stream!
java.lang.NullPointerException
at java.net.PlainSocketImpl.getOutputStrea(PlainSocketImpl.java, Compiled Code)
at java.lang.Exception.<init>(Exception.java, Compiled Code)
at java.lang.RuntimeException.<init>(RuntimeException.java, Compiled Code)
at java.lang.NullPointerException.<init>(NullPointerException.java, Compiled Code)
at java.io.FileOutputStream.<init>(FileOutputStream.java, Compiled Code)
at java.net.SocketOutputStream.<init>(SocketOutputStream.java, Compiled Code)
at java.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java, Compiled Code)
at java.net.Socket$2.run(Socket.java, Compiled Code)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.Socket.getOutputStream(Socket.java, Compiled Code)
at SocketMangmnt.sendData(SocketMangmnt.java, Compiled Code)
at SocketMangmnt.sUnix(SocketMangmnt.java, Compiled Code)

I create a plain socket which connect to the telnet port of my Unix server like this :
         loginsoc = new Socket(host, 23);
         loginsoc.setTcpNoDelay(true);
         System.out.println("Creating a socket to handle it");

Then, I read the inputstream of that socket and I got those
255|253|24....(without the '|').  That should be the negotiation of the telnet protocol. Now, I use my sendData method (show below) to send a 3-byte data back to the Unix server and I got the Exception immediately.

private boolean sendData(byte bytestrm[], int size)
  {
      try
      {
          System.out.println("Right before I get the output Stream!");
          DataOutputStream doutstrm = new DataOutputStream(loginsoc.getOutputStream());
          doutstrm.write(bytestrm, 0, size);
          //TTTTTTTTTT  Print out the data it sends out
          System.out.println("Here is the data being sent out");
          for (int i=0;i<size;i++)
          {
              if (((int)bytestrm[i]>=32) && ((int)bytestrm[i]<=127))
                  System.out.print((char)bytestrm[i]);
              else
                  System.out.print(bytestrm[i]+"|");
          }

          //TTTTTTTTTTTT
          doutstrm.close();
          System.out.println(" --");
      }
      catch (IOException ioe)
      {
          return false;
      }
      return true;
  }

I don't know what causes the problem. It doesn't seem it is proxy server/firewall problem.  When I check the the socket properties, it still bounds to the local port and the remote telnet port.

I would be appreciated if somebody can get me a hand on this.
Thank you very much.
0
Comment
Question by:UTEK
17 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 2724349
A code snippet may help.
0
 

Author Comment

by:UTEK
ID: 2724372
Edited text of question.
0
 

Author Comment

by:UTEK
ID: 2724394
Edited text of question.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:stalefish
ID: 2724750
Can you do a check before you use the OutputStream to make sure it is not null?

if(loginsoc == null) System.out.println("Login Socket is null");
....

if(doutstrm == null) System.out.println("DoutStrm is Null");
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2724771
hehe, an exception in Exception.<init> :-)
0
 

Author Comment

by:UTEK
ID: 2725241
Hm....I know the socket is not null. I can print out the localaddress and the Inetaddress.  The problem is most likely to be the socket.getOutputStream somehow returns a null pointer. But it doesn't make sense.
0
 

Expert Comment

by:Ash_
ID: 2725382
what are you running the servlets on?? platform??

in the stack trace,this line is present :

at java.security.AccessController.doPrivileged(Native Method)

does the servlet runner you are using have a servlet "sandbox" like a applet sandbox ??

finally, try placing the servlet code in the servlet runner's classpath ( not the servlets directory)
0
 

Author Comment

by:UTEK
ID: 2727131
I am using the ServletExec from New Atlanta on NT workstation with peer web Service 3.0.

I have checked the security of my servlet runner and it grants all premittions to the local servlets.  If there is a servlet sandbox, it shouldn't allow me to connect to anything.
0
 

Author Comment

by:UTEK
ID: 2727922
Adjusted points from 75 to 100
0
 

Author Comment

by:UTEK
ID: 2731055
I finally I find out the source of the problem, but I don't know why it happenes like that. Does anybody know why?

I wrote a small test program like this:
<pre>
import java.io.*;
import java.net.*;

public class testconnection
{

  public testconnection() {
  }

  public static void main(String[] args)
  {
      Socket loginsoc;
      DataInputStream instrm;
      DataOutputStream doutstrm;

      try
      {
          loginsoc = new Socket("127.56.1.3", 23);
          loginsoc.setTcpNoDelay(true);
          System.out.println("Creating a socket to handle it");
 //<b>Line A</b>    instrm = new DataInputStream(loginsoc.getInputStream());
 //<b>Line B</b>    doutstrm = new DataOutputStream(new BufferedOutputStream(loginsoc.getOutputStream()));
      }
      catch (Exception e)
      {
          System.out.println("More likely socket Exception");
          return;
      }

      try
      {
          byte bstrm[] = new byte[1024];
 //<b>Line C</B>   DataInputStream instrm = new DataInputStream(new BufferedInputStream(loginsoc.getInputStream()));
 //<b>Line D</B>   instrm = new DataInputStream(loginsoc.getInputStream());
          int counter = instrm.read(bstrm);
          //KT DB
          instrm.close();

          /*Read the instream byte by byte and put them into an int array
            Since C is unsigned byte but java is signed byte, we have to use
            int which is 32-bit to keep a one byte information to avoid loss of
            info */
          if (counter <= 0)
          {
              System.out.println("Nothing in the in stream");
              return;
          }

          //TTTTTTTTTT  Print out the data it got
          System.out.println("Here is the data I get");
          for (int i=0;i<counter;i++)
          {
              if ((bstrm[i]>=32) && (bstrm[i]<=127))
                  System.out.print((char)bstrm[i]);
              else
                  System.out.print(bstrm[i]+"|");
          }
          System.out.println(" ");
          //TTTTTTTTTTTT
      }
      catch (Exception e)
      {
          System.out.println("Exception" + e.getClass().getName()+ ": " + e.getMessage());
          return;
      }


       try
      {
          byte bytestrm[] = {(byte)255, (byte)254, (byte)24};
          System.out.println("Right before I get the output Stream!");
 //<b>Line E</B>   DataOutputStream doutstrm = new DataOutputStream(loginsoc.getOutputStream());
 //<b>Line F</B>   doutstrm = new DataOutputStream(new BufferedOutputStream(loginsoc.getOutputStream()));
          doutstrm.write(bytestrm, 0, 3);
          //TTTTTTTTTT  Print out the data it sends out
          System.out.println("Here is the data being sent out");
          for (int i=0;i<bytestrm.length;i++)
          {
              if (((int)bytestrm[i]>=32) && ((int)bytestrm[i]<=127))
                  System.out.print((char)bytestrm[i]);
              else
                  System.out.print(bytestrm[i]+"|");
          }

          //TTTTTTTTTTTT
          doutstrm.close();
          System.out.println(" --");
      }
      catch (IOException ioe)
      {
          return;
      }

  }
}
0
 

Author Comment

by:UTEK
ID: 2731060
I finally I find out the source of the problem, but I don't know why it happenes like that. Does anybody know why?

I wrote a small test program like this:
<pre>
import java.io.*;
import java.net.*;

public class testconnection
{

  public testconnection() {
  }

  public static void main(String[] args)
  {
      Socket loginsoc;
      DataInputStream instrm;
      DataOutputStream doutstrm;

      try
      {
          loginsoc = new Socket("127.56.1.3", 23);
          loginsoc.setTcpNoDelay(true);
          System.out.println("Creating a socket to handle it");
 //<b>Line A</b>    instrm = new DataInputStream(loginsoc.getInputStream());
 //<b>Line B</b>    doutstrm = new DataOutputStream(new BufferedOutputStream(loginsoc.getOutputStream()));
      }
      catch (Exception e)
      {
          System.out.println("More likely socket Exception");
          return;
      }

      try
      {
          byte bstrm[] = new byte[1024];
 //<b>Line C</B>   DataInputStream instrm = new DataInputStream(new BufferedInputStream(loginsoc.getInputStream()));
 //<b>Line D</B>   instrm = new DataInputStream(loginsoc.getInputStream());
          int counter = instrm.read(bstrm);
          //KT DB
          instrm.close();

          /*Read the instream byte by byte and put them into an int array
            Since C is unsigned byte but java is signed byte, we have to use
            int which is 32-bit to keep a one byte information to avoid loss of
            info */
          if (counter <= 0)
          {
              System.out.println("Nothing in the in stream");
              return;
          }

          //TTTTTTTTTT  Print out the data it got
          System.out.println("Here is the data I get");
          for (int i=0;i<counter;i++)
          {
              if ((bstrm[i]>=32) && (bstrm[i]<=127))
                  System.out.print((char)bstrm[i]);
              else
                  System.out.print(bstrm[i]+"|");
          }
          System.out.println(" ");
          //TTTTTTTTTTTT
      }
      catch (Exception e)
      {
          System.out.println("Exception" + e.getClass().getName()+ ": " + e.getMessage());
          return;
      }


       try
      {
          byte bytestrm[] = {(byte)255, (byte)254, (byte)24};
          System.out.println("Right before I get the output Stream!");
 //<b>Line E</B>   DataOutputStream doutstrm = new DataOutputStream(loginsoc.getOutputStream());
 //<b>Line F</B>   doutstrm = new DataOutputStream(new BufferedOutputStream(loginsoc.getOutputStream()));
          doutstrm.write(bytestrm, 0, 3);
          //TTTTTTTTTT  Print out the data it sends out
          System.out.println("Here is the data being sent out");
          for (int i=0;i<bytestrm.length;i++)
          {
              if (((int)bytestrm[i]>=32) && ((int)bytestrm[i]<=127))
                  System.out.print((char)bytestrm[i]);
              else
                  System.out.print(bytestrm[i]+"|");
          }

          //TTTTTTTTTTTT
          doutstrm.close();
          System.out.println(" --");
      }
      catch (IOException ioe)
      {
          return;
      }

  }
}
</PRE>
0
 

Author Comment

by:UTEK
ID: 2731100
LINE C AND LINE E have been commented out.

See above for the test program
If I do the Socket.getOutputStream at Line B, it works fine without any exceptions. But If I do it at Line F, it gets me the null pointer exception.

On the other, my Socket.getInputStream can be either on Line A or Line D, both work fine.

Weird? Why?
0
 

Author Comment

by:UTEK
ID: 2731128
LINE C AND LINE E have been commented out.

See above for the test program
If I do the Socket.getOutputStream at Line B, it works fine without any exceptions. But If I do it at Line F, it gets me the null pointer exception.

On the other, my Socket.getInputStream can be either on Line A or Line D, both work fine.

Weird? Why?
0
 

Author Comment

by:UTEK
ID: 2731320
LINE C AND LINE E have been commented out.

See above for the test program
If I do the Socket.getOutputStream at Line B, it works fine without any exceptions. But If I do it at Line F, it gets me the null pointer exception.

On the other, my Socket.getInputStream can be either on Line A or Line D, both work fine.

Weird? Why?
0
 

Author Comment

by:UTEK
ID: 2731447
LINE C AND LINE E have been commented out.

See above for the test program
If I do the Socket.getOutputStream at Line B, it works fine without any exceptions. But If I do it at Line F, it gets me the null pointer exception.

On the other, my Socket.getInputStream can be either on Line A or Line D, both work fine.

Weird? Why?
0
 
LVL 3

Accepted Solution

by:
falter earned 100 total points
ID: 2731720
UTEK,

I think, the problem is instrm.close(),
this will not only close the InputStream it also closes the FileDescriptor of the Socket, because the InputStream and the OutputStream are based on the same FileDescriptor.
The Socket is holding this FileDescriptor in its SocketImpl.
 InputStream and OutputStream are only References to the same FileDescriptor, changing this referenced Object will of cause be visible in InputStream, OutputStream and the Socket.
So closing the Socket will also close OutputStream and InputStream. Closing the InputStream will close ...

If you have access to the Java sources have a look at
java.net.Socket
java.net.SocketImpl
java.net.PlainSocketImpl

Hope this helps a little bit!

regards
jf
0
 

Author Comment

by:UTEK
ID: 2732313
Thanks falter and Thanks you guys.
It seems it is my InputStrem.close() causing the problem. It also causes an socket.exception : descriptor not a socket being thrown.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Way to decrease size of apk file 9 86
check java version using powershell 13 168
null output 3 35
Cisco ASA: Java web start no go, asdm launcher no go 3 34
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

808 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