一个简单的TCP模拟实现(一)

【一个简单的TCP模拟实现(一)】现在我想实现一个基于UDP之上,实现可靠的传输。仿造TCP做一个简单的可靠传输,TCP的可靠传输在于:

  • 解决了乱序
  • 超时重传
  • 拥塞控制
  • 流量控制
哈哈,工程就叫MUTP好了!
基于socket的大文件传输 这是一个udp传输音乐的例子,6M的音乐最后4M,播放起来有很多模糊的地方,wireshark抓包,发现丢了一些。
MUTP希望能实现可靠传输,传输完整的文件
  • client
package main.client; import main.common.DataPacket; import main.common.DataPacketHeader; import main.common.mutpConst; import java.io.*; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketException; /** * Created by liqiushi on 2017/11/22. */ public class Client { public static void main(String[] args) throws IOException {String dstPort = mutpConst.dstPort; String dstHost = mutpConst.dstHost; String srcPort = mutpConst.srcPort; String srcHost = mutpConst.srcHost; InetSocketAddress clientSocketAddr = new InetSocketAddress(srcHost,Integer.parseInt(srcPort)); InetSocketAddress serverSocketAddr = new InetSocketAddress(dstHost,Integer.parseInt(dstPort)); try { DatagramSocket socket = new DatagramSocket(clientSocketAddr); /*private boolean ACK; private boolean SYN; private int ackNum; private int seqNum; */ DataPacketHeader dph = new DataPacketHeader(true,true,1,1); DataPacket dp = new DataPacket(dph); ByteArrayOutputStream baos = null; ObjectOutputStream dos = null; //读取文件流 RandomAccessFile accessFile = null; accessFile = new RandomAccessFile("D:/CloudMusic/五月天 - 倔强.mp3","r"); int readSize = -1; int count = 0; while((readSize = accessFile.read(dp.getBuf(),0,dp.getBuf().length)) != -1){ count++; baos = new ByteArrayOutputStream(); dos = new ObjectOutputStream(baos); dph.setAckNum(count); dph.setSeqNum(readSize); dos.writeUnshared(dp); dos.flush(); byte onceBuf[] = baos.toByteArray(); DatagramPacket dpk = new DatagramPacket(onceBuf,onceBuf.length,serverSocketAddr); socket.send(dpk); baos.close(); dos.close(); //dp.setBuf(new byte[51200]); } System.out.println(count); } catch (SocketException e) { e.printStackTrace(); } } }

  • server
package main.server; import main.common.DataPacket; import main.common.mutpConst; import java.io.*; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketException; /** * Created by liqiushi on 2017/11/22. */ public class server { public static void main(String[] args) { String dstPort = mutpConst.dstPort; String dstHost = mutpConst.dstHost; String srcPort = mutpConst.srcPort; String srcHost = mutpConst.srcHost; InetSocketAddress clientSocketAddr = new InetSocketAddress(srcHost, Integer.parseInt(srcPort)); InetSocketAddress serverSocketAddr = new InetSocketAddress(dstHost, Integer.parseInt(dstPort)); ByteArrayInputStream bais = null; ObjectInputStream ois = null; DatagramSocket socket = null; BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(new FileOutputStream("E:/5.mp3")); socket = new DatagramSocket(serverSocketAddr); //DataPacketHeader dph = new DataPacketHeader(true,true,12,1); //DataPacket dp = new DataPacket(dph); byte recvBuf[] = new byte[1024 * 100]; DatagramPacket recvDp = new DatagramPacket(recvBuf, recvBuf.length); while (true) { socket.receive(recvDp); System.out.println("发送后包的大小:" + recvDp.getLength()); bais = new ByteArrayInputStream(recvBuf); ois = new ObjectInputStream(bais); DataPacket dataPacket = (DataPacket) ois.readObject(); //Object object = ois.readObject(); //读入文件 System.out.println(dataPacket.getHeader().getAckNum()); byte[] fileByte = dataPacket.getBuf(); bos.write(fileByte,0,fileByte.length); } // bos.flush(); /*byte buf[] = baos.toByteArray(); DatagramPacket dpk = new DatagramPacket(buf,buf.length,serverSocketAddr); socket.send(dpk); */ } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (bais != null) { bais.close(); } if (ois != null) { ois.close(); } } catch (IOException e) { e.printStackTrace(); } if (socket != null) { socket.close(); } } } }

    推荐阅读