java-socket传输zip文件实例
来源:互联网 发布:数据库截断 编辑:程序博客网 时间:2024/05/21 14:03
服务器端程序:
import java.io.*;
import java.net.*;
import java.io.BufferedInputStream;
public class SocketServer {
ServerSocket ss=null;
Socket s=null;
DataInputStream inStream=null;
DataOutputStream outStream=null;
FileInputStream fin = null;
public SocketServer() {
try{
ss=new ServerSocket(765);
s.setSoTimeout(3000);
}catch(Exception e){
System.out.println(e.toString());
}
}
void waitForClient(){
try{
while(true){
s=ss.accept();
ThreadServer thread = new ThreadServer(s);
thread.start();
}
}catch(Exception e){
System.out.println(e.toString());
}
}
public static void main(String[] args) {
SocketServer socketServer1 = new SocketServer();
socketServer1.waitForClient();
}
}
线程类:
import java.io.*;
import java.net.*;
class ThreadServer extends Thread{
private Socket socket;
private DataInputStream inStream=null;
private DataOutputStream outStream=null;
private FileInputStream fin = null;
public ThreadServer(Socket sock){
this.socket = sock;
}
public void run(){
boolean bool = false;
//while(!bool){
try{
inStream=new DataInputStream(socket.getInputStream());
outStream=new DataOutputStream(socket.getOutputStream());
fin = new FileInputStream("C:/temp/socket/200212060001_ds.zip");
//socket.setSoTimeout(3000);
byte[] b = new byte[200];
int i;
while((i=fin.read(b))!=-1){
outStream.write(b);
}
fin.close();
socket.close();
//bool = true;
}catch(IOException ex){
System.out.println(ex);
}
//}
}
}
客户端:
import java.net.*;
import java.io.*;
public class SocketClient{
Socket s=null;
DataInputStream inStream=null;
DataOutputStream outStream=null;
FileOutputStream fout = null;
public SocketClient() {
try{
s=new Socket("192.9.207.52",765); //把这里的IP改成你运行SocketServer.class的IP
inStream=new DataInputStream(s.getInputStream());
outStream=new DataOutputStream(s.getOutputStream());
fout = new FileOutputStream("C:/temp/socket/test11.zip");
s.setSoTimeout(3000);
waitData();
}
catch(Exception e){
System.out.println(e.toString());
}
}
void init() throws Exception{
}
void waitData(){
try{
byte[] b = new byte[200];
int i;
while((i=inStream.read(b))!=-1){
fout.write(b);
}
fout.flush();
fout.close();
s.close();
}catch(Exception e){
System.out.println(e.toString());
}
}
public static void main(String[] args) {
SocketClient socketClient1 = new SocketClient();
}
}
- java-socket传输zip文件实例
- java Socket传输文件
- java socket 传输文件
- 利用java socket传输文件
- JAVA应用socket传输文件
- Java使用Socket传输文件
- java 利用socket传输文件
- Java NIO实例-ServerSocketChannel实现Socket传输
- Java实现Socket的TCP传输实例
- java socket通信-传输文件图片
- java socket通信-传输文件图片
- socket的UDP传输文件java源代码
- 简单socket编程,传输文件,JAVA
- java socket通信-传输文件图片
- java nio socket结合netty传输文件
- Java中通过socket来传输文件
- Java:Socket断点传输大文件
- java socket通信-传输文件图片
- android--下拉列表 Spinner。
- android--Java基于Socket文件传输示例
- android--imageView使用sd卡中图片资源
- OA实战项目
- Android--toast使用
- java-socket传输zip文件实例
- IT各种百度云资料
- android--在sd卡中创建文件夹
- android--基于android平台socket服务器端实例
- 【转载】Xcode 4.1~4.6 + iOS 5、iOS 6免证书(iDP)开发+真机调试+生成IPA全攻略
- iOS--串口通讯初始化
- 分数化小数(指定精度)
- Android--模拟器尺寸大小修改
- android--读取sd卡中text文件