import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner; public class MediaServer {
private static ServerSocket serverSocket;
private static final int PORT = 12345; public static void main(String[] args) {
System.out.println("Opening port...");
try {
serverSocket = new ServerSocket(PORT);
} catch (IOException e) {
System.out.println("Unable to attach to port!");
System.exit(1);
}
do {
try {
Socket connection = serverSocket.accept();
Scanner in = new Scanner(connection.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
String message = in.nextLine();
System.out.println(message);
if (message.equalsIgnoreCase("image")) {
sendFile("F://apple.jpg", out);
}
if (message.equalsIgnoreCase("sound")) {
sendFile("F://Matthew.mp3", out);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
} while (true);
} private static void sendFile(String filePath, ObjectOutputStream out) throws Exception {
FileInputStream fileInputStream = new FileInputStream(filePath);
long fileLeng = new File(filePath).length();
int length = (int) fileLeng;
byte[] bytes = new byte[(int) length];
fileInputStream.read(bytes);
fileInputStream.close();
out.writeObject(bytes);
out.flush();
} }
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner; public class MediaClient {
private static InetAddress host;
private static final int PORT = 12345; public static void main(String[] args) {
try {
host = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
System.out.println("Host ID not found.");
System.exit(1);
}
try {
Socket connection = new Socket(host, PORT);
ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
PrintWriter out = new PrintWriter(connection.getOutputStream(), true);
Scanner userIn = new Scanner(System.in);
System.out.println("Enter request (image/sound):");
String message = userIn.nextLine();
while (!message.equalsIgnoreCase("image") && !message.equalsIgnoreCase("sound")) {
System.out.println("Try again:");
System.out.println("Enter request (image/sound):");
message = userIn.nextLine();
}
userIn.close();
out.println(message);
getFile(in, message);
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
} private static void getFile(ObjectInputStream in, String message) throws Exception {
byte[] byteArray = (byte[]) in.readObject();
FileOutputStream outputStream;
if (message.equalsIgnoreCase("image")) {
outputStream = new FileOutputStream("E://apple.jpg");
} else {
outputStream = new FileOutputStream("E://Matthew.mp3");
}
outputStream.write(byteArray);
outputStream.close();
}
}

用socket方式传输Image和Sound文件的更多相关文章

  1. python+socket实现网络信息交互及文件传输

    Socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket又称"套接字",应用程序通常通过"套接字" ...

  2. java socket通信-传输文件图片--传输图片

    ClientTcpSend.java   client发送类 package com.yjf.test; import java.io.DataOutputStream; import java.io ...

  3. 基于TCP协议的项目架构之Socket流传输的实现

    项目背景  某银行的影像平台由于使用时间长,服务器等配置原因,老影像系统满足不了现在日益增长的数据量的需求,所以急需要升级改造.传统的影像平台使用的是Oracle数据库和简单的架构来存储数据(视频.图 ...

  4. Protobuf3 + Netty4: 在socket上传输多种类型的protobuf数据

    Protobuf序列化的字节流数据是不能自描述的,当我们通过socket把数据发送到Client时,Client必须知道发送的是什么类型的数据,才能正确的反序列化它.这严重影响限制了C/S功能的实现, ...

  5. 使用socket方式连接Nginx优化php-fpm性能

    Nginx连接fastcgi的方式有2种:TCP和unix domain socket 什么是Unix domain socket?-- 维基百科 Unix domain socket 或者 IPC ...

  6. 网络数据(socket)传输总结

    环境限定:TCP/IP下的socket网络传输:C/C++开发语言,32/64位机. 目前有两种方式对数据进行传输:1)字符流形式,即将数据用字符串表示:2)结构型方式,即将数据按类型直接传输. 1) ...

  7. Node.js初探之GET方式传输

    Node.js初探之GET方式传输 例子:form用GET方法向后台传东西 html文件: <form action="http://localhost:8080/aaa" ...

  8. lnmp使用socket方式连接nginx优化php-fpm性能

    lnmp使用socket方式连接nginx优化php-fpm性能 Nginx连接fastcgi的方式有2种:TCP和unix domain socket 什么是Unix domain socket?- ...

  9. 通俗易懂,C#如何安全、高效地玩转任何种类的内存之Span的脾气秉性(二)。 异步委托 微信小程序支付证书及SSL证书使用 SqlServer无备份下误删数据恢复 把list集合的内容写入到Xml中,通过XmlDocument方式写入Xml文件中 通过XDocument方式把List写入Xml文件

    通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的脾气秉性(二).   前言 读完上篇<通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的本质(一).>,相信大家对sp ...

随机推荐

  1. Careercup - Facebook面试题 - 4713484755402752

    2014-05-02 00:30 题目链接 原题: Given two arrays of sorted integers, merge them keeping in mind that there ...

  2. 不同的source control下配置DiffMerge

    TFS: 1. 打开Option -> Source Control -> Visual Studio TFS -> Configure User Tools; 2. 添加 .*, ...

  3. 【POJ】【2960】S-Nim

    博弈论 这题跟 BZOJ 1874 取石子游戏 差不多 先暴力求出10000以内的SG函数(利用定义来求即可) 然后每次询问直接将SG值异或起来即可…… Source Code Problem: Us ...

  4. KASS分布式文件系统(Kass File System)

    KASS分布式文件系统(Kass File System),简称KFS,是开始公司自主研发的分布式文件存储服务平台.KFS系统架构及功能服务类似Hadoop/GFS/DFS,它通过HTTP-WEB为上 ...

  5. RFID开发笔记 Alien阅读器文档

    1. 开机使用serial connect,完成boot后使用TCP/IP协议与主机通信 2.TagList,是一个活跃标签的列表,这里活跃的含义是在一个间隔里被监听到.如果一个标签之前没有被监听到, ...

  6. 建立docker私有hub

    docker是一个非常好用的虚拟化工具. 下面给出建立私有docker hub的方法.docker将私有hub的环境打包在registry image中 执行指令: docker run -p 500 ...

  7. 深入浅出ES6(一):ES6是什么

    作者 Jason Orendorff github主页 https://github.com/jorendorff ECMAScript发生了什么变化? 编程语言JavaScript是ECMAScri ...

  8. Fragment (一)

      1,简介 Fragement(碎片)允许将Activity拆分成多个完全独立封装的可重用组件,每个组件有它自己的生命周期和UI布局,由此可见,Fragement依赖于Activity,它的生命周期 ...

  9. lintcode: search for a range 搜索区间

    题目 搜索区间 给定一个包含 n 个整数的排序数组,找出给定目标值 target 的起始和结束位置. 如果目标值不在数组中,则返回[-1, -1] 样例 给出[5, 7, 7, 8, 8, 10]和目 ...

  10. 欧拉工程第73题:Counting fractions in a range

    题目链接:https://projecteuler.net/problem=73 n/d的真分数 ,当d<=12000时 在 1/3 and 1/2 之间的有多少个 public class P ...