黑马程序员——网络编程之 TCP 编程

来源:互联网 发布:linux下python环境 编辑:程序博客网 时间:2024/06/05 07:31


01.------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

一、概述

      本文主要是在上一遍文章的基础上写的。TCP编程的重要性,在此不用多说想必大家定然知道。TCP在软件世界中应用十分广泛,大部分软件中几乎都用的有TCP协议,比如QQ,飞信等,可以通过360的进程管理进行查看,在此就不过多的叙述。本文主要以多线程实现上传功能,旨在让大家快速掌握TCP编程。

二、TCP的特点

      以下特点主要是和UDP进行对比:

      (1)面向连接(具有3次握手机制)。

      (2)传输效率相对来说较低

      (3)传输数据安全

      (4)对传输数据长度没有限制。

三、多线程实现上传

      与上一篇blog一样,在此不做过多的叙述,源码如下。

      客户端源码:

package com.dyn.itheima.test8;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {

 public static void main(String[] args) {

  // 创建字节输出流对象
  BufferedInputStream inputStream = null;
  // 创建字节输出流对象
  BufferedOutputStream outputStream = null;
  // 创建套接字对象
  Socket socket = null;
  try {
   socket = new Socket("127.0.0.1", 10086);
   // 封装输出流对象
   inputStream = new BufferedInputStream(new FileInputStream(
     "C:\\Users\\Administrator\\Desktop\\aa.jpg"));
   // 封装输出流对象
   outputStream = new BufferedOutputStream(socket.getOutputStream());
   // 数据写入服务器

   byte[] data = new byte[1024];
   int len = 0;

   while ((len = inputStream.read(data)) != -1) {
                 outputStream.write(data, 0, len);
                 outputStream.flush();
   }
   socket.shutdownOutput();

  } catch (UnknownHostException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   if(socket.isClosed()){
    try {
     socket.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }
}

      服务器源码:

package com.dyn.itheima.test8;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

 public static void main(String[] args) {
  
  //创建服务端套接字
  ServerSocket serverSocket = null;
  try {
   serverSocket = new ServerSocket(10086);
   //开启服务线程
   while(true){
    Socket socket = serverSocket.accept();
    new Thread(new ServerThread(socket)).start();
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   if(serverSocket.isClosed()){
    try {
     serverSocket.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }

 }
}

       服务器线程源码:

package com.dyn.itheima.test8;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerThread implements Runnable {

 
 // 创建套接字对象
 private Socket socket = null;
 // 创建字节输出流对象
 private BufferedInputStream inputStream = null;
 // 创建字节输出流对象
 private BufferedOutputStream outputStream = null;
 
    public ServerThread(Socket socket){
     this.socket = socket;
    }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  try {
   // 获取套接字输入流,并将其封装到BufferedInputStream中
   inputStream = new BufferedInputStream(socket.getInputStream());
   // 封装outputStream对象
   outputStream = new BufferedOutputStream(new FileOutputStream(
     "dyn.jpg"));
   // 声明数组缓冲区
   byte[] data = new byte[1024];
   int len = 0;
   // 将数据输出
   while ((len = inputStream.read(data)) != -1) {
    outputStream.write(data, 0, len);
    // 刷新缓冲区
    outputStream.flush();
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   if (socket.isClosed()) {
    try {
     socket.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }
}


四、总结

       由于本人技术有限,上面代码可能存在不合理的地方,还望大家积极点评,不吝赐教。


0 0
原创粉丝点击