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. Java 8 VM GC Tunning Guide Charter 6

    第六章 并行GC The Parallel Collector The parallel collector (also referred to here as the throughput coll ...

  2. linux-CentOS6.4下安装oracle11g详解

    参考地址:http://dengqsintyt.iteye.com/blog/1991930

  3. jQuery基于ajax实现星星评论代码

    本文实例讲述了jQuery基于ajax实现星星评论代码.分享给大家供大家参考.具体如下: 这里使用jquery模仿点评网的星星评论功能,Ajax评论模块,鼠标点击星星即可评价,下边是分数,可以点击后给 ...

  4. 20160725noip模拟赛“Paodekuai” alexandrali

    T1: 我们可以用火柴棒来表示十进制下的0~9, 如图所示 现给定火柴数n, 问用这n根火柴能组成的最小数和最大数分别是多少. 所有火柴必须全部用完, 并且所有数字必须是正的且不含前缀零. [解题] ...

  5. PHP之list()函数讲解

    定义和用法 list() 函数用数组中的元素为一组变量赋值. 注意,与 array() 类似,list() 实际上是一种语言结构,不是函数. 语法 list(var1,var2...) 参数 描述 v ...

  6. 【面试题021】包含min函数的栈

    [面试题021]包含min函数的栈  MinStack.cpp: 1234567891011121314151617181920212223242526272829303132333435363738 ...

  7. poj 2387 Til the Cows Come Home (最短路,dijkstra模版题)

    题目 #define _CRT_SECURE_NO_WARNINGS #include<string.h> #include<stdio.h> #include<math ...

  8. POJ 3292

    Semi-prime H-numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7059   Accepted: 3 ...

  9. POJ 2080 Calendar(很水的模拟)

    刚开始一直WA,才发现原来代码中两处减去年份.月份的天数的判断条件用的是>=,虽然最后考虑n=0要退回一天的情况,但还是WA.后来改成>的条件判断,省去了考虑n=0的麻烦,AC. 此题无非 ...

  10. java核心技术记录之java术语

    术语名 缩写 解释 Java Development Kit JDK 编写java程序的程序员使用的软件 Java Runtime Environment JRE 运行java程序的用户使用的软件 S ...