多线程之简单TCP应用
/*
使用TCP协议发送数据
创建发送端Socket对象(创建连接)
获取输出流对象
发送数据
释放资源
*/
public class ClientDemo {
public static void main(String[] args) throws IOException {
System.out.println("Client");
//创建发送端Socket对象(创建连接)
Socket s = new Socket(InetAddress.getByName("127.0.0.1"),10086);
//创建线程对象
ClientThreadSend sent = new ClientThreadSend(s);
ClientThreadReceive receive = new ClientThreadReceive(s);
Thread t1 = new Thread(sent,"ClientSent");
Thread t2 = new Thread(receive,"ClientReceive");
//启动线程
t1.start();
t2.start();
}
}
/*
使用TCP协议接收数据
创建接收端Socket对象
监听(阻塞)
获取输入流对象
获取数据
输出数据
释放资源
*/
public class ServerDemo {
public static void main(String[] args) throws IOException {
System.out.println("Server");
//创建接收端Socket对象
ServerSocket ss = new Serv