The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
September 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

socket programming having flushing problem

Posted by chetana on September 14, 2000 at 3:20 AM

i am having a strange problem.i have written a code in socket programming wherein client sends a string to server and server sends back the string converting it to uppercase.the problem is when i run the code and start giving input the program works fine for first 7-8 lines and after that for the next 8-10 line of input no o/p is displayed immediately.the o/p appears together after 10 lines and again everything is all right for the next 8 lines.this cycle continues till i end the program.what is wrong with the code?
i am enclosing the code as well


//for client
import java.io.*;
import java.net.*;
class TCPClient {

public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;

BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));

Socket clientSocket = new Socket("hostname", 6789);

DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());

BufferedReader inFromServer =
new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while(true)
{
sentence = inFromUser.readLine();

outToServer.writeBytes(sentence + '\n');

modifiedSentence = inFromServer.readLine();

System.out.println(modifiedSentence);
if(sentence.equals(".")break;
}

clientSocket.close();

}
}


//for server

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

class TCPServer {

public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;

ServerSocket welcomeSocket = new ServerSocket(6789);

while(true) {

Socket connectionSocket = welcomeSocket.accept();

BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
while(true)
{
clientSentence = inFromClient.readLine();

capitalizedSentence = clientSentence.toUpperCase() + '\n';

outToClient.writeBytes(capitalizedSentence);
if(clientsentence.equals(".")break;
}
}
}
}




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us