TCP协议的通信实例

TCP协议的通信实例
马克-to-win:我们首先给出一个最最简单的helloworld通信程序。让大家体会一把两台机器的通信,大家之后就可以慢慢把它发展成为聊天程序。马克-to-win:TCP通信有两个类:1)ServerSocket:服务器用它监听进入的连接;2)Socket:双方都用它初始一次连接。一旦客户端申请建立一个连接,ServerSocket就会返回(通过accept()方法)一个对应的服务器端的Socket,以便进行直接通信。从此时起,我们就得到了一对真正的“Socket-Socket”连接,此时可以利用getInputStream()以及getOutputStream()从每个Socket产生对应的 InputStream和OutputStream对象。之后,可按上章介绍的方法对类进行处理,就象原来对待其他任何流对象那样。创建一个 ServerSocket时,只需为其赋予一个端口编号。但在创建一个客户端 Socket时,必须同时赋予IP以及要连接的端口。下面这对程序先运行服务器程序,再运行客户端程序。马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。




例:2.2.1(客户端写,服务器端读)

import java.io.*;
import java.net.*;
public class TestMark_to_win {
    public static final int PORT = 4002;
    public static void main(String[] args) throws IOException {
        ServerSocket s = new ServerSocket(PORT);
        // Blocks until a connection occurs:
        System.out.println("我作为服务器,正等着你");
        Socket socket = s.accept();
        System.out.println("这句开始打印不出来");
        InputStream in = socket.getInputStream();
        int i = in.read();
        System.out.println("Echoing: " + i);
        socket.close();
        s.close();
    }
}

以下是客户端程序:

import java.net.*;
import java.io.*;
public class Test {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 4002);
        OutputStream out = socket.getOutputStream();
        out.write(97);
        socket.close();
    }
}

输出结果:

我作为服务器,正等着你
这句开始打印不出来
Echoing: 97




例:2.2.2(客户端读写,服务器端也读写)(蓝笔为比上一个程序多的部分)

import java.io.*;
import java.net.*;
public class TestMark_to_win {
    public static final int PORT = 4002;
    public static void main(String[] args) throws IOException {
        ServerSocket s = new ServerSocket(PORT);
        // Blocks until a connection occurs:
        System.out.println("我作为服务器,正等着你");
        Socket socket = s.accept();
        System.out.println("这句开始打印不出来");
        InputStream in = socket.getInputStream();
// Blocks until a connection occurs:
        int i = in.read();
        System.out.println("服务器端反馈" + i);

        OutputStream out = socket.getOutputStream();
        out.write(98);
      
        socket.close();
        s.close();
    }
}

以下是客户端程序:
import java.net.*;
import java.io.*;
public class Test {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 4002);
        OutputStream out = socket.getOutputStream();
        out.write(97);
      
        InputStream in = socket.getInputStream();
// Blocks until a connection occurs:
        int i = in.read();
        System.out.println("客户端反馈" + i);
      
        socket.close();
    }
}

服务器输出结果:

我作为服务器,正等着你
这句开始打印不出来
服务器端反馈97



客户端输出结果:
客户端反馈98


马克-to-win:上一个程序是一个helloworld程序,所以我们写的故意很简单。下一个程序,我们写一个稍微有点儿实用性的,可以在客户端和服务器端传输中文的。有一个重要的问题就是:我们为什么用BufferedReader而BufferedInputstream。因为 BufferedReader有一个方法是readLine,而在BufferedInputstream当中,没有这个方法。PrintWriter又是BufferedReader的一对天生好搭档,里面有个方法:println。这样客户端和服务器端就可以每次都传输一行数据,比上个例子每次传输一个字节,方便的多了。




例:2.2.3(客户端读写,服务器端也读写)

import java.io.*;
import java.net.*;
public class TestMark_to_win {
    public static final int PORT = 4002;
    public static void main(String[] args) throws IOException {
        ServerSocket s = new ServerSocket(PORT);
        System.out.println("服务器正等着");
        // Blocks until a connection occurs:
        Socket socket = s.accept();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));
        /* autoFlush - A boolean; if true, the println() methods will flush the output buffer*/
        PrintWriter out = new PrintWriter((new OutputStreamWriter(
                socket.getOutputStream())), true);
        String str = in.readLine();
        System.out.println("Echoing: " + str);
        out.println(str + "回来从服务器");
        // Always close the two sockets...
        System.out.println("closing...");
        socket.close();
        s.close();
    }
}

下面是客户端程序:
import java.net.*;
import java.io.*;
public class Test {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 4002);
        BufferedReader in = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));
/* autoFlush - A boolean; if true, the println() methods will flush the output buffer*/
        PrintWriter out = new PrintWriter((new OutputStreamWriter(
                socket.getOutputStream())), true);
        out.println("你好 ");
        String str = in.readLine();
        System.out.println(str);
        System.out.println("closing...");
        socket.close();
    }
}

服务器输出结果:
服务器正等着
Echoing: 你好
closing...

客户端输出结果:
你好 回来从服务器


closing...