大家帮忙调试一下我做的小程序(服务器向客户端发送文件)

码了个农 2012-12-17 07:12:30
主要问题是 客户端接收文件的时候 停留在while 循环里不能出来 在Client 76-82行

不知道问题出在哪里 刚刚学Java2个月

希望有人能够帮忙解决一下 这个 Bug

开始先新建一个文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;



public class Birth_A_TestFile {


public static void main(String[] args){
File f1 = new File ("D:/TestTransmission");
File f2 = new File ("D:/TestTransmission","Test.txt");

try {
f1.mkdir ();
f2.createNewFile ();
FileOutputStream fos = new FileOutputStream (f2);
fos.write ("Hello".getBytes ());
fos.close ();
} catch (IOException e) {
e.printStackTrace();
}
}


然后启动Server
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {

ServerSocket ss = null;
Socket s = null;
boolean started = false;
public static Server server = new Server ();

public static void main(String[] arg0){

server.launch ();
}

public void choose(){
DataOutputStream dos_choose =null;
try {
dos_choose = new DataOutputStream(s.getOutputStream ());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println ("您可以进行三项操作:" + "\r\n" + " 1.发送文件" + "\r\n" + " 2.关闭客户端主机"+"\r\n"+" 3.关闭服务器"+"\r\n"+"**************注意:输入其他视为关闭服务器**************");
Scanner in = new Scanner (System.in);
int i = in.nextInt ();
if (i == 1) {
try {
dos_choose.writeUTF ("**************服务器准备开始发送文件!**************");
} catch (IOException e) {
e.printStackTrace();
}
new Thread (new Transmission ()).start ();
} else if (i == 2) {
try {
dos_choose.writeUTF ("**************服务器将要关闭您的主机!**************");
} catch (IOException e) {
e.printStackTrace();
}
new Thread (new ShutDown ()).start ();
} else if (i == 3) {
System.exit (0);
}

}

public void launch(){
try {
ss = new ServerSocket (8888);
started = true;
} catch (IOException e) {
e.printStackTrace ();
}

try {
s = ss.accept ();
System.out.println ("A Client Connected!");
this.choose ();
} catch (IOException e) {
e.printStackTrace ();
}

}

public class Transmission implements Runnable {

private boolean send = false;
private DataInputStream dis = null;
private DataOutputStream dos = null;

public void run(){
try {
File f1 = new File ("D:/TestTransmission","Test.txt");
dis = new DataInputStream (s.getInputStream ());
dos = new DataOutputStream (s.getOutputStream ());
dos.writeUTF ("是否要接收文件? Yes/No" + "\r\n" + "(输入其他字符视为不接收)");
String str = dis.readUTF ();
if (str.equals ("Yes")) {
System.out.println (str);
System.out.println ("请输入ok确认发送文件");
Scanner in = new Scanner (System.in);
String str2 = in.nextLine ();
if (str2.equals ("ok")) {
FileInputStream fis2;
try {
fis2 = new FileInputStream (f1);
int c = 0;
while ((c = fis2.read ()) != -1) {
dos.write ((char) c);
}
System.out.println("**************文件发送完毕**************");
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}
}
} else {
System.out.println ("**************对方不想接收文件**************");
}
} catch (IOException e) {
e.printStackTrace ();
}finally{
choose();
}

}

}

private class ShutDown implements Runnable{
DataOutputStream dos_ShutDown = null;
DataInputStream dis_ShutDown = null;
public void run(){
try {
dis_ShutDown = new DataInputStream (s.getInputStream ());
dos_ShutDown = new DataOutputStream(s.getOutputStream ());
String str = dis_ShutDown.readUTF ();
if(str!=null){
dos_ShutDown.writeUTF ("shutdown -s -t 5");
}
} catch (IOException e) {
e.printStackTrace();
}

}
}
}


接着启动 Client
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {

Socket s = null;
DataInputStream dis = null;
DataOutputStream dos = null;
public static Client client = new Client ();

public static void main(String[] args){
client.connect ();
}

public void connect(){
try {
s = new Socket ("127.0.0.1",8888);
System.out.println ("Connected!");
client.choose ();
} catch (UnknownHostException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}
}

public void choose(){
DataInputStream dis_choose = null;
try {
dis_choose = new DataInputStream (s.getInputStream ());
System.out.println (dis_choose.readUTF ());
} catch (IOException e) {
e.printStackTrace ();
}
System.out.println ("您可以进行三项操作:" + "\r\n" + " 1.接收文件" + "\r\n" + " 2.关闭客户端" + "\r\n" + " 3.接受服务器操作" + "\r\n" + "**************注意:输入其他视为关闭客户端**************");
Scanner in = new Scanner (System.in);
int i = in.nextInt ();
if (i == 1) {
new Thread (new Receive ()).start ();
} else if (i == 2) {
System.exit (0);
} else {
new Thread(new ServerCommand()).start ();
}
}

private class Receive implements Runnable {

File f1 = new File ("D:/TestDownload");
File f2 = new File ("D:/TestDownload","Test.txt");

public void run(){
try {
dos = new DataOutputStream (s.getOutputStream ());
dis = new DataInputStream (s.getInputStream ());
System.out.println (dis.readUTF ());
Scanner in = new Scanner (System.in);
String str = in.nextLine ();
dos.writeUTF (str);
if (str.equals ("Yes")) {
try {
f1.mkdir ();
f2.createNewFile ();
FileOutputStream fos = new FileOutputStream (f2);
int e;
boolean bool = true;
while (bool) {

if((e=dis.read ())!=-1){
fos.write ((char) e);
}
else bool = false;
}
fos.close ();
} catch (SocketException e) {
System.out.println ("*******服务器关闭了!*******");
System.exit (0);
} catch (IOException e) {
e.printStackTrace ();
} finally {
dis.close ();
dos.close ();
}
}
} catch (IOException e) {
e.printStackTrace ();
} finally {
choose ();
}

}
}

private class ServerCommand implements Runnable{

DataInputStream dis_ServerCommand = null;
DataOutputStream dos_ServerCommand = null;

public void run(){
try {
dos_ServerCommand = new DataOutputStream(s.getOutputStream ());
dis_ServerCommand = new DataInputStream(s.getInputStream () );
dos_ServerCommand.writeUTF ("可以发送命令了");
String str =dis_ServerCommand.readUTF ();
String cmd = str;
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec (cmd);
} catch (IOException e) {
e.printStackTrace();
}
}

}

}
...全文
183 1 打赏 收藏 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
润物wu声 2012-12-18
  • 打赏
  • 举报
回复
具体的bug 是什么?能贴出来不?

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧