Getting double responses from my ChatServer
I'm writing a chat server from scratch, and have a BASIC telnet connectable program that stdout's the client input, and echo's back to my telnet (since my telnet is DOS, and has no local echo). The problem is that every time i try to output something to the client, I get double letters (except the first thing to be sent.)
I'm handling it charactor by charactor, so when i type "test" into it, it echos back
" teesstt"
to me. This is a bit of a problem,a s I'd MUCH rather get around this as opposed to write some stupid algorythm to extract the proper message out of those.
Here's the problematic code.
privatevoid HandleInputOutput ()
{
char temp;
try
{// Set up the input/output system
istrm = sckt.getInputStream ();
ostrm = sckt.getOutputStream ();
bfdr =new BufferedReader (new InputStreamReader (istrm));
prtw =new PrintWriter (new OutputStreamWriter (ostrm));
// while temp is read a value, and that value != -1
while ((temp = (char) bfdr.read ()) != (char) - 1)
{//print char for server, write out char to user (problematic, see other comment)...
System.out.print (temp);
prtw.print(temp);
prtw.flush();// getting double chars.
}
System.out.println("\nConnection broken");
}
catch (Exception e)
{
System.out.println ("Exception of type " + e.getClass () +"With message \"" + e.getMessage () +"\" occoured in HandleInputOutput method.");
}
}
[2358 byte] By [
Bobbiasa] at [2007-10-2 21:51:50]

This code looks and works fine for me. Are you sure you have NO echo in you telnet?
Nope, i'm not getting any echo on my telnet, i don't even see the written stuff unless i send the flush command each time. I've reproduced it with full lines too, it does the same thing.
Nothing wrong with your code that I can see. It must be Windows telnet.
ejpa at 2007-7-14 1:07:43 >

What kind of telnet do you use? Windows?Do the followed:telnetunset local_echoopen 127.0.0.1 <yourport>and check if double characters come or no.
I tried that just now, and still get the double charactors. This is beginning to get to be a pain in the *** :/ I hate bugs like this because it's nothing wrong with the code, but how the system reacts to the code :/
In any case, I did what you said (there was no underscore in local echo, but other than that, it was the same), and still got the double letters.
There IS underscore in parameter!
no, there is not an underscore.
http://img.photobucket.com/albums/v227/Bobbias/nope.jpg
In any case, I've done **** near everything I can aside from killing someone, and it hasn't helped, so if anyone has ANY idea why the hell this could happen, I'd appreciate a response that could help figure out why this is happening.
It is clearly a bug in Windows Telnet. Setting localecho off has no effect - it is no different from setting it on. If your server echoes a line at a time instead of a character at a time the behaviour changes to dual lines instead of dual characters.
You can test this hypothesis by implementing a client in Java corresponding to your server and seeing if the problem occurs.
ejpa at 2007-7-14 1:07:43 >

Yeah, that's true, I've tried using it line mode as well. The odd part is that with no flush statement, none of the charactors or lines get printed.
modify your server code to return some fixed char such as "*" instead of echo ans see what happen.If you still ge the echo form your telnet its not your problem
LRMKa at 2007-7-14 1:07:43 >

Excellent suggestion. This proves that it is Telnet.
ejpa at 2007-7-14 1:07:43 >

Well, Bobbias, if you use some kind of really old (or too modern) telnet (what is your OS tell at last?) you should said two days ago and save some time for all of us :)
Telnet from Windows 2000 AS lools like followed and works fine with your code:
Microsoft (R) Windows 2000 (TM) Version 5.00 (Build 2195)
Welcome to Microsoft Telnet Client
Telnet Client Build 5.00.99206.1
Escape Character is 'CTRL+]'
Microsoft Telnet> unset ?
NTLMTurn OFF NTLM Authentication.
LOCAL_ECHOTurn OFF LOCAL_ECHO.
CRLFSend only CR (no LF is sent)
Microsoft Telnet>
I'm running microsoft XP home edition using the default telnet. Changing the output to * proves telnet IS sticking the letters in (I get " t*e*s*t*" when I type test)
Now I need to find a way to make it stop :/ I'd much rather not write a client untill I need one, because I'll end up making a hackjob of it if I don't wait.
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
That's the info I get when running cmd, and then telnet gives no build info.
Well, there are problems. Intersting but I just have no time to detailed investigation - change your code as followed and Windows XP telnet will work fine as well as Windows 2000 telnet. Try to investigate and tell us ;)
import java.net.*;
import java.io.*;
class a
{
private static void HandleInputOutput( Socket sckt )
{
int temp;
try
{// Set up the input/output system
BufferedReader bfdr = new BufferedReader ( new InputStreamReader( sckt.getInputStream( ) ) );
BufferedWriter bfdw = new BufferedWriter( new OutputStreamWriter( sckt.getOutputStream( ) ) );
// while temp is read a value, and that value != -1
char[] buf = new char[ 100 ];
while( ( temp = bfdr.read( buf, 0, 100 ) ) != -1 )
{//print char for server, write out char to user (problematic, see other comment)...
//System.out.print( temp );
bfdw.write( buf, 0, temp );//prtw.print( temp );
//prtw.flush( );// getting double chars.
}
System.out.println("\nConnection broken");
}
catch( Exception e )
{
System.out.println ("Exception of type " + e.getClass () + "With message \"" + e.getMessage () + "\" occoured in HandleInputOutput method.");
}
}
public static void main( String[] args ) throws java.io.IOException
{
ServerSocket s = new ServerSocket( 2000 );
Socket ss = s.accept( );
HandleInputOutput( ss );
return;
}
}
For some reason, that's not working right. I'm not getting any input properly, considering that with this code I can't get it to even say that it recieved a charactor (by replacing the STDOUT statement with one that jusat said "charactor recieved". somewhere there the execution broke, so I'm guessing that the read statement is messed.
I'm no expert with I/O functions and I'm not used to the form of the functions you're using, so I couldn't tell you why it's not working, but I'm still getting double letters by just running telnet, and using the flush command (I guess I'll just have to rely on localecho there).