![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2005
Location: PA, USA
Posts: 8
Rep Power: 0
![]() |
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; 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. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
A small snippet of code:
bw = new BufferedWriter(new OutputStreamWriter(smtpSock.getOutputStream())); bw.write(data + CRLF); bw.flush(); |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Dec 2005
Location: PA, USA
Posts: 8
Rep Power: 0
![]() |
Wonderful! This does it! Thanks so much. Now I have to clean up my output more and get some better error-handling.
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Dec 2005
Location: PA, USA
Posts: 8
Rep Power: 0
![]() |
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()); 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. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Dec 2005
Location: PA, USA
Posts: 8
Rep Power: 0
![]() |
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()); At the bare minimum, I hope these posts help someone in the future who runs into this problem. |
|
|
|
|
|
#6 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,031
Rep Power: 5
![]() |
Quote:
__________________
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 |
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Dec 2005
Location: PA, USA
Posts: 8
Rep Power: 0
![]() |
Awesome. Glad I could help.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|