Java 版 (精华区)

发信人: allen (夏夜晚风·原来的我), 信区: Java
标  题: 我的<<每日一题3>>解答
发信站: 哈工大紫丁香 (2002年08月17日19:59:39 星期六), 站内信件

   这道题比上一道简单一些 ^^
   
   运行结果:
       D:\Program\myself>java TCPServer
       
       D:\Program\myself>java TCPClient
       Please Input the String:
       pukeDW2002
       From Server: PUKEDW2002   

   代码如下:

// TCPServer.java
  
import java.io.*;
import java.net.*;

class TCPServer {
    public static void main(String args[]){
        ServerSocket server = null;
        Socket client = null;
        try{
            // 服务端监听5555端口 允许连接数为1
            server = new ServerSocket(5555, 1);
            client = server.accept();
            // 处理客户端请求
            processClient(client);
            client.close();
        }
        catch (IOException exec){
            System.out.println("Error in Server: " + exec);
        }
        
    }
    
    static void processClient(Socket client) throws IOException {
        BufferedReader in = new BufferedReader(new 
                            InputStreamReader(client.getInputStream()));
        PrintWriter out = new PrintWriter(client.getOutputStream(), true);
        String inputStr;
        try {
            // 读入客户端传送的数据
            inputStr=in.readLine();
            // 大写后写回客户端
            out.println(inputStr.toUpperCase());
            System.out.println("Client Input: " + inputStr);
        }finally{
            out.close();
            in.close();
            client.close();
        }
    }       
}

// TCPClient.java
import java.io.*;
import java.net.*;

public class TCPClient {
    public static void main(String args[]){
        try{
            // 尝试连接本机5555端口的服务端
            Socket client = new Socket("localhost", 5555);
            // 处理传送数据
            getString(client);
        }catch (UnknownHostException exec){
            System.out.println("Unknown Host Exception: " + exec);
        }catch (IOException exec){
            System.err.println("Some IO Exception: " + exec);
        }
    }
    
    static void getString(Socket client) throws IOException {
        try {
            BufferedReader in = new BufferedReader(new 
                                InputStreamReader(client.getInputStream()));
            PrintWriter out = new PrintWriter(client.getOutputStream(), 
                                              true);
            BufferedReader br = new BufferedReader(new 
                                InputStreamReader(System.in));
            String inputStr;
            System.out.println("Please Input the String:");
            // 读入控制台输入串
            inputStr=br.readLine();
            // 送至服务端
            out.println(inputStr);
            String responseStr;
            while ((responseStr = in.readLine()) != null){
                // 收到服务端响应字符串 显示在控制台
                System.out.println("From Server: " + responseStr);
            }
            out.close();
            in.close();
            client.close();
        }catch (IOException exec){
            System.out.println("Some IO Exception: " + exec);
        }
    }       
}  

--
 如 果 你 爱 我
    你 会 来 找 我

※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 218.7.25.8]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:4.410毫秒