Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7688893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:02:02+00:00 2026-05-31T20:02:02+00:00

I’m having trouble transitioning to Java from C/C++ for my Telnet interface to some

  • 0

I’m having trouble transitioning to Java from C/C++ for my “Telnet” interface to some modules we work with here. I want to be able to establish a connection with a card that, after starting it’s command line interface, waits for a connection and serves up a prompt (“OK>”) to the clients. This works fine for both C and C# clients I’ve written, but the Java has given me some issues. I’ve attached some code that I grabbed from some examples online, but so far, all I can ascertain for sure is that the socket is being created.

Code:

   private boolean CreateTelnetSession()
   {
        try
        {
            _socket = new Socket();
            _socket.connect(new InetSocketAddress(_ipAddr, _ipPort));
            _socket.setSoTimeout(10000);
            _socket.setKeepAlive(true);

            _out = new PrintWriter(_socket.getOutputStream(), true);
            _in = new BufferedReader(new InputStreamReader(_socket.getInputStream()));

            _out.println("\r\n");

            System.out.println(_in.readLine());

            return true;
        }
        catch(Exception e)
        {
            System.out.println("Exception!");
        }
        return false;
    }

The socket SEEMS to be created correctly, and when the program shuts down, I can see the session close on the card(s) I’m trying to talk to, but I don’t see the carriage return/line feed echoed on the card as I would expect, or a prompt returned via the InputStream. Is it possible that it’s a character encoding issue? Am I doing something incorrectly with the streams (crossing them!?!)? Any insight at all? When I get over this initial learning curve, I would like to acknowledge how easy Java makes these socket reads and writes, but until then…

I read this post:
java simple telnet client using sockets

It seems similar to what I’m running up against, but it’s not the same. I’m willing to take the rep hit if someone has seen something on here that resolves my issue, so feel free to let me know, bluntly, what I missed.

Edit:

private boolean CreateTelnetSession()
{
    try
    {
        _socket = new Socket();
        _socket.connect(new InetSocketAddress(_ipAddr, _ipPort));
        _socket.setSoTimeout(10000);
        _socket.setKeepAlive(true);

        _out = new DataOutputStream(_socket.getOutputStream());
        _in = new DataInputStream(_socket.getInputStream());

        _outBuffer = ByteBuffer.allocate(2048);
        _outBuffer.order(ByteOrder.LITTLE_ENDIAN);
        _inBuffer = ByteBuffer.allocate(2048);
        _inBuffer.order(ByteOrder.LITTLE_ENDIAN);

        System.out.println("Connection Response: " + _in.read(_inBuffer.array()));
        System.out.println("Response: " + WriteCommand("DRS\r\n"));

        return true;
    }
    catch(Exception e)
    {
        System.out.println("Exception!");
    }
    return false;
}

private String WriteCommand(String command)
{
    try
    {
        _outBuffer = encoder.encode(CharBuffer.wrap(command));
        _out.write(_outBuffer.array());
        _out.flush();

        _in.read(_inBuffer.array());
        String retString = decoder.decode(_inBuffer).toString();

        return retString.substring(0, retString.indexOf('>') + 1);
    }
    catch(Exception e)
    {
        System.out.println("Exception!");
    }

    return "E1>";
}   

There are many things to clean up and I’m going to experiment with whether I need to do it in quite this way, but this is the gist of the “solution”. The big killer was the endian-ness. It should be mentioned, once again, that this is ugly and non-production code, but any other input would still be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:02:04+00:00Added an answer on May 31, 2026 at 8:02 pm

    I have a couple things you can try. You are using a PrintWriter for your output, this is a fairly high-level Writer (i.e. it encapsulates a lot of things from you). My concern is that the println() method in PrintWriter adds a line terminating character(s) at the end automatically (as appropriate for your OS). So what you are really sending is “/r/n(line terminator)” so on a unix box you would be sending “/r/n/n”.

    I would recommend switching to a DataOutputStream which gives you much more control over the raw bytes that are sent: http://docs.oracle.com/javase/6/docs/api/java/io/DataOutputStream.html

    Remember if you switch to DataOutputStream you need to call flush on the output stream.

    My other thought is it might be an endianess problem. Java is strictly Big Endian (network byte order). Is it possible your “card” is reading things in little-endian? If you need to write over the network in little endian (if so your card is a bad netizen!) you will need to use a ByteBuffer, set its order to little-endian. Write your bytes to it, then write the bytes from your ByteBuffer to the DataOutputStream.

    I would probably switch to a DataInputStream for your input stream too. readline() will only return once the newline character is seen. Is your card returning a newline in its response?

    My last thought is that your println methods might have an error and you don’t know it because PrintWriter doesn’t throw exceptions. The PrintWriter JavaDocs says:

    “Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError().”

    Hopefully something in my long rambling response will help you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.