接收端代码 GUI 端
sock=new Socket(IP,port);
os = new DataOutputStream(sock.getOutputStream());
if (sock != null && os != null ) {
os.writeUTF("21");
}
os.flush();
int bytesRead;
InputStream in=sock.getInputStream(); //socket closed..
Receiver receive =new Receiver(sock,in);
s=receive.flagvalue();
if(s==1)
JOptionPane.showMessageDialog(null, "File Transfer Done","INFORMATION",JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(this, "YOU CAN'T ACCESS THIS PORT","BLOCKED",JOptionPane.ERROR_MESSAGE);
}
//Receiver Class whom to class from RECEIVER GUI
public class Receiver implements Runnable {
public String receiverIP;
String IP;
Socket sock;
InputStream in;
public Receiver(Socket sock,InputStream in) {
this.sock=sock;
this.in=in;
Thread th=new Thread(this);
th.start();
}
@Override
public void run() {
try {
int bytesRead;
String filename="Files\\rahul.zip";
int current = 0;
while(true) {
if(in.available()>0) {
flag=1;
FileOutputStream fos = new FileOutputStream(filename);
BufferedOutputStream out =new BufferedOutputStream(fos);
byte[] buffer=new byte[6022386];
bytesRead = in.read(buffer, 0, buffer.length);
current = bytesRead;
do {
bytesRead =in.read(buffer, current, (buffer.length - current));
if (bytesRead >= 0) {
current += bytesRead;
}
} while (bytesRead > -1);
out.write(buffer, 0, current);
out.flush();
out.close();
in.close();
break;
}
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage()+" HEre");
}
}
public int flagvalue() {
return flag;
}
}
GUI 端的服务器端代码
CheckIP chkIP;
port=5001;
try {
chkIP=new CheckIP();
boolean start=true;
String line="";
server=new ServerSocket(port);
String receiverIP;
int pointer;
String finalip;
while(start) {
clientSocket=server.accept();
is1 = new DataInputStream(clientSocket.getInputStream());
line=is1.readUTF();
is1.close();
filename="Files\\"+line.trim()+".zip";
receiverIP=clientSocket.toString();
pointer=receiverIP.indexOf("/") ;
finalip=receiverIP.substring(pointer+1) ;
pointer=finalip.indexOf(",");
receiverIP=finalip.substring(0, pointer) ;
String check=chkIP.checkReceiver(receiverIP);
if(check.toUpperCase().equals("APPROVED")) {
System.out.println("filename,clientSocket"+filename+" "+clientSocket);
Server server1=new Server(filename,clientSocket);
Thread th=new Thread(server1);
th.start();
JOptionPane.showMessageDialog(null, "File Transfered","Process Done" , 1);
} else
JOptionPane.showMessageDialog(this, "USER BLOCKED","BLOCKED",JOptionPane.ERROR_MESSAGE);
}
服务器代码
public class Server implements Runnable {
Socket clientSocket;
String filename;
public Server(String filename,final Socket clientSocket) {
this.filename=filename;
this.clientSocket=clientSocket;
}
@Override
public void run() {
FileInputStream fis = null;
try {
System.out.println("filename "+filename);
File file=new File(filename);
fis = new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(fis);
byte[] buffer=new byte[(int)file.length()];
bis.read(buffer,0,buffer.length);
OutputStream os=clientSocket.getOutputStream();
os.write(buffer, 0, buffer.length);
os.flush();
os.close();
} catch (IOException ex) {
Logger.getLogger(FirewallServer.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(FirewallServer.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}
}
}
}
我想将文件名发送到服务器,并且只想接收指定的文件。但不知何故,接收方没有等待服务器发送文件。如何从接收方执行暂停条件,以便服务器可以选择该文件并将其发送给接收方。请帮忙?