Java TCP服务端向客户端发送图片
/**
* 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服务端向客户端发送图片的更多相关文章
- 【转】TCP/UDP简易通信框架源码,支持轻松管理多个TCP服务端(客户端)、UDP客户端
[转]TCP/UDP简易通信框架源码,支持轻松管理多个TCP服务端(客户端).UDP客户端 目录 说明 TCP/UDP通信主要结构 管理多个Socket的解决方案 框架中TCP部分的使用 框架中UDP ...
- swoole创建TCP服务端和客户端
服务端: server.php <?php //创建Server对象,监听 127.0.0.1:9501端口 $serv = new swoole_server("127.0.0 ...
- 基于Select模型的Windows TCP服务端和客户端程序示例
最近跟着刘远东老师的<C++百万并发网络通信引擎架构与实现(服务端.客户端.跨平台)>,Bilibili视频地址为C++百万并发网络通信引擎架构与实现(服务端.客户端.跨平台),重新复习下 ...
- vertx 从Tcp服务端和客户端开始翻译
写TCP 服务器和客户端 vert.x能够使你很容易写出非阻塞的TCP客户端和服务器 创建一个TCP服务 最简单的创建TCP服务的方法是使用默认的配置:如下 NetServer server = ve ...
- TCP/UDP简易通信框架源码,支持轻松管理多个TCP服务端(客户端)、UDP客户端
目录 说明 TCP/UDP通信主要结构 管理多个Socket的解决方案 框架中TCP部分的使用 框架中UDP部分的使用 框架源码结构 补充说明 源码地址 说明 之前有好几篇博客在讲TCP/UDP通信方 ...
- C++封装的基于WinSock2的TCP服务端、客户端
无聊研究Winsock套接字编程,用原生的C语言接口写出来的代码看着难受,于是自己简单用C++封装一下,把思路过程理清,方便自己后续翻看和新手学习. 只写好了TCP通信服务端,有空把客户端流程也封装一 ...
- python创建tcp服务端和客户端
1.tcp服务端server from socket import * from time import ctime HOST = '' PORT = 9999 BUFSIZ = 1024 ADDR ...
- java的服务端与客户端通信(2)
一.Socket连接与HTTP连接 1.1Socket套接字 套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元.它是网络通信过程中端点的抽象表示,包含进行网络通信 ...
- go --socket通讯(TCP服务端与客户端的实现)
这篇文章主要使用Go语言实现一个简单的TCP服务器和客户端.服务器和客户端之间的协议是 ECHO, 这个RFC 862定义的一个简单协议.为什么说这个协议很简单呢, 这是因为服务器只需把收到的客户端的 ...
随机推荐
- java邮件客户端
/*** *邮件VO **/package net.jk.util.email.vo; import java.util.Date; import java.util.List; import net ...
- J2EE初探
J2EE概述 3层结构 4层模型 13项核心技术 J2EE容器 J2EE的优势与缺陷 J2EE概述 Java 2平台有3个版本,分别是适用于小型设备和智能卡的Java 2平台Micro版(Java ...
- 七.生成n位随机字符串
--1.借助newid() go --创建视图(因为在函数中无法直接使用newid()) create view vnewid as select newid() N'MacoId'; go --创建 ...
- Swift - 22 - 循环结构
//: Playground - noun: a place where people can play import UIKit // for-in for i in -99...99 { i * ...
- DOM4J 读取XML配置文件进行数据库连接
介绍介绍DOM4J. 据说是非常优秀非常优秀的Java XML API(Dom4j is an easy to use, open source library for working ...
- nodejs新手教程中upload file的问题
可参见: http://cnodejs.org/topic/50234890f767cc9a51f88481 request.setEncoding("utf8");应注释掉.
- C# Trim方法去除字符串两端的指定字符
var str= ",2,3,4,6,7,"; var str2 = str.Trim(new char[] { ',' }); //去除字符串str两端的','字符. //则st ...
- eclipse中如何导入jar包
如图,首先右键点击项目,选择最下面的properties, 然后进去之后点击java build path,右边会出来4个选项卡,选择libraries, 这时候最右边会有多个选项,第一个add ja ...
- python运维开发之第六天
Python面向对象 python从设计之初就已经是一门面向对象的语言,在python中创建一个类和对象很容易. 面向对象简介:类(class),类变量,object(基类),实例变量,构造函数,封装 ...
- 2-路插入排序(2-way Insertion Sort)的C语言实现
原创文章,转载请注明来自钢铁侠Mac博客http://www.cnblogs.com/gangtiexia 2-路插入排序(2-way Insertion Sort)的基本思想: 比fis ...