利用Java编写一个程序利用Socket和ServerSocket类,实现网络的点对点文件传输
2013-09-15
展开全部
服务器端代码,支持多用户访问,端口号9001:
package net;
import java.net.*;
import java.io.*;
public class Server extends Thread {
private ServerSocket serverSocket;
private Socket serverEndPoint;
private InputStream in;
private OutputStream out;
public Server() {
}
private Server(Socket serverEndPoint) {
this.serverEndPoint = serverEndPoint;
}
public void startService(int port) throws IOException, InterruptedException {
System.out.println("service started...");
serverSocket = new ServerSocket(port);
while (true)
new Server(serverSocket.accept()).start();
}
public void run() {
try {
in = serverEndPoint.getInputStream();
out = serverEndPoint.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
String request;
while (true) {
if ((request = dis.readUTF()) != null) {
if (request.trim().equals("bye")
|| request.trim().equals("exit")
|| request.trim().equals("quit"))
break;
System.out.println(serverEndPoint.getInetAddress()
.getCanonicalHostName()
+ " is getting '" + request.trim() + "'...");
File f = new File(request.trim());
if (!f.exists()) {
dos.writeBoolean(false);
dos.writeUTF("File '" + f.getPath()
+ "' doesn't exist!");
continue;
}
if (f.isDirectory()) {
dos.writeBoolean(false);
dos.writeUTF("'" + f.getPath() + "' is a folder!");
continue;
}
FileInputStream fis = new FileInputStream(f);
int i;
int count = 0;
byte[] buf = new byte[1024];
dos.writeBoolean(true);
dos.writeLong(f.length());
while ((i = fis.read(buf)) != -1) {
dos.write(buf, 0, i);
count += i;
}
fis.close();
Thread.sleep(1000);
dos.writeUTF("File '" + f.getName()
+ "' has been saved in the current folder.");
}
}
System.out.println("Connection to "
+ serverEndPoint.getInetAddress().getCanonicalHostName()
+ " is ready to close...");
Thread.sleep(5000);
dis.close();
dos.close();
serverEndPoint.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
new Server().startService(90011);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package net;
import java.net.*;
import java.io.*;
public class Server extends Thread {
private ServerSocket serverSocket;
private Socket serverEndPoint;
private InputStream in;
private OutputStream out;
public Server() {
}
private Server(Socket serverEndPoint) {
this.serverEndPoint = serverEndPoint;
}
public void startService(int port) throws IOException, InterruptedException {
System.out.println("service started...");
serverSocket = new ServerSocket(port);
while (true)
new Server(serverSocket.accept()).start();
}
public void run() {
try {
in = serverEndPoint.getInputStream();
out = serverEndPoint.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
String request;
while (true) {
if ((request = dis.readUTF()) != null) {
if (request.trim().equals("bye")
|| request.trim().equals("exit")
|| request.trim().equals("quit"))
break;
System.out.println(serverEndPoint.getInetAddress()
.getCanonicalHostName()
+ " is getting '" + request.trim() + "'...");
File f = new File(request.trim());
if (!f.exists()) {
dos.writeBoolean(false);
dos.writeUTF("File '" + f.getPath()
+ "' doesn't exist!");
continue;
}
if (f.isDirectory()) {
dos.writeBoolean(false);
dos.writeUTF("'" + f.getPath() + "' is a folder!");
continue;
}
FileInputStream fis = new FileInputStream(f);
int i;
int count = 0;
byte[] buf = new byte[1024];
dos.writeBoolean(true);
dos.writeLong(f.length());
while ((i = fis.read(buf)) != -1) {
dos.write(buf, 0, i);
count += i;
}
fis.close();
Thread.sleep(1000);
dos.writeUTF("File '" + f.getName()
+ "' has been saved in the current folder.");
}
}
System.out.println("Connection to "
+ serverEndPoint.getInetAddress().getCanonicalHostName()
+ " is ready to close...");
Thread.sleep(5000);
dis.close();
dos.close();
serverEndPoint.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
new Server().startService(90011);
} catch (Exception e) {
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-15
展开全部
客户端代码:
package net;
import java.net.*;
import java.io.*;
public class Client {
private Socket clientEndPoint;
private InputStream in;
private OutputStream out;
public void connectToService() throws IOException, InterruptedException {
clientEndPoint = new Socket("127.0.0.1", 9001);//根据实际情况修改此处地址,本地测试默认为“127.0.0.1”
in = clientEndPoint.getInputStream();
out = clientEndPoint.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Server is ready. Please type in file to get: ");
String request;
while (true) {
System.out.print("Get: ");
request = br.readLine() + "\r\n";
dos.writeUTF(request);
//输入“bye”、“exit”“quit”则退出
if (request.trim().equals("bye") || request.trim().equals("exit")
|| request.trim().equals("quit"))
break;
while (true) {
if (in != null) {
System.out.println("Getting file(s)...");
int i;
long count = 0;
boolean fileExist = dis.readBoolean();
if (fileExist) {
File f = getTargetFile(request.trim());
FileOutputStream fos = new FileOutputStream(f);
long fileLength = dis.readLong();
byte[] buf = new byte[1024];
System.out.println(fileLength);
while (true) {
i = dis.read(buf, 0, 1024);
fos.write(buf, 0, i);
count += i;
if (count >= fileLength) {
break;
}
}
fos.close();
}
break;
}
}
System.out.println(dis.readUTF());
}
System.out.println("Client ready to close...");
Thread.sleep(5000);
dis.close();
dos.close();
clientEndPoint.close();
}
private File getTargetFile(String path) {
path = path.replace("/", "\\");
path = path.substring(path.lastIndexOf("\\") + 1);
return new File(path);
}
public static void main(String[] args) {
try {
new Client().connectToService();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package net;
import java.net.*;
import java.io.*;
public class Client {
private Socket clientEndPoint;
private InputStream in;
private OutputStream out;
public void connectToService() throws IOException, InterruptedException {
clientEndPoint = new Socket("127.0.0.1", 9001);//根据实际情况修改此处地址,本地测试默认为“127.0.0.1”
in = clientEndPoint.getInputStream();
out = clientEndPoint.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Server is ready. Please type in file to get: ");
String request;
while (true) {
System.out.print("Get: ");
request = br.readLine() + "\r\n";
dos.writeUTF(request);
//输入“bye”、“exit”“quit”则退出
if (request.trim().equals("bye") || request.trim().equals("exit")
|| request.trim().equals("quit"))
break;
while (true) {
if (in != null) {
System.out.println("Getting file(s)...");
int i;
long count = 0;
boolean fileExist = dis.readBoolean();
if (fileExist) {
File f = getTargetFile(request.trim());
FileOutputStream fos = new FileOutputStream(f);
long fileLength = dis.readLong();
byte[] buf = new byte[1024];
System.out.println(fileLength);
while (true) {
i = dis.read(buf, 0, 1024);
fos.write(buf, 0, i);
count += i;
if (count >= fileLength) {
break;
}
}
fos.close();
}
break;
}
}
System.out.println(dis.readUTF());
}
System.out.println("Client ready to close...");
Thread.sleep(5000);
dis.close();
dos.close();
clientEndPoint.close();
}
private File getTargetFile(String path) {
path = path.replace("/", "\\");
path = path.substring(path.lastIndexOf("\\") + 1);
return new File(path);
}
public static void main(String[] args) {
try {
new Client().connectToService();
} catch (Exception e) {
e.printStackTrace();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询