我的程序在localhost上运行完全,但在两台机器上实现时会出错

时间:2016-11-03 15:52:51

标签: java client-server

在客户端和服务器项目上,每个客户端和服务器相互发送文件。程序目标是在两端发送和接收文件。 该程序在本地主机上运行非常完美,但是当在两台机器上实现避免指定服务器机器IP时会带来错误,但它仍会带来错误。我只是想知道阻碍程序在机器上运行的可能原因是什么。

服务器端

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JOptionPane;

class ServerHandler {
    PrintStream printStream;

    public ServerHandler(){

    }
    public static void main(String [] args){
        new ServerHandler().sentIT("Super_woman.avi");
    }

    public void sentIT(String pathName) {

        try {
            ServerSocket server = new ServerSocket(65141);
            System.out.println("Server Started");
            Socket socket = server.accept();
            System.out.println(" "+socket.getLocalAddress());
            int BUFFER_SIZE = 1024 * 50;
            byte[] buffer;
            buffer = new byte[BUFFER_SIZE];
            File paths = new File(pathName);
            BufferedInputStream input = 
                new BufferedInputStream(new FileInputStream(paths));
            DataOutputStream ou = new DataOutputStream(socket.getOutputStream());
            try (BufferedOutputStream out = new BufferedOutputStream(ou)) {
                ou.writeUTF( paths.getName());
                int len, i=0;
                while ((len = input.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
                out.flush();
                out.close();
                input.close();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Error sending File/n FAILED", "Sending Failed",
                JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

客户端

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JOptionPane;

public class ClientReceive{
    private DataInputStream ins;
    private BufferedInputStream bis;
    private BufferedOutputStream out;

    public ClientReceive(){

    }
    public static void main(String [] args){
        new ClientReceive().receiveFile();
    }

    public void receiveFile() {

        try {
            int BUFFER_SIZE = 1024 * 50;
            byte[] buffer;
            buffer = new byte[BUFFER_SIZE];
            Socket socket = new Socket("localhost", 65141);
            ins = new DataInputStream(socket.getInputStream());
            String path=ins.readUTF();
            bis = new BufferedInputStream(ins);
            out = new BufferedOutputStream(new FileOutputStream("HotSpotFileShare_"+path));
            int len;
            while ((len = bis.read(buffer)) > 0) {
                out.write(buffer, 0, len);   
            }  
            out.flush();
            JOptionPane.showMessageDialog(null, "File "+path+" Received Successfully", 
                    "File Received", JOptionPane.INFORMATION_MESSAGE);
            //System.out.println("\nDone!");    
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Operation Failed", "Error", JOptionPane.ERROR_MESSAGE);
        }
        finally {
            if (ins != null && out != null){

                try {
                    out.close();
                    ins.close();

                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null, "Unable to Close Connection", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

在客户端,你有这个:

Socket socket = new Socket("localhost", 65141);

但您不想连接到localhost,因此请输入运行服务器的计算机的IP地址而不是localhost

这将起作用如果它们在同一网络(LAN,即)。 要获取服务器计算机的IP(在Windows上),您可以打开CMD(Ctrl + R - >键入cmd.exe并按Enter键)并输入CMD:

ipconfig

行中的数字序列IPv4...是您的IP地址