/*
* DateServer.java
* Class for a Date server
*
*/
/*
* @author Michael Hvidsten
* @date December 7, 2009
*
*/
import java.net.*;
import java.util.*;
import java.io.*;
public class DateServer {
public static void main(String[] args) {
Date now = new Date( );
try{
System.out.println("Waiting for connection on " +
" port 7654.");
ServerSocket serverSock = new ServerSocket(7654);
System.out.println(" Server is up ");
// Listen for connection from a client
// Execution will halt until a client is detected
Socket clientSock = serverSock.accept( );
System.out.println("client has connected");
// Create input/output streams to client socket
BufferedReader clientInput = new BufferedReader(
new InputStreamReader(
clientSock.getInputStream( )));
DataOutputStream clientOutput =
new DataOutputStream(
clientSock.getOutputStream( ));
// Ask client to send their name
clientOutput.writeUTF("Please send your name\n");
clientOutput.flush();
String clientText = clientInput.readLine();
// Reply to client with current Date
String replyText = "Welcome, " + clientText +
", Today is " + now.toString( ) + "\n";
clientOutput.writeUTF(replyText);
clientOutput.flush();
System.out.println(" Sent: " +replyText);
// Close the streams and all sockets
clientOutput.close( );
clientInput.close( );
clientSock.close( );
serverSock.close( );
}catch (IOException e){
System.out.println(e.getMessage( ));
}
}
} // end of class definition
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
WorkPlace.rar (2个子文件)
DateServer.java 2KB
DateClient.java 1KB
共 2 条
- 1
资源评论
xiaoyaojj1
- 粉丝: 1
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功