這也正是服務器使用BufferedWriter
對象將發送到客戶端接收在對象BufferedReader
一個簡單的客戶端 - 服務器。爲什麼我的客戶端沒有正確讀取服務器發送的outputStream?
當我使用OutputStream
和PrintWriter
爲服務器和InputStream
和Scanner
爲客戶它工作得很好。
會發生什麼事是,在緩衝方式的客戶端讀取-1,如果我發送INT和空爲字符串。
我希望我的英語有道理。 ,P
這是代碼:
服務器:
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8189);
Socket incoming;
incoming = server.accept();
try {
// OutputStream output = incoming.getOutputStream();
// PrintWriter outStream = new PrintWriter(output, true /*autoFlush*/);
// outStream.println("ENTER");
BufferedWriter output = new BufferedWriter(new
OutputStreamWriter(incoming.getOutputStream()));
output.write(3);
System.out.println("\nSent");
} finally {
incoming.close();
}
}
}
客戶:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
//Client theClient= new Client();
Socket RTSPSocket;
int ServerPort = 8189;
//server name address
String ServerHost = "localhost";
//get Server IP Address
InetAddress ServerIPAddress = InetAddress.getByName(ServerHost);
RTSPSocket = new Socket(ServerIPAddress, ServerPort);
try {
/*
InputStream input = theClient.RTSPSocket.getInputStream();
Scanner in = new Scanner(input);
String line = in.nextLine();
System.out.println(line);
*/
BufferedReader input = new BufferedReader(new
InputStreamReader(RTSPSocket.getInputStream()));
//String line = input.readLine();
//System.out.println("\nRicevuto: " + line);
System.out.println(input.read());
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
}
謝謝你們。現在它可以工作。我不知道! – soneangel 2010-09-30 09:33:31