这是用压缩方式传输传输文件的例子
=========server
int ret=0;
byte[] tmpdata=new byte[4096];
byte[] data;
FileInputStream in=new FileInputStream("Test.txt");
Socket client=server.accept();
BufferedOutputStream out=new BufferedOutputStream(new GZIPOutputStream(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:
int ret;
byte[] tmpdata=new byte[4096];
byte[] data;
Socket c=new Socket("heifei", 5050);
InputStream in=c.getInputStream();
FileOutputStream f=new FileOutputStream("Test2.gz");
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();
=========server
int ret=0;
byte[] tmpdata=new byte[4096];
byte[] data;
FileInputStream in=new FileInputStream("Test.txt");
Socket client=server.accept();
BufferedOutputStream out=new BufferedOutputStream(new GZIPOutputStream(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:
int ret;
byte[] tmpdata=new byte[4096];
byte[] data;
Socket c=new Socket("heifei", 5050);
InputStream in=c.getInputStream();
FileOutputStream f=new FileOutputStream("Test2.gz");
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();
解决方案 »
- ==================处理字符串===============
- 3道java基础的问题
- [不知算不算]发现JDK中一个BUG
- 将数字转为中文金额的大写方式(Java版)
- 哪位有java帮助文档 中文的
- 有关classpath的问题
- ◎◎◎昨儿晚上和他打招呼,被冷冰冰的拒绝了,求救◎◎◎
- 16进制整数显示的问题
- 最复杂的循环啊~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
- 100分求developer2000的下载地址,谢谢了
- 一个很简单的问题(javaMail发送邮件的问题!)
- 200分!讨论JB7中OpenTool的开发经验
不知道符合不符合你的要求///
// Very simple server that just
// echoes whatever the client sends.
import java.io.*;
import java.net.*;
public class JabberServer {
// Choose a port outside of the range 1-1024:
public static final int PORT = 9090;
public static void main(String[] args)
throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.println(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
while (true) {
String str = in.readLine();
if (str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
// Always close the two sockets...
} finally {
System.out.println("closing...");
socket.close();
}
} finally {
s.close();
}
}
} ///:~
//: c15:JabberClient.java
// Very simple client that just sends
// lines to the server and reads lines
// that the server sends.
import java.net.*;
import java.io.*;
public class JabberClient {
public static void main(String[] args)
throws IOException {
// Passing null to getByName() produces the
// special "Local Loopback" IP address, for
// testing on one machine w/o a network:
InetAddress addr =
InetAddress.getByName(null);
// Alternatively, you can use
// the address or name:
// InetAddress addr =
// InetAddress.getByName("127.0.0.1");
// InetAddress addr =
// InetAddress.getByName("localhost");
System.out.println("addr = " + addr);
Socket socket =
new Socket(addr, JabberServer.PORT);
// Guard everything in a try-finally to make
// sure that the socket is closed:
try {
System.out.println("socket = " + socket);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
for(int i = 0; i < 10; i ++) {
out.println("howdy " + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
} finally {
System.out.println("closing...");
socket.close();
}
}
} ///:~