Socket编程练习(TCP)

Server.java
==================================================
import java.net.*;
import java.io.*;

/**
 * 
 * 服务器端程序:
 *
 * 1. 监听一端口,等待客户接入;
 * 2. 一旦有客户接入,就构造一个Socket会话对象;
 * 3. 将这个会话交给线程处理,然后主程序继续监听。
 * 
 * @author OKJohn
 * @version 1.0
 */

public class Server extends ServerSocket {

    public Server(int serverPort) throws IOException {
         //用指定的端口构造一个ServerSocket
        super(serverPort); 
        try {
            while (true) {
                //监听一端口,等待客户接入
                Socket socket = accept();  
                //将会话交给线程处理
                new ServerThread(socket); 
            }
        } catch (IOException e) {
     e.printStackTrace();
 }
 finally {
            close();  //关闭监听端口
        }
    }

    // inner-class ServerThread
    class ServerThread extends Thread {
        private Socket socket;
        private BufferedReader in;
        private PrintWriter out;

        // Ready to conversation
        public ServerThread(Socket s) throws IOException {
            this.socket = s;
            // 构造该会话中的输入输出流
            in = new BufferedReader(new InputStreamReader(
           socket.getInputStream(), "GB2312"));
            out = new PrintWriter(socket.getOutputStream(), true);
            start();
        }

        // Execute conversation
        public void run() {
            try {

                // Communicate with client until "bye" received.
                while (true) {
                    // 通过输入流接收客户端信息
                    String line = in.readLine(); 
                    if ("bye".equals(line)) {  // 是否终止会话
                        break;
                    }
                    System.out.println("Received message:" + line);
                    String msg = "'" + line + "'has been accepted by server.";
                    // 通过输出流向客户端发送信息
                    out.println(msg);
                    out.flush();
                }

                out.close();
                in.close();
                socket.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    // main method
    public static void main(String[] args) throws IOException {
        new Server(2000);
    }
}

Client.java
================================================
import java.net.*;
import java.io.*;

/**
 * 
 * 客户端程序
 *
 * @author OKJohn
 * @version 1.0
 */

public class Client {

    private Socket socket;
    private BufferedReader reader;
    private PrintWriter writer;

    public Client(int serverPort) {
        try {

            // 向指定服务器(IP、端口)发出请求
            socket = new Socket("127.0.0.1", serverPort);

            // 用得到的会话对象构造输入输出流
            reader = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));
            writer = new PrintWriter(socket.getOutputStream());

            // Communicate with server until "bye" input.
            while (true) {
  
                // 接受统标准输入(键盘)输入的信息
               BufferedReader in = new BufferedReader(
                        new InputStreamReader(System.in));
                String message = in.readLine();
                // 将信息通过输出流发送给服务器
                writer.println(message);
                writer.flush();
                // 是否终止会话
                if ("bye".equals(message)) {
                    break;
                }
                // 通过输入流接收服务器信息
                String received = reader.readLine();
                System.out.println(received);
            }

            writer.close();
            reader.close();
            socket.close();

        } catch (UnknownHostException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Client(2000);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值