/**
* 1、创建TCP服务端,TCP客户端
* 2、服务端等待客户端连接,客户端连接后,服务端向客户端写入图片
* 3、客户端收到后进行文件保存
* @author Administrator
*
*/
public class ServerTcpListener implements Runnable{ public static void main(String[] args) { try {
final ServerSocket server = new ServerSocket(33456); Thread th = new Thread(new Runnable() {
public void run() {
while (true) {
try {
System.out.println("开始监听...");
Socket socket = server.accept();
System.out.println("有链接");
send(socket);
} catch (Exception e) {
}
}
}
}); th.run(); //启动线程运行
} catch (Exception e) {
e.printStackTrace();
}
} @Override
public void run() { } public static void send(Socket socket) {
byte[] inputByte = null;
int length = 0;
DataOutputStream dos = null;
FileInputStream fis = null;
try {
try {
File file = new File("D:/1.png");
fis = new FileInputStream(file);
dos = new DataOutputStream(socket.getOutputStream()); inputByte = new byte[1024];
while ((length = fis.read(inputByte, 0, inputByte.length)) > 0) {
dos.write(inputByte, 0, length);
dos.flush();
} } finally {
if (fis != null)
fis.close();
if (dos != null)
dos.close();
if (socket != null)
socket.close();
}
} catch (Exception e) {
}
}
}

ServerTcpListener.java

 public class ClientTcpReceive {

     public static void main(String[] args) {

             int length = 0;
byte[] receiveBytes = null;
Socket socket = null;
DataInputStream dis = null;
FileOutputStream fos = null; try {
try {
socket = new Socket("127.0.0.1", 33456); dis = new DataInputStream(socket.getInputStream());
fos = new FileOutputStream(new File("1.png")); receiveBytes = new byte[1024];
System.out.println("开始接收数据...");
while ((length = dis.read(receiveBytes, 0, receiveBytes.length)) > 0) {
System.out.println(length);
fos.write(receiveBytes, 0, length);
fos.flush();
}
System.out.println("完成接收");
} finally {
if (dis != null)
dis.close();
if (fos != null)
fos.close();
if (socket != null)
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
} }

ClientTcpReceive.java

Java TCP服务端向客户端发送图片的更多相关文章

  1. 【转】TCP/UDP简易通信框架源码,支持轻松管理多个TCP服务端(客户端)、UDP客户端

    [转]TCP/UDP简易通信框架源码,支持轻松管理多个TCP服务端(客户端).UDP客户端 目录 说明 TCP/UDP通信主要结构 管理多个Socket的解决方案 框架中TCP部分的使用 框架中UDP ...

  2. swoole创建TCP服务端和客户端

    服务端: server.php <?php //创建Server对象,监听 127.0.0.1:9501端口    $serv = new swoole_server("127.0.0 ...

  3. 基于Select模型的Windows TCP服务端和客户端程序示例

    最近跟着刘远东老师的<C++百万并发网络通信引擎架构与实现(服务端.客户端.跨平台)>,Bilibili视频地址为C++百万并发网络通信引擎架构与实现(服务端.客户端.跨平台),重新复习下 ...

  4. vertx 从Tcp服务端和客户端开始翻译

    写TCP 服务器和客户端 vert.x能够使你很容易写出非阻塞的TCP客户端和服务器 创建一个TCP服务 最简单的创建TCP服务的方法是使用默认的配置:如下 NetServer server = ve ...

  5. TCP/UDP简易通信框架源码,支持轻松管理多个TCP服务端(客户端)、UDP客户端

    目录 说明 TCP/UDP通信主要结构 管理多个Socket的解决方案 框架中TCP部分的使用 框架中UDP部分的使用 框架源码结构 补充说明 源码地址 说明 之前有好几篇博客在讲TCP/UDP通信方 ...

  6. C++封装的基于WinSock2的TCP服务端、客户端

    无聊研究Winsock套接字编程,用原生的C语言接口写出来的代码看着难受,于是自己简单用C++封装一下,把思路过程理清,方便自己后续翻看和新手学习. 只写好了TCP通信服务端,有空把客户端流程也封装一 ...

  7. python创建tcp服务端和客户端

    1.tcp服务端server from socket import * from time import ctime HOST = '' PORT = 9999 BUFSIZ = 1024 ADDR ...

  8. java的服务端与客户端通信(2)

    一.Socket连接与HTTP连接   1.1Socket套接字 套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元.它是网络通信过程中端点的抽象表示,包含进行网络通信 ...

  9. go --socket通讯(TCP服务端与客户端的实现)

    这篇文章主要使用Go语言实现一个简单的TCP服务器和客户端.服务器和客户端之间的协议是 ECHO, 这个RFC 862定义的一个简单协议.为什么说这个协议很简单呢, 这是因为服务器只需把收到的客户端的 ...

随机推荐

  1. 将从网上下载下来的javaweb项目继续配置

    1.将下载下来的项目,看有没有报错,这里推荐的是不变成web项目的方法,直接通过编译到服务器目录 2.报错的问题,一般是包,服务器的包,(tomcat-home)指向自己的bin目录 3.然后是添加s ...

  2. JavaScript--对象+函数

    1. 复杂数据类型 Object ECMAScript中的对象其实就是一组数据(属性)和功能(方法)的集合.    1) 创建Object实例:   1.使用构造函数创建,new Object()   ...

  3. ExtJs API 下载以及部署

    ExtJs API 下载方法 1.进入sencha官网:https://www.sencha.com/ 2.点击“Docs”进入文档帮助页面:http://docs.sencha.com/ 3.点击左 ...

  4. centos 给鼠标右击添加 “打开终端” 菜单项

    1.以root身份在终端执行如下命令 yum -y install nautilus-open-terminal   2.重启操作系统 shutdown -r now

  5. c++面试(二)

    1.宏参数的连接 #define CONS(a,b) (int)(a##e##b) CONS(2,3) =>2e3 =2000 2.const int b=10; int c=20; const ...

  6. linux关机重启命令浅析

    linux关机重启命令 今天我们来介绍下linux系统中常用到的关机重启命令—shutdown.halt.reboot.poweroff以及init. shutdown命令 以安全的方式关闭系统或重启 ...

  7. 谈.Net委托与线程——解决窗体假死

    转自:http://www.cnblogs.com/smartls/archive/2011/04/08/2008981.html#2457370   引言 在之前的<创建无阻塞的异步调用> ...

  8. set_time_limit() 控制页面运行时间

    当你的页面有大量数据时,建议使用set_time_limit()来控制运行时间,默认是30s,所以需要你将执行时间加长点,如 set_time_limit(300)  ,其中将秒数设为0 ,表示持续运 ...

  9. Hadoop-CDH5.7.0 for CentOS7

    一.需求 系统 CentOS 7 最小化安装 JDK环境 JDK版本:1.8.0_91 jdk-8u91-linux-x64.rpm 下载地址:http://www.oracle.com/techne ...

  10. 关于 wait_event_interruptible() 和 wake_up()的使用

    来源:http://blog.csdn.net/allen6268198/article/details/8112551 1. 关于 wait_event_interruptible() 和 wake ...