开发者

Java - Socket not reading until client disconnects?

So I made a Server and Client with sockets in Java. I'm trying to get the server to read/write from the socket, but it only reads once the client disconnects:

        System.out.println("Server initialized");
        clientSocket = server.accept();
        System.out.println("Client connected");

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        for(int loop = 0;loop<5;loop++){
            out.write("Hello");
            System.out.println(in.readLine());
        }   //end for

The problem is that once the client connects, it says "Client connected" bu开发者_如何学运维t then it doesn't run through the for loop. Once the client disconnects, however, the server executes the for loop and returns

null
null
null
null
null

What am I missing here? Here is my full server code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public ServerSocket server;

    public Server(int port){
        try {
            server = new ServerSocket(port);
        } catch (IOException e) {
            System.out.println("Cannot bind to port: "+ port);
            System.exit(-1);
        }

    }   //end constructor

    public void WaitForClient() throws IOException{
        Socket clientSocket = null;

        try {
            System.out.println("Server initialized");
            clientSocket = server.accept();
            System.out.println("Client connected");

            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

            for(int loop = 0;loop<5;loop++){
                out.write("Hello");
                System.out.println(in.readLine());
            }   //end for

        }   //end try
        catch (IOException e) {
            System.out.println("Accept failed");
            System.exit(-1);
        }   //end catch

        clientSocket.close();
        server.close();

    }   //end method

}   //end class

(My main method is called from another class.)


You have to flush the stream on the server if you want to see the results immediately. The stream is auto-flushed on close(), that's why you seen the output then.


At first glance, it looks like your call to in.readLine() is blocking because it hasn't encountered a newline yet.

Thus, it blocks until your connection drops.

Try BufferedReader's read method:

public int read(char[] cbuf,
       int off,
       int len)

and pass in a fixed length array such as:

char[] cbuf = new char[1024];

Use the return value of the read call to see how many bytes were in the last read (0-1024) if you read 1024, there is likely more to read. If you read 0, then there is likely no more to read.


You are reading lines but you aren't writing them. write() doesn't write a newline, and readLine() blocks until it receives one.


It is server side Code for accepting String From Client but according to my code u will have use DataInputStream and DataOutputStream instead of PrintWriter And BufferedReader.

This code is Completely Working for One to One Client..

In code "jta" is JTextArea ..

soc = server.accept();

while(true)

{

        String data="";
            try
            {                   
                dis = new DataInputStream(soc.getInputStream());
                dos = new DataOutputStream(soc.getOutputStream());
                data = dis.readUTF();
            }
            catch(Exception e)
            { }
            jta.append(data + "\n");
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜