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定义的一个简单协议.为什么说这个协议很简单呢, 这是因为服务器只需把收到的客户端的 ...
随机推荐
- UITextView textViewShouldEndEditing
最近做到UITextView, 在取消键盘事件上我以为和UITextField差不多,于是我这样写: UITextView *textView = [[UITextView alloc] initWi ...
- 【转】 iOS开发数据库篇—SQLite简单介绍
开始学SQLite啦, 原文: http://www.cnblogs.com/wendingding/p/3868893.html iOS开发数据库篇—SQLite简单介绍 一.离线缓存 在项目开发中 ...
- Char Varchar Nvarchar区别
char和varchar是一样的字符型,不同在于,varchar比char更灵活,精确,且不占内存空间,当你取同样的字符时,char会在该字符后面加上空格,而varchar则只取得这个字符,比如有字段 ...
- C++封装常用对象和对头文件探索
在C++实际开发中,难免会使用到一些你极为常用的算法(比如笔者经常使用的多线程技术),实现这些算法的类或是全局函数或是命名空间等等经常都要被使用多次,你会有哪些办法来使用呢?笔者有4个办法. 第一个方 ...
- [HttpClient]简单使用GET请求
package com.jerry.httpclient; import java.io.IOException; import org.apache.http.HttpEntity; import ...
- php开发中的url地址传输加密解密函数
function keyED($txt,$encrypt_key) //定义一个keyED { $encrypt_key = md5($encrypt_key); $ctr=0; $tmp = ''; ...
- 平稳退化,JS和HTML标记分离,极致性能的JavaScript图片库
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- connect函数
TCP客户用connect函数来建立与TCP服务器的连接 int connect (int sockfd, const sockaddr * servaddr, socklen_t addrlen); ...
- Excel--java POi
import java.io.File; import java.io.FileOutputStream; import org.apache.commons.io.FileUtils; import ...
- JS之路——浏览器window对象
window对象的方法 window.alert(msg) window.close() window.print() var a = window.setIntval(function,毫秒) // ...