Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading the data from the Socket  RSS feed

 
Sanju Shah
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I'm new to socket/java programming. Following is what I'm doing..

I'm doing a socket connection to a server. After making the connection,the server will prompt me for the password.. And then after entering the password. It will give me few options/selections that I need to make to get the config information about the server. following is the code


char[] data = new char[1000];
int dataRead = 0;
String out = null;
Socket soc = new Socket(IPAddress,Port);

BufferedWriter buffwrite = new BufferedWriter( new OutputStreamWriter (soc.getOutputstreamWriter())));

BufferedReader buffRead = new BufferedReader( new InputStreamReader( soc.getInputStream() ) );

while ((dataRead = buffRead.read(data,0,data.length)) != -1)
{ out = new String(data, 0, dataRead);
System.out.println("data read is " + out);
if (out.equalsIgnoreCase("enter password:"))
{ buffWrite.write("psswd");
buffWrite.flush();
dataRead = 0;
System.out.println("1" );

}
else if (out.equalsIgnoreCase(" select your option:"))
{
buffWrite.write("1");
buffWrite.flush();
dataRead = 0;
System.out.println("2");

}
else if (out.equalsIgnoreCase("server config:"))
{ System.out.println("3" );

break;
}
}

When I run my application, all I get is the first System.out i.e "data read is enter password " . Can U please tell me why its not entering the loops and writing to the server? what am I doing wrong? I appologize if its something silly as I said I'm new to java.

Thanks
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Sanju.
Your first system out says the value of out is "enter password" but your first if statement is looking for "enter password:". I'd say that's a problem.
It's a good practice when you have a bunch of "if"'s checking for various conditions to have one last "else", even if it only prints out "this should never happen", for situations like this, when it does happen.
 
Sanju Shah
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply. That's a typo when entering it into the forum.. I went and checked back the code , it looks fine. .... Please help....
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you never see the printout from this line:

in the enter password block?
How about putting a line before writing the password?
And I'm serious about putting that final "else" in. If not now, it will save you down the road.
 
Sanju Shah
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have added the else portion to my code... I does write the password to the scoket and I do see the System.out.println("1") but after that it just stops.. I don't see anything being read from the socket or written to.

Ideally, after wrtiting the psswd to the scoket, the server should present few options from which I have to select one of the options to read the server config information..

Am I using the right output and input streams.?? Is something getting blocked..?
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that the "1" is getting printed out is a very important piece of the puzzle.
You don't tell us what your server appliation is, but I suspect that it is blocked waiting for the line terminator following the password. Do you have access to the server code or protocol?
 
Sanju Shah
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately I don't have access to the server code/protocol.
 
Sanju Shah
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also added note is that it look like its waiting to read from the socket during the second around hence it goes into an infinte loop and there fore socket and the output streams doesn't get closed . So how do I make it not wait/block? Also why is it waiting ?. When I telent to server I can see that it prompts me for a password and after entering it , it presents me with options that I can select and it works prefectly fine.

I'm cluelss. what is it that I'm doing wrong.?
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you normally access this server? Sounds like you don't have all the information you need.
You can test my theory by simply adding a BufferedWriter.newLine() call right after writing your password to the stream. Of course, this assumes that your server is looking for the platform-specific line separator. In general, this is a bad idea
 
Sanju Shah
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added bw.newline() right after bw.flush(). I see that being executed but after that nothing happens. Also I added a counter to break out of the while loop after 2nd iteration. I can see that the counter never got to the 2nd iteration which proves the point that it it is waiting to read from the socket.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the newline before the flush, otherwise it may get stuck in a buffer somewhere.
If that doesn't work and you don't have access to the server code or a protocol specification I'd say you don't have enough information.
Can you access the server with telnet?
 
Sanju Shah
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hurray it worked. I think putting the newline() in front of flush did the trick.
I 'm not very clear as to what does this newline() method does and why it worked if put before flush()... Can U please explain.??

THANKS for your time...
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public void newLine() throws IOException
Write a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.

Java API Documentation - java.io.BufferedWriter
As I said before, using this method with sockets is, in general, a Bad Idea because line separators are different for different platforms.
Putting newLine() after the flush() didn't work because BufferedWriter has an 8k buffer. It doesn't empty the buffer until it is full or one calls flush(), so your newline was just sitting in the buffer and your server was waiting on the newline.
 
Sanju Shah
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank U very much. Appericate all your help
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!