Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 15th, 2005, 3:27 PM   #1
Mendicant
Newbie
 
Mendicant's Avatar
 
Join Date: Dec 2005
Location: PA, USA
Posts: 8
Rep Power: 0 Mendicant is on a distinguished road
Question best way to send data to a listening mud server

Hello,

I'm currently developing a Java swing mud client, and have run into some trouble sending valid data to a listening mud.

My main class contains the following declarations
private Socket socket;
private BufferedReader in;
private BufferedOutputStream os;
where socket is the connection that I open, in is the text stream received from the mud server I connect to, and os is the output stream from which I send information to the mud.

I receive messages from the mud successfully and with no problem. Sending data back is the problem. I've fiddled with other output stream such as DataOutputStream, but it seems a lot of these format the data before sending it, which results in problems.

Here is the relevent code.
    private void connectToMud(String host, int port)
    {
        String x;
        try
        {
            socket = new Socket(host, port);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            os = new BufferedOutputStream(socket.getOutputStream());
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        connected = true;
        Thread thread = new Thread(this);
        thread.start(); // We need the Socket-listening to be on a separate thread or
                        // Swing gets horribly confused and will freeze. :(
    }

    public void run() // method of Runnable interface!
    {
        try
        {
            int x;
            while(connected == true)
            {
                x = in.read();
                textArea.append(new Character((char)x).toString());
                repaint();
            }
            in.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }

    private void sendInputToMud()
    {
        try
        {
            String str = textInput.getText();
            //char strchar[] = new char[str.length()];
            //strchar = str.toCharArray();
            //os.writeUTF(str);
            //os.flush();
            os.write(new Byte("r")); // The mud doesn't even receive this R!
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }

I've searched Google tirelessly and turned up nothing that helps me. Does anyone know the best way to go about this?

Thanks in advance.
Mendicant is offline   Reply With Quote
Old Dec 15th, 2005, 4:10 PM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 311
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
A small snippet of code:

bw = new BufferedWriter(new OutputStreamWriter(smtpSock.getOutputStream()));

bw.write(data + CRLF);
bw.flush();
andro is offline   Reply With Quote
Old Dec 15th, 2005, 4:28 PM   #3
Mendicant
Newbie
 
Mendicant's Avatar
 
Join Date: Dec 2005
Location: PA, USA
Posts: 8
Rep Power: 0 Mendicant is on a distinguished road
Wonderful! This does it! Thanks so much. Now I have to clean up my output more and get some better error-handling.
Mendicant is offline   Reply With Quote
Old Dec 15th, 2005, 7:49 PM   #4
Mendicant
Newbie
 
Mendicant's Avatar
 
Join Date: Dec 2005
Location: PA, USA
Posts: 8
Rep Power: 0 Mendicant is on a distinguished road
Scrollpane Magic

Ok, I've run into my next snag. I use a JTextArea to display information sent to my client from the MUD. This JTextArea is inside a JScrollPane named scrollPane.

Now, whenever I append text to this textArea, I don't want the vertical scrollbar to stay situated in the northern part of the JTextArea, I want it to become situated in the southern part. I've accomplished this through the following code whenever the textArea receives more text.
JScrollBar bar = scrollPane.getVerticalScrollBar();
bar.setValue(bar.getMaximum());
While this code works, it's rather jumpy and the bar will not always situate itself perfectly after receiving text quickly in succession. I've tried figuring out through the Java documentation if there is a way to change the JScrollPane's behavior so that it is essentially reversed but there is not. setPolicy merely lets you determine when/if scrollBars are displayed.

Anybody have any ideas how to go about this in a more efficient manner? I've also tried setting the caret position in the JTextArea to the last character, but that is even worse in regard to "jumpiness" and lag.

Thanks in advance.
Mendicant is offline   Reply With Quote
Old Dec 15th, 2005, 9:45 PM   #5
Mendicant
Newbie
 
Mendicant's Avatar
 
Join Date: Dec 2005
Location: PA, USA
Posts: 8
Rep Power: 0 Mendicant is on a distinguished road
I hate replying to my own posts, but I figured out the problem. The best way to do something like this is to use the following code.
textArea.setCaretPosition(textArea.getDocument().getLength());
Rather than requesting and unrequesting the focus on the textArea, just set the caret's position. No noticeable lag and the scroll bars adjust properly.

At the bare minimum, I hope these posts help someone in the future who runs into this problem.
Mendicant is offline   Reply With Quote
Old Dec 19th, 2005, 5:33 AM   #6
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by Mendicant
I hate replying to my own posts, but I figured out the problem. The best way to do something like this is to use the following code.
textArea.setCaretPosition(textArea.getDocument().getLength());
Rather than requesting and unrequesting the focus on the textArea, just set the caret's position. No noticeable lag and the scroll bars adjust properly.

At the bare minimum, I hope these posts help someone in the future who runs into this problem.
This should help me, thanks. I was planning on using a JTextArea inside a JScrollPane, just as you did, for one of my projects. Having not yet added enough text to cause scrolling, I didn't realize this would be an issue, but your information has pre-emptively allowed me to solve the problem.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Dec 19th, 2005, 8:44 PM   #7
Mendicant
Newbie
 
Mendicant's Avatar
 
Join Date: Dec 2005
Location: PA, USA
Posts: 8
Rep Power: 0 Mendicant is on a distinguished road
Awesome. Glad I could help.
Mendicant is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC