当前位置: 技术问答>java相关
100分:~~请哪位大侠随便用java写一个socket传文件的例子,服务器和客户端写全哦!!~~
来源: 互联网 发布时间:2015-11-19
本文导语: 另外想问一下: socket编程中: DataInputStream的 writeBytes()方法 和 writeUTF()方法有什么区别? 谢谢!!! | mport java.io.*; import java.net.*; import java.util.zip.*; class FileSocketServ{ public...
另外想问一下:
socket编程中:
DataInputStream的
writeBytes()方法
和
writeUTF()方法有什么区别?
谢谢!!!
socket编程中:
DataInputStream的
writeBytes()方法
和
writeUTF()方法有什么区别?
谢谢!!!
|
mport java.io.*;
import java.net.*;
import java.util.zip.*;
class FileSocketServ{
public void start()
throws IOException{
int ret=0;
byte[] tmpdata=new byte[4096];
byte[] data;
ServerSocket s=new ServerSocket(5918);
while(true){
FileInputStream in=new FileInputStream("test.exe");
Socket client=s.accept();
GZIPOutputStream out=new GZIPOutputStream(new BufferedOutputStream(client.getOutputStream()));
do{
ret=in.read(tmpdata);
System.out.println(ret);
if(ret==-1) break;
data=new byte[ret];
System.arraycopy(tmpdata, 0, data, 0, ret);
out.write(data);
out.flush();
}while(true);
in.close();
out.close();
}
}
}
client===
import java.io.*;
import java.net.*;
import java.util.zip.*;
class FileSocketClnt{
public static void main(String[] args)
throws IOException{
int ret;
byte[] tmpdata=new byte[4096];
byte[] data;
Socket c=new Socket("zxf", 5918);
GZIPInputStream in=new GZIPInputStream(c.getInputStream());
FileOutputStream f=new FileOutputStream("Test2.exe");
do{
ret=in.read(tmpdata);
if(ret==-1)break;
data=new byte[ret];
System.arraycopy(tmpdata, 0, data, 0, ret);
f.write(data);
f.flush();
}while(true);
f.close();
in.close();
c.close();
}
}
import java.net.*;
import java.util.zip.*;
class FileSocketServ{
public void start()
throws IOException{
int ret=0;
byte[] tmpdata=new byte[4096];
byte[] data;
ServerSocket s=new ServerSocket(5918);
while(true){
FileInputStream in=new FileInputStream("test.exe");
Socket client=s.accept();
GZIPOutputStream out=new GZIPOutputStream(new BufferedOutputStream(client.getOutputStream()));
do{
ret=in.read(tmpdata);
System.out.println(ret);
if(ret==-1) break;
data=new byte[ret];
System.arraycopy(tmpdata, 0, data, 0, ret);
out.write(data);
out.flush();
}while(true);
in.close();
out.close();
}
}
}
client===
import java.io.*;
import java.net.*;
import java.util.zip.*;
class FileSocketClnt{
public static void main(String[] args)
throws IOException{
int ret;
byte[] tmpdata=new byte[4096];
byte[] data;
Socket c=new Socket("zxf", 5918);
GZIPInputStream in=new GZIPInputStream(c.getInputStream());
FileOutputStream f=new FileOutputStream("Test2.exe");
do{
ret=in.read(tmpdata);
if(ret==-1)break;
data=new byte[ret];
System.arraycopy(tmpdata, 0, data, 0, ret);
f.write(data);
f.flush();
}while(true);
f.close();
in.close();
c.close();
}
}