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. How to use Android Activity's finish(), onDestory() and System.exit(0) methods

    Activity.finish() Calling this method will let the system know that the programmer wants the current ...

  2. shell date

    .查看指定时间的时间戳 查看当前时间 #date +%s 查看指定时间 #date -d -- +%s #date -d +%s .将时间戳转换成date #date -d '1970-01-01 U ...

  3. mac mysql安装

    一.安装 1.下载软件包直接安装即可: http://rj.baidu.com/soft/detail/25675.html?ald 安装完成后root默认密码为空: 二.修改密码 直接修改密码会提示 ...

  4. ffmpeg参数解释

    基本选项: -formats 输出所有可用格式 -f fmt 指定格式(音频或视频格式) -i filename 指定输入文件名,在linux下当然也能指定: 0.0(屏幕录制)或摄像头 -y 覆盖已 ...

  5. curPos和tgtPos

    curpos tgtpos 乍一看以为是当前位置和目标位置,但在项目里面这两个位置有点坑 当客户端玩家移动或者AI里面的位置,会把获得的位置付给tgtpos 而以前的tgtpos会付给curpos 所 ...

  6. HDU1002 -A + B Problem II(大数a+b)

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. linux下如何导入导出MySQL数据库

    一.导出:用mysqldump命令行命令格式mysqldump -u 用户名 -p 数据库名 > 数据库名.sql范例:mysqldump -u root -p abc > abc.sql ...

  8. android 解析XML方式(一)

    在androd手机中处理xml数据时很常见的事情,通常在不同平台传输数据的时候,我们就可能使用xml,xml是与平台无关的特性,被广泛运用于数据通信中,那么在android中如何解析xml文件数据呢? ...

  9. jquery div层级选择器

    div id="modelName" class="modelName"> <!-- 车系的层 --> <div name=" ...

  10. hdu 4473 Exam 数学

    思路: 将条件转化为满足abc<=n的abc的数目. 1.3个数相等时,为 A; 2.有2个数相等时,为 B; 3.都不相等时,为 C. 则结果为A+3*B+6*C. 代码如下: #includ ...