?? socketthread.java
字號:
package daen.graduate.trans;
import java.net.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.*;
import java.net.*;
/**
* Socket線程
*/
public class SocketThread extends Thread {
public SocketThread() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public final static int CACHE_SIZE = 10240; //緩存大小
public final static int FILE_TRANS_STATUS_FILENAME = 0x01; //文件名傳輸狀態
public final static int FILE_TRANS_STATUS_CONTEXT = 0x02; //文件內容傳輸狀態
public final static int FILE_TRANS_STATUS_WAITFORCONFIRM = 0x03; //等待確認接收文件
public final static int FILE_TRANS_STATUS_SUCCESS = 0x04; //文件傳輸成功
public final static int FILE_TRANS_STATUS_FAIL = 0x05; //文件傳輸失敗
public final static int FILE_TRANS_STATUS_CANCELTRANS = 0x06; //取消文件傳輸
public final static int PACKAGE_TYPE_FILENAME = 0x01; //文件名包
public final static int PACKAGE_TYPE_CONTEXT = 0x02; //文件內容包
public final static int PACKAGE_TYPE_CONFIRMRECEIVE = 0x03; //是否接收文件
private Socket aSocket; //套接字
private String serverName; //服務器名稱
private DataInputStream dis; //輸入流
private DataOutputStream dos; //輸出流
private DataInputStream fDis; //文件輸入流
private RandomAccessFile raf; //文件輸出流
private boolean fileSender = false; //文件發送者
private boolean running = false; //線程運行
public int fileTransStatus = 0x0; //文件傳輸狀態
private File aFile; //傳輸的文件
public long fileSize; //文件大小
private String fileName; //文件名稱
private String errorMessage; //錯誤消息
private long transFileLength = 0; //已傳輸字節數
private byte [] dataBuf;
private String message; //驗證消息
private String IP; //目標IP
private int port; //目標端口
private boolean fileTransed=false; //文件是否已經開始傳輸
int count=0;
//接收者構造函數
public SocketThread(Socket aSocket) {
this.aSocket = aSocket;
try {
aSocket.setSoTimeout(300000);
} catch (SocketException ex) {
}
byte[] address = aSocket.getInetAddress().getAddress();
IP = (address[0] & 0xff) + "." + (address[1] & 0xff) + "." +
(address[2] & 0xff) + "." + (address[3] & 0xff);
try {
dis = new DataInputStream(aSocket.getInputStream());
dos = new DataOutputStream(aSocket.getOutputStream());
fileTransStatus = FILE_TRANS_STATUS_FILENAME;
} catch (IOException ex) {
setError("創建連接錯誤!");
}
try {
aSocket.setReceiveBufferSize(CACHE_SIZE*2);
} catch (SocketException ex1) {
ex1.printStackTrace();
}
dataBuf=new byte[CACHE_SIZE+100];
}
//發送者構造函數
public SocketThread(String serverName, int portNo, String fileName,
String message) {
aFile = new File(fileName);
this.fileName = aFile.getName();
this.fileSize = fileSize;
fileSender = true;
if (message != null) {
this.message = message;
}
this.IP = serverName;
this.port = portNo;
dataBuf=new byte[CACHE_SIZE];
}
//線程執行函數
public void run() {
running = true;
if (fileSender) {
try {
aSocket = new Socket(IP, port);
aSocket.setSoTimeout(300000);
aSocket.setSendBufferSize(CACHE_SIZE*2);
dos = new DataOutputStream(aSocket.getOutputStream());
dis = new DataInputStream(aSocket.getInputStream());
fDis = new DataInputStream(new FileInputStream(aFile));
fileTransStatus = FILE_TRANS_STATUS_FILENAME;
} catch (UnknownHostException ex1) {
ex1.printStackTrace();
setError("連接服務器錯誤!");
} catch (IOException ex1) {
ex1.printStackTrace();
setError("創建連接錯誤!");
}
} while (running) {
if (fileSender) {
sendFile();
} else {
receiveFile();
}
try {
Thread.sleep(6);
} catch (InterruptedException ex) {
}
}
}
//從socket讀
private int readFromSocket(byte[] data) throws IOException {
int length = 0;
length = fDis.read(data);
return length;
}
//從socket讀
private int readFromSocket() throws IOException {
int buf = 0;
buf = dis.readInt();
return buf;
}
//從文件讀
private int readFromFile(byte[] data,int off,int length) {
int len=0;
try {
len = fDis.read(data,off,length);
} catch (IOException ex) {
setError("文件讀取錯誤!");
}
return len;
}
//寫入socket
private void writeToSocket(byte[] data) throws IOException {
dos.write(data);
}
//寫入文件
private void writeToFile(byte[] data,int off,int length) throws IOException {
raf.write(data,off,length);
}
//寫入socket
private void writeToSocket(int data) throws IOException {
dos.writeInt(data);
}
private void writeToSocket(long data) throws IOException {
dos.writeLong(data);
}
private long readLongFromSocket() throws IOException {
return dis.readLong();
}
//打包
private byte[] doPackage(byte[] data, int length) throws IOException {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream bufDos = new DataOutputStream(buf);
DataOutputStream baosDos = new DataOutputStream(baos);
switch (fileTransStatus) {
case FILE_TRANS_STATUS_FILENAME: {
bufDos.writeInt(PACKAGE_TYPE_FILENAME);
bufDos.writeInt(fileName.getBytes().length);
bufDos.write(fileName.getBytes());
bufDos.writeLong(fileSize);
if (message!=null) {
bufDos.writeInt(message.getBytes().length);
bufDos.write(message.getBytes());
} else {
bufDos.writeInt(-1);
}
baosDos.writeInt(buf.toByteArray().length);
baosDos.write(buf.toByteArray());
break;
}
case FILE_TRANS_STATUS_CONTEXT: {
bufDos.writeInt(PACKAGE_TYPE_CONTEXT);
if ((transFileLength + length) >= fileSize) {
bufDos.writeInt(0);
} else {
bufDos.writeInt(1);
}
bufDos.writeInt(length);
bufDos.write(data, 0, length);
baosDos.writeInt(buf.toByteArray().length);
baosDos.write(buf.toByteArray());
break;
}
}
return baos.toByteArray();
}
//停止線程
public void stopThread() {
running = false;
try {
if (dis != null) {
dis.close();
}
if (dos != null) {
dos.close();
}
if (fDis != null) {
fDis.close();
}
if (raf != null) {
raf.close();
}
} catch (Exception ex) {
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -