日期:2014-05-20 浏览次数:20834 次
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
/**
* Socket服务器端,a simple example
*/
public class ServerOne {
public static void main(String[] args) {
ServerSocket serverSocket = null;
while (true) {
try {
// 服务器端SeverSocket
serverSocket = new ServerSocket(8080, 0, InetAddress.getLocalHost());
// 阻塞直到有请求接入
Socket accept = serverSocket.accept();
// 得到输入流
InputStream inputStream = accept.getInputStream();
// 输出请求的字符
BufferedInputStream inBuffer = new BufferedInputStream(inputStream);
for (int c; (c = inBuffer.read()) != -1;) {
System.out.print((char) c);
}
// 得到输出流
OutputStream outputStream = accept.getOutputStream();
OutputStreamWriter streamWriter = new OutputStreamWriter(outputStream);
BufferedWriter writer = new BufferedWriter(streamWriter);
DateFormat df = new SimpleDateFormat("yyyy-mm-dd hh:MM:ss");
String str = "当前时间是:" + df.format(System.currentTimeMillis());
// 输出日期,时间,并刷新输出缓冲区
writer.write(str);