ä¸é¢æ¯ä¸ä¸ªç®åçé讯å®ä¾ï¼è¿è¡ServeråClientä¹é´çæä»¶ä¼ è¾ããå¦ææ¯ç®åçææ¬ä¼ è¾çè¯ç®åæææ¬æä½çå
容å³å¯ãã
1.æå¡å¨ç«¯
package sterning;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerTest {
int port = 8821;
void start() {
Socket s = null;
try {
ServerSocket ss = new ServerSocket(port);
while (true) {
// éæ©è¿è¡ä¼ è¾çæ件
String filePath = "D:\\lib.rar";
File fi = new File(filePath);
System.out.println("æ件é¿åº¦:" + (int) fi.length());
// public Socket accept() throws
// IOException侦å¬å¹¶æ¥åå°æ¤å¥æ¥åçè¿æ¥ãæ¤æ¹æ³å¨è¿è¡è¿æ¥ä¹åä¸ç´é»å¡ã
s = ss.accept();
System.out.println("建ç«socketé¾æ¥");
DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
dis.readByte();
DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
DataOutputStream ps = new DataOutputStream(s.getOutputStream());
//å°æ件ååé¿åº¦ä¼ ç»å®¢æ·ç«¯ãè¿éè¦çæ£éç¨ææå¹³å°ï¼ä¾å¦ä¸æåçå¤çï¼è¿éè¦å å·¥ï¼å
·ä½å¯ä»¥åè§Think In Java 4théæç°æç代ç ã
ps.writeUTF(fi.getName());
ps.flush();
ps.writeLong((long) fi.length());
ps.flush();
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true) {
int read = 0;
if (fis != null) {
read = fis.read(buf);
}
if (read == -1) {
break;
}
ps.write(buf, 0, read);
}
ps.flush();
// 注æå
³ésocketé¾æ¥å¦ï¼ä¸ç¶å®¢æ·ç«¯ä¼çå¾
serverçæ°æ®è¿æ¥ï¼
// ç´å°socketè¶
æ¶ï¼å¯¼è´æ°æ®ä¸å®æ´ã
fis.close();
s.close();
System.out.println("æä»¶ä¼ è¾å®æ");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String arg[]) {
new ServerTest().start();
}
}
2.socketçUtilè¾
å©ç±»
package sterning;
import java.net.*;
import java.io.*;
public class ClientSocket {
private String ip;
private int port;
private Socket socket = null;
DataOutputStream out = null;
DataInputStream getMessageStream = null;
public ClientSocket(String ip, int port) {
this.ip = ip;
this.port = port;
}
/** *//**
* å建socketè¿æ¥
*
* @throws Exception
* exception
*/
public void CreateConnection() throws Exception {
try {
socket = new Socket(ip, port);
} catch (Exception e) {
e.printStackTrace();
if (socket != null)
socket.close();
throw e;
} finally {
}
}
public void sendMessage(String sendMessage) throws Exception {
try {
out = new DataOutputStream(socket.getOutputStream());
if (sendMessage.equals("Windows")) {
out.writeByte(0x1);
out.flush();
return;
}
if (sendMessage.equals("Unix")) {
out.writeByte(0x2);
out.flush();
return;
}
if (sendMessage.equals("Linux")) {
out.writeByte(0x3);
out.flush();
} else {
out.writeUTF(sendMessage);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
if (out != null)
out.close();
throw e;
} finally {
}
}
public DataInputStream getMessageStream() throws Exception {
try {
getMessageStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
return getMessageStream;
} catch (Exception e) {
e.printStackTrace();
if (getMessageStream != null)
getMessageStream.close();
throw e;
} finally {
}
}
public void shutDownConnection() {
try {
if (out != null)
out.close();
if (getMessageStream != null)
getMessageStream.close();
if (socket != null)
socket.close();
} catch (Exception e) {
}
}
}
3.客æ·ç«¯
package sterning;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class ClientTest {
private ClientSocket cs = null;
private String ip = "localhost";// 设置ææå¡å¨IP
private int port = 8821;
private String sendMessage = "Windwos";
public ClientTest() {
try {
if (createConnection()) {
sendMessage();
getMessage();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private boolean createConnection() {
cs = new ClientSocket(ip, port);
try {
cs.CreateConnection();
System.out.print("è¿æ¥æå¡å¨æå!" + "\n");
return true;
} catch (Exception e) {
System.out.print("è¿æ¥æå¡å¨å¤±è´¥!" + "\n");
return false;
}
}
private void sendMessage() {
if (cs == null)
return;
try {
cs.sendMessage(sendMessage);
} catch (Exception e) {
System.out.print("åéæ¶æ¯å¤±è´¥!" + "\n");
}
}
private void getMessage() {
if (cs == null)
return;
DataInputStream inputStream = null;
try {
inputStream = cs.getMessageStream();
} catch (Exception e) {
System.out.print("æ¥æ¶æ¶æ¯ç¼åé误\n");
return;
}
try {
//æ¬å°ä¿åè·¯å¾ï¼æ件åä¼èªå¨ä»æå¡å¨ç«¯ç»§æ¿èæ¥ã
String savePath = "E:\\";
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
int passedlen = 0;
long len=0;
savePath += inputStream.readUTF();
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(newBufferedOutputStream(new FileOutputStream(savePath))));
len = inputStream.readLong();
System.out.println("æ件çé¿åº¦ä¸º:" + len + "\n");
System.out.println("å¼å§æ¥æ¶æ件!" + "\n");
while (true) {
int read = 0;
if (inputStream != null) {
read = inputStream.read(buf);
}
passedlen += read;
if (read == -1) {
break;
}
//ä¸é¢è¿åº¦æ¡æ¬ä¸ºå¾å½¢çé¢çprograssBaråçï¼è¿éå¦ææ¯ææ件ï¼å¯è½ä¼éå¤æå°åºä¸äºç¸åçç¾åæ¯
System.out.println("æ件æ¥æ¶äº" + (passedlen * 100/ len) + "%\n");
fileOut.write(buf, 0, read);
}
System.out.println("æ¥æ¶å®æï¼æ件å为" + savePath + "\n");
fileOut.close();
} catch (Exception e) {
System.out.println("æ¥æ¶æ¶æ¯é误" + "\n");
return;
}
}
public static void main(String arg[]) {
new ClientTest();
}
}
追é®æé®çæ¯å®¢æ·ç«¯æ¶javaæå¡å¨ç«¯æ¯C++åç