用socket方式传输Image和Sound文件
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文件的更多相关文章
- python+socket实现网络信息交互及文件传输
Socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket又称"套接字",应用程序通常通过"套接字" ...
- java socket通信-传输文件图片--传输图片
ClientTcpSend.java client发送类 package com.yjf.test; import java.io.DataOutputStream; import java.io ...
- 基于TCP协议的项目架构之Socket流传输的实现
项目背景 某银行的影像平台由于使用时间长,服务器等配置原因,老影像系统满足不了现在日益增长的数据量的需求,所以急需要升级改造.传统的影像平台使用的是Oracle数据库和简单的架构来存储数据(视频.图 ...
- Protobuf3 + Netty4: 在socket上传输多种类型的protobuf数据
Protobuf序列化的字节流数据是不能自描述的,当我们通过socket把数据发送到Client时,Client必须知道发送的是什么类型的数据,才能正确的反序列化它.这严重影响限制了C/S功能的实现, ...
- 使用socket方式连接Nginx优化php-fpm性能
Nginx连接fastcgi的方式有2种:TCP和unix domain socket 什么是Unix domain socket?-- 维基百科 Unix domain socket 或者 IPC ...
- 网络数据(socket)传输总结
环境限定:TCP/IP下的socket网络传输:C/C++开发语言,32/64位机. 目前有两种方式对数据进行传输:1)字符流形式,即将数据用字符串表示:2)结构型方式,即将数据按类型直接传输. 1) ...
- Node.js初探之GET方式传输
Node.js初探之GET方式传输 例子:form用GET方法向后台传东西 html文件: <form action="http://localhost:8080/aaa" ...
- lnmp使用socket方式连接nginx优化php-fpm性能
lnmp使用socket方式连接nginx优化php-fpm性能 Nginx连接fastcgi的方式有2种:TCP和unix domain socket 什么是Unix domain socket?- ...
- 通俗易懂,C#如何安全、高效地玩转任何种类的内存之Span的脾气秉性(二)。 异步委托 微信小程序支付证书及SSL证书使用 SqlServer无备份下误删数据恢复 把list集合的内容写入到Xml中,通过XmlDocument方式写入Xml文件中 通过XDocument方式把List写入Xml文件
通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的脾气秉性(二). 前言 读完上篇<通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的本质(一).>,相信大家对sp ...
随机推荐
- 如何自定义Liferay 7 portal的Log in登录界面
前提: 1. Liferay portal 7 2. Liferay IDE 3.0.1 Liferay现有的工具中提供了很多修改portal的模板,以满足开发者的各种自定义需求. 修改的原理是利用M ...
- WPF多语言化的实现
Metro插件系统系列就暂时停一下,这次我们讨论一下WPF的资源本地化实现,主要用到的:CultureInfo,ResourceManger,MarkupExtension,RESX文件,这些都是.N ...
- 2432: [Noi2011]兔农 - BZOJ
Description 农夫栋栋近年收入不景气,正在他发愁如何能多赚点钱时,他听到隔壁的小朋友在讨论兔子繁殖的问题. 问题是这样的:第一个月初有一对刚出生的小兔子,经过两个月长大后,这对兔子从第三个月 ...
- sv_target_output dx11
http://msdn.microsoft.com/en-us/library/windows/desktop/bb509647(v=vs.85).aspx
- CentOS(RHEL) 操作备忘
1.安装中文语言包及切换 yum groupinstall chinese-support vi /etc/sysconfig/i18n change en_US to zh_CN 2.用户自动登录 ...
- POJ 1852
#include <iostream> using namespace std; int main() { //freopen("acm.acm","r&qu ...
- 深入浅出ES6(一):ES6是什么
作者 Jason Orendorff github主页 https://github.com/jorendorff ECMAScript发生了什么变化? 编程语言JavaScript是ECMAScri ...
- POJ 1062 昂贵的聘礼(Dijkstra)
题意 : 真真是做POJ第一次遇到中文题,好吧,虽然语言通了,我一开始也没看懂样例什么意思,题意的话就是说这个探险家想娶酋长的女儿,但是没有钱,酋长说他可以用祭司的水晶球或者皮袄来换取少花一部分钱,同 ...
- linux出现bash: ./java: cannot execute binary file 问题的解决办法
问题现象描述: 到orcal官网上下载了两个jdk: (1)jdk-7u9-linux-i586.tar.gz ------------>32位 (2)jdk-7u9-linux-x64.tar ...
- 朴素贝叶斯方法(Naive Bayes Method)
朴素贝叶斯是一种很简单的分类方法,之所以称之为朴素,是因为它有着非常强的前提条件-其所有特征都是相互独立的,是一种典型的生成学习算法.所谓生成学习算法,是指由训练数据学习联合概率分布P(X,Y ...