tcp是一个有连接的可靠的连接
服务器端:主要是建议一个等待连接的端口,等待客户机的连接,等待线程,用户之后,获取输入流,输出流,然后利用输入流输出流进行传送文件。
传送文件时候,因为文件比较大,所以需要分为多次传送。传送的次序,首先传送文件的名字,文件的长度,然后分多次传送文件的内容。
ZFile
import java.net.Socket;
import java.io.*;
public class ZFile extends File {
public ZFile(String parent, String child) {
super(parent, child);
// TODO Auto-generated constructor stub
}
public ZFile(String pathname) {
super(pathname);
// TODO Auto-generated constructor stub
}
public ZFile(File parent, String child) {
super(parent, child);
// TODO Auto-generated constructor stub
}
public void sendFile(Socket connectionSocket) throws Exception {
DataOutputStream outputStream = new DataOutputStream(connectionSocket
.getOutputStream()); // 获取socket的输出流
FileInputStream input = new FileInputStream(this.getName());// 获取文件的输入流
int bufsize = 1024;
byte[] buf = new byte[bufsize + 1];// 读入缓存
outputStream.writeBytes(this.getName()+"\n");// 传送文件名 读取时候需要读取一行
outputStream.writeBytes("Filelen " + this.length() + "\n");// 传送文件长度 传输字符串 读取时候需要读取一行
long filelen = this.length();
System.out.println(filelen);
long count = 0;
/*大文件分为多次读取,每次读取1024,最后一次不足1024全部读取*/
while (count < filelen) {
long pre = count;
if (filelen - pre > bufsize) {
count += input.read(buf, 0, bufsize);
outputStream.write(buf, 0, bufsize);
} else {
count += input.read(buf, 0, (int) (filelen - pre));
outputStream.write(buf, 0, (int) (filelen - pre));
}
}
input.close();
outputStream.close();
}
}
服务器:
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception {
String ClientSentence;
@SuppressWarnings("unused")
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6669); // 建立tcp的等待端口
while (true) {
Socket connectionSocket = welcomeSocket.accept(); // tcp建立连接
try {
System.out.println("build ");
BufferedReader infromClient = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream())); // tcp的传输流
// 输入流读入流
DataOutputStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream()); // tcp的输出流 写流
System.out.println("establish");
ClientSentence = infromClient.readLine(); // 读取一行 遇到换行停止
System.out.println(ClientSentence);
String filename = "in.pdf";
ZFile sendFile = new ZFile("in.pdf");
sendFile.sendFile(connectionSocket);
FileInputStream input = new FileInputStream(sendFile);
outToClient.close();
infromClient.close();
connectionSocket.close();
} catch (Exception e) {
connectionSocket.close();
}
}
}
}
客户端
客户端一上来需要与服务器进行连接,接着获取输入流,输出流,然后紧接着利用输入流获取文件名,获取文件的长度,然后很多次获取文件内容
import java.io.*;
import java.net.*;
class TCPClient{
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader infromUser =
new BufferedReader(
new InputStreamReader(System.in));
Socket clientSocket = new Socket("127.0.0.1",6669);
try{
DataOutputStream outToServer =
new DataOutputStream(
clientSocket.getOutputStream());
BufferedReader infromServer =
new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
DataInputStream dataInput = new DataInputStream(clientSocket.getInputStream());
sentence = infromUser.readLine();
outToServer.writeBytes(sentence + '\n');
String rec ;
rec =infromServer.readLine();
String filename = rec;
System.out.println(rec);
rec = infromServer.readLine();
int fileLen = Integer.parseInt(rec.substring(8));
FileOutputStream fileWrite = new FileOutputStream(filename);
System.out.println(fileLen);
int bufsize = 1024;
byte[] buf = new byte[bufsize +1];
int n = -1;
int count = 0;
while(count<fileLen)
{
int pre = count;
if(fileLen - pre > bufsize)
{
count += bufsize;
dataInput.read(buf, 0,bufsize);
fileWrite.write(buf,0,bufsize);
System.out.println(new String(buf,0,bufsize));
}
else
{
count += dataInput.read(buf, 0,fileLen-pre);
count = fileLen;
fileWrite.write(buf,0,fileLen-pre);
System.out.println(new String(buf,0,fileLen - pre));
}
}
clientSocket.close();
}
catch(Exception e)
{
clientSocket.close();
}
}
}