Java communication software design 3(updating....)
来源:互联网 发布:德力西选型软件 编辑:程序博客网 时间:2024/06/07 04:10
The third server and client program is about the file transfering.....
FileServer.java:
import java.net.*;import java.io.*;public class FileServer { public static void main (String [] args ) throws IOException { // create socket ServerSocket servsock = new ServerSocket(1234); while (true) { System.out.println("File Server Waiting for Client..."); Socket sock = servsock.accept(); System.out.println("Accepted connection : " + sock); // sendfile File myFile = new File ("CSD.ppt"); byte [] mybytearray = new byte [(int)myFile.length()]; FileInputStream fis = new FileInputStream(myFile); BufferedInputStream bis = new BufferedInputStream(fis); bis.read(mybytearray,0,mybytearray.length); OutputStream os = sock.getOutputStream(); System.out.println("Sending..."); os.write(mybytearray,0,mybytearray.length); os.flush(); sock.close(); } }}
FileClient.java:
import java.net.*;import java.io.*;public class FileClient{ public static void main (String [] args ) throws IOException { int filesize=6022386; // filesize temporary hardcoded long start = System.currentTimeMillis(); int bytesRead; int current = 0; // localhost for testing Socket sock = new Socket("127.0.0.1",1234); System.out.println("Connecting..."); // receive file byte [] mybytearray = new byte [filesize]; InputStream is = sock.getInputStream(); FileOutputStream fos = new FileOutputStream("CSD.ppt"); BufferedOutputStream bos = new BufferedOutputStream(fos); bytesRead = is.read(mybytearray,0,mybytearray.length); current = bytesRead; do { bytesRead = is.read(mybytearray, current, (mybytearray.length-current)); if(bytesRead >= 0) current += bytesRead; } while(bytesRead > -1); bos.write(mybytearray, 0 , current); bos.flush(); long end = System.currentTimeMillis(); System.out.println(end-start); bos.close(); sock.close(); }}
Goal: the file server send the csd.ppt to file client:
the following gif is the test result:
- Java communication software design 3(updating....)
- Java Communication design(Updating)
- Java communication software design2(updating...)
- Software Design
- Software Design
- Software Architecture Design Patterns in Java
- communication architecture design
- Software Deployment, Updating, and Patching
- Communication on Agile Software Projects
- Java Software Solutions : Foundations of Program Design (4th Edition)
- Introduction to Java and Software Design: Swing Update
- agsXMPP Software Design
- Software Modeling and Design
- Embeded Software Design
- Embedded Software Design Engineer
- What is Software Design?
- Software Design Specification Templates
- Some Software Design Principle
- Linux环境变量 概念 用途 详解
- JAVA的web截图功能实现
- Mac 环境变量配置
- 进程系统调用——fork函数的深入理解使用
- 互联网线下活动一周整理 11月23日—29日
- Java communication software design 3(updating....)
- poj 2352
- 向linux内核中添加新的系统调用
- 设置jsp中pageEncoding为UTF-8
- 记录头像
- android 自定义processDialog
- java UIManager.getDefaults()
- SVN,GIT usage
- 单片机片外程序存储器数据存储器操作命令