Welcome to Dream.In.Code
Become a Java Expert!

Join 149,518 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,373 people online right now. Registration is fast and FREE... Join Now!




Telnet Troubles

 
Reply to this topicStart new topic

Telnet Troubles, Problem with telnet test.

codemonkey83
4 Jul, 2007 - 01:48 PM
Post #1

New D.I.C Head
*

Joined: 10 Feb, 2007
Posts: 43


My Contributions
Hi all, it's been a while.

I'v been working on writing my own mud client in Java, primarily to be used for the Mud Achaea.

I "borrowed" the telnet client code from the Java Cookbook in order to get an idea ow writing the basic client might work, but I seem to be having issues getting the prompt to display until after I type a command and was wondering if someone might be able to help me see what needs to be done. Here's the basic code(uncommented) from the Java Cookbook.

CODE

import java.net.*;
import java.io.*;

public class Telnet {
    String host;
    int portNum;
    public static void main(String[] argv) {
        new Telnet().talkTo(argv);
    }
    private void talkTo(String av[]) {
        if (av.length >= 1)
            host = av[0];
        else
            host = "localhost";
        if (av.length >=2)
            portNum = Integer.parseInt(av[1]);
        else
            portNum = 23;
        System.out.println("Host " + host + "; port " + portNum);
        try {
            Socket s = new Socket(host, portNum);
            
            new Pipe(s.getInputStream(), System.out).start();
            new Pipe(System.in, s.getOutputStream()).start();
        } catch (IOException e) {
            System.out.println(e);
            return;
        }
        System.out.println("Connected OK");
    }
}

class Pipe extends Thread {
    BufferedReader is;
    PrintStream os;
    
    Pipe(InputStream is, OutputStream os) {
        this.is = new BufferedReader(new InputStreamReader(is));
        this.os = new PrintStream(os);
    }
    
    public void run() {
        String line;
        try {
            while ((line = is.readLine()) != null) {
                os.print(line);
                os.print("\r\n");
                os.flush();
            }
            
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}


Any help would be appreciated.
User is offlineProfile CardPM
+Quote Post

ap0c0lyps3
RE: Telnet Troubles
7 Jul, 2007 - 12:32 PM
Post #2

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
I had the same problem when testing my program. As soon as the client opens the port send a blank character to the client like a space or something then it will show all stuff you send
User is offlineProfile CardPM
+Quote Post

codemonkey83
RE: Telnet Troubles
24 Dec, 2007 - 05:25 PM
Post #3

New D.I.C Head
*

Joined: 10 Feb, 2007
Posts: 43


My Contributions
QUOTE(codemonkey83 @ 4 Jul, 2007 - 02:48 PM) *

Hi all, it's been a while.

I'v been working on writing my own mud client in Java, primarily to be used for the Mud Achaea.

I "borrowed" the telnet client code from the Java Cookbook in order to get an idea ow writing the basic client might work, but I seem to be having issues getting the prompt to display until after I type a command and was wondering if someone might be able to help me see what needs to be done. Here's the basic code(uncommented) from the Java Cookbook.

CODE

import java.net.*;
import java.io.*;

public class Telnet {
    String host;
    int portNum;
    public static void main(String[] argv) {
        new Telnet().talkTo(argv);
    }
    private void talkTo(String av[]) {
        if (av.length >= 1)
            host = av[0];
        else
            host = "localhost";
        if (av.length >=2)
            portNum = Integer.parseInt(av[1]);
        else
            portNum = 23;
        System.out.println("Host " + host + "; port " + portNum);
        try {
            Socket s = new Socket(host, portNum);
            
            new Pipe(s.getInputStream(), System.out).start();
            new Pipe(System.in, s.getOutputStream()).start();
        } catch (IOException e) {
            System.out.println(e);
            return;
        }
        System.out.println("Connected OK");
    }
}

class Pipe extends Thread {
    BufferedReader is;
    PrintStream os;
    
    Pipe(InputStream is, OutputStream os) {
        this.is = new BufferedReader(new InputStreamReader(is));
        this.os = new PrintStream(os);
    }
    
    public void run() {
        String line;
        try {
            while ((line = is.readLine()) != null) {
                os.print(line);
                os.print("\r\n");
                os.flush();
            }
            
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}


Any help would be appreciated.


I have recently revisted this project and began working on a system from scratch. I am still finding it difficult to get the server's prompt to display until after input something and hit enter, which results in something like.
CODE

2
Enter an option or enter your character's name. ��


The prompt should be:
CODE

Enter an option or enter your character's name.

If the suggested solution will in fact work for this, how would I do this?


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:11PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month