Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sockets close() and flush()

843790Mar 5 2008 — edited Mar 6 2008
Hello,

I've been searching the internet and I can see that there are people that have the same problem that I do
but still I cant find any answer, so my problem is this:

I have a client that sends some data over to a server, using sockets, that is listening for connections. Everything works well
(data is transfered to server) only if I use the close() method on my output stream.

The thing is that I don't to do that I want to reuse the connection (dont know if I'm using the correct term).
Thus I tried only flushing and not closing afterwrds, still nothing.

Here is the client code:
public class Client 
{    
	static String test="test string";

    
    public static void main(String args[]) throws Exception 
    {
        Socket clientSocket = new Socket("127.0.0.1", 4567);
        
        BufferedOutputStream outToServer =
                new BufferedOutputStream(clientSocket.getOutputStream());
        outToServer.write(test.getBytes(),0,test.length());
        outToServer.flush();
        //outToServer.close();

              while(true)
{}

    }// main
    
}// class
Server programm:
class Server
{

    public static void main(String args[]) throws Exception
    {

       ServerSocket welcomeSocket = new ServerSocket(4567);

        while (true)
        {
            System.out.println("waiting client ...");

            Socket serverSocket = welcomeSocket.accept();
            System.out.println("connected with client");

          
            BufferedInputStream inFromClient =
                    new BufferedInputStream(serverSocket.getInputStream());

            StringBuffer b = new StringBuffer();
            int ch;
            while ((ch = inFromClient.read()) != -1)
            {
                b.append((char) ch);
            }            
           
            System.out.println(b.toString());
       
        }// while

    }// main
}// class
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 3 2008
Added on Mar 5 2008
3 comments
730 views