robber:这个程序是我自己写的,在testServer.java文件中已经有下面这句语句了 out.println("Java test ServerSocket: " + new Date()); 请问testClient中应该怎样处理,用in.readLine()接受并打印出来不行吗? 能把这段程序改正并使它成功接受testServer吗? 多谢
一个socket分别有: BufferedReader in PrintWriter out in.readLine()的时候,如果这时候BufferedReader 中没有可以读取的信息,那你的程序会在这里停下来。当然,你下一步想要out.print(....),根本执行不到了。因此实际你的程序只交互了一次就停了下来了。解决的方法在很多书中都有介绍,去看看吧!
可是testServer.java中有out.println("Java test ServerSocket: " + new Date()); 这句语句中的("Java test ServerSocket: " + new Date());应该会传到testClient 中的BufferedReader 中去啊。你能帮我改一下程序吗?
Here is the souce code I write for a time server using socket. Hope it will help you.// The server: import java.net.*; import java.io.*; import java.util.Date;/** * A class send out date infomation through socket. * * @author: java8964 * @version: 1 */ public class DaytimeServer { public static final int PORT = 1300; public static void main(String[] args) { try { ServerSocket server = new ServerSocket(PORT); Socket connection = null; while (true) { // using a while (true) loop try { connection = server.accept(); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); Date now = new Date(); out.write(now.toString() + "\r\n"); out.flush(); connection.close(); } catch (IOException ioe) { System.err.println(ioe); } finally { try { if (connection != null) connection.close(); } catch (IOException e) {} } } } catch (IOException ee) { System.err.println(ee); } } // end main } // end DaytimeServer// The client import java.net.*; import java.io.*;/** * A class to receive time infomation from server through socket. * * @author: java8964 * @version: 1 */ public class DaytimeClient { /** * The port which client and server make connection. */ public static final int PORT = 1300; public static void main(String[] args) { String hostName; if (args.length > 0) { hostName = args[0]; } else { hostName = "localhost"; } try { Socket clientSocket = new Socket(hostName, PORT); InputStream timeStream = clientSocket.getInputStream(); StringBuffer time = new StringBuffer(); int c; while ((c = timeStream.read()) != -1) { time.append((char)c); } String timeString = time.toString().trim(); System.out.println("It is " + timeString + " at " + hostName); } catch (UnknownHostException ukhe) { System.err.println(ukhe); } catch (IOException ioe) { System.err.println(ioe); } } // end main } // end DaytimeClient
out.println("Java test ServerSocket: " + new Date());
请问testClient中应该怎样处理,用in.readLine()接受并打印出来不行吗?
能把这段程序改正并使它成功接受testServer吗?
多谢
BufferedReader in
PrintWriter out
in.readLine()的时候,如果这时候BufferedReader 中没有可以读取的信息,那你的程序会在这里停下来。当然,你下一步想要out.print(....),根本执行不到了。因此实际你的程序只交互了一次就停了下来了。解决的方法在很多书中都有介绍,去看看吧!
这句语句中的("Java test ServerSocket: " + new Date());应该会传到testClient
中的BufferedReader 中去啊。你能帮我改一下程序吗?
import java.net.*;
import java.io.*;
import java.util.Date;/**
* A class send out date infomation through socket.
*
* @author: java8964
* @version: 1
*/
public class DaytimeServer {
public static final int PORT = 1300; public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(PORT);
Socket connection = null; while (true) { // using a while (true) loop
try {
connection = server.accept();
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
Date now = new Date();
out.write(now.toString() + "\r\n");
out.flush();
connection.close();
} catch (IOException ioe) {
System.err.println(ioe);
} finally {
try {
if (connection != null)
connection.close();
} catch (IOException e) {}
}
}
} catch (IOException ee) {
System.err.println(ee);
}
} // end main
} // end DaytimeServer// The client
import java.net.*;
import java.io.*;/**
* A class to receive time infomation from server through socket.
*
* @author: java8964
* @version: 1
*/
public class DaytimeClient {
/**
* The port which client and server make connection.
*/
public static final int PORT = 1300; public static void main(String[] args) {
String hostName;
if (args.length > 0) {
hostName = args[0];
} else {
hostName = "localhost";
} try {
Socket clientSocket = new Socket(hostName, PORT);
InputStream timeStream = clientSocket.getInputStream();
StringBuffer time = new StringBuffer();
int c;
while ((c = timeStream.read()) != -1) {
time.append((char)c);
}
String timeString = time.toString().trim();
System.out.println("It is " + timeString + " at " + hostName);
} catch (UnknownHostException ukhe) {
System.err.println(ukhe);
} catch (IOException ioe) {
System.err.println(ioe);
}
} // end main
} // end DaytimeClient