0

受信側コード 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();
            }
        }

    }
}

ファイル名をサーバーに送信し、その指定されたファイルのみを受信したい。しかし、どういうわけか、受信者はサーバーがファイルを送信するのを待っていません。サーバーがそのファイルを選択して受信者に送信できるように、受信者側から一時停止条件を実行する方法。助けてください?

4

1 に答える 1

0

サーバーの応答を待つレシーバーコードは次のとおりです クライアントはサーバーの応答を待ちます

于 2013-02-26T11:01:05.397 に答える