客户端:

package tt;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.net.Socket; public class ClientTcpSend { public static void main(String[] args) {
int length = 0;
byte[] sendByte = null;
Socket socket = null;
DataOutputStream dout = null;
FileInputStream fin = null;
try {
try {
socket = new Socket();
socket.connect(new InetSocketAddress("127.0.0.1", 33456),10 * 1000);
dout = new DataOutputStream(socket.getOutputStream());
File file = new File("E:\\TU\\DSCF0320.JPG");
fin = new FileInputStream(file);
sendByte = new byte[1024];
dout.writeUTF(file.getName());
while((length = fin.read(sendByte, 0, sendByte.length))>0){
dout.write(sendByte,0,length);
dout.flush();
}
} catch (Exception e) { } finally{
if (dout != null)
dout.close();
if (fin != null)
fin.close();
if (socket != null)
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
} }

服务端:

package test;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket; public class ServerTcpListener implements Runnable { @Override
public void run() { } public static void main(String[] args) {
try {
final ServerSocket server = new ServerSocket(33456);
Thread th = new Thread(new Runnable() { @Override
public void run() {
while (true) {
try {
System.out.println("开始监听。。。");
Socket socket = server.accept();
System.out.println("有链接");
receiveFile(socket);
} catch (Exception e) {
e.printStackTrace();
}
} } });
th.run();
} catch (Exception ex) {
ex.printStackTrace();
}
} public static void receiveFile(Socket socket) throws IOException {
byte[] inputByte = null;
int length = 0;
DataInputStream din = null;
FileOutputStream fout = null;
try {
din = new DataInputStream(socket.getInputStream()); fout = new FileOutputStream(new File("E:\\"+din.readUTF()));
inputByte = new byte[1024];
System.out.println("开始接收数据...");
while (true) {
if (din != null) {
length = din.read(inputByte, 0, inputByte.length);
}
if (length == -1) {
break;
}
System.out.println(length);
fout.write(inputByte, 0, length);
fout.flush();
}
System.out.println("完成接收");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (fout != null)
fout.close();
if (din != null)
din.close();
if (socket != null)
socket.close();
}
} }

最新文章

  1. linux菜鸟日记(2)
  2. tornado 非阻塞方法
  3. [转]理解Linux系统中的load average
  4. express-generator安装时出错,最后用VPS解决
  5. python类及其方法
  6. iOS开发之Objective-c的MD5/SHA1加密算法的实现
  7. Python字符编码详解
  8. ASP.NET MVC 下载列表
  9. jboss 7部署cas3.4.11
  10. Java实现常见排序算法
  11. ios 加水印
  12. QQ对话代码
  13. Ionic3学习笔记(五)动画之使用 animate.css
  14. [ Build Tools ] Repositories
  15. SSM框架整合思想
  16. Vuejs——(9)组件——props数据传递
  17. 在 golang 中使用 Json
  18. 基础练习 2n皇后问题
  19. C#获取电脑型号、系统版本、内存大小、硬盘大小、CPU信息
  20. session 详解

热门文章

  1. FastReport的交叉表实际使用的一个例子
  2. Delphi xe7 FireMonkey / Mobile (Android, iOS)生成 QR Code完整实例
  3. android常用命令
  4. Android笔记:gson处理多层嵌套的复杂形式的json
  5. 【C++】函数指针宏定义
  6. ECSHOP不同商品分类调用不同模板
  7. leap motion
  8. swift-03-构造器(Designated&&Convenience)
  9. Java学习-023-Properties 类 XML 配置文件读取及写入源代码
  10. 我的工具箱之MyEclipse9.1