使用套接字的Java文件传输代码中IP地址的客户端错误?

使用套接字的Java文件传输代码中IP地址的客户端错误?,java,socket.io,file-transfer,Java,Socket.io,File Transfer,几天前我开始学习Java套接字编程。我在一些网站上找到了这段代码,它使用线程池,通过多线程将文件从服务器传输到多个客户端 当我使用环回接口127.0.0.1运行此应用程序时,它工作正常,文件被传输到目标文件夹。但是当我将服务器pc 192.168.1.2连接到客户端pc 192.168.1.3时,会出现异常并显示 "Could not establish I/O for IP: <ipaddress of client> <port number>" 服务器端代码:4个

几天前我开始学习Java套接字编程。我在一些网站上找到了这段代码,它使用线程池,通过多线程将文件从服务器传输到多个客户端

当我使用环回接口127.0.0.1运行此应用程序时,它工作正常,文件被传输到目标文件夹。但是当我将服务器pc 192.168.1.2连接到客户端pc 192.168.1.3时,会出现异常并显示

"Could not establish I/O for IP: <ipaddress of client> <port number>"
服务器端代码:4个Java文件

    myfileserver.java // First File

    import javax.swing.JOptionPane;
    public class myfileserver {

    /**
     * @param args
     */
    public static void main(String[] args) {

        System.out.println("Server: Start");
        JOptionPane.showMessageDialog(null,"Server Started ","Success",JOptionPane.PLAIN_MESSAGE);
        Multithreading server = new Multithreading(4444);
        server.run();

    }
}

Multithreading.java // Second File

import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Multithreading implements Runnable{

private ServerSocket server;
private ExecutorService pool;

public Multithreading(int listenPort) {

    this.pool = Executors.newFixedThreadPool(50); //Max connections

    try {
        server = new ServerSocket(listenPort);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

 public void run() {
     try {

         while(true) {
            pool.execute(new WorkerThread(server.accept()));
         }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


}

ServerStatistics.java // Third File

public class ServerStatistics {


private static int ConnectionCounter;   //M
private static int FileCounter;         //N

public static synchronized  int getConnectionCounter(){
    return ConnectionCounter;
}

public static synchronized  int getFileCounter(){
    return FileCounter; 
}

public static synchronized  int IncConnectionCounter(){
    return ConnectionCounter++;
}

public static synchronized  int IncFileCounter(){
    return FileCounter++;   
}
}


WorkerThread.java // Fourth File

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JOptionPane;


public class WorkerThread extends Thread {

    public Socket connection;

    public  WorkerThread(Socket connection) {
        this.connection = connection;
        handleConnection();
    }

    private synchronized boolean  handleConnection() {
        try {
            //Increment Connection statistics 
            ServerStatistics.IncConnectionCounter();

            //Create read/write buffers for the socket
            PrintWriter streamWriter = new PrintWriter(connection.getOutputStream());
            BufferedReader  streamReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            //Get filename from client 
            String fileName =  streamReader.readLine();
            System.out.println("server> File " + fileName + " requested from " + connection.getInetAddress().getHostAddress());



            try{
                File myFile = new File (fileName); //Files Are in the Upload directory 

                if(myFile.exists()){
                    //File exists
                    ServerStatistics.IncFileCounter();

                    System.out.println("server> Successful, " + fileName + " exists");
                    System.out.println("server> Total successful requests so far = " + ServerStatistics.getFileCounter() + " out of " + ServerStatistics.getConnectionCounter());

                    //Send N and M
                    streamWriter.println(ServerStatistics.getConnectionCounter() + "," + ServerStatistics.getFileCounter());
                    streamWriter.flush();

                    //Create File buffer to read file to socket
                    byte [] mybytearray  = new byte [(int)myFile.length()];
                    FileInputStream fis = new FileInputStream(myFile);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    bis.read(mybytearray,0,mybytearray.length);
                    OutputStream os = connection.getOutputStream();

                    //Send File
                    os.write(mybytearray,0,mybytearray.length);
                    os.flush();
                    connection.close();

                    System.out.println("server> File transfer complete [" + fileName + "]");
                    JOptionPane.showMessageDialog(null,"File " + fileName + " requested from " + connection.getInetAddress().getHostAddress() +"\nSuccessful File exists!!" +"\nTotal successful requests so far = " + ServerStatistics.getFileCounter() + " out of " + ServerStatistics.getConnectionCounter() + "\nFile transfer complete [" + fileName + "]" ,"",JOptionPane.PLAIN_MESSAGE);

                }else{
                    //File does not exists
                    System.out.println("server> Not Successful, " + fileName + " does not exists");
                    System.out.println("server> Total successful requests so far = " + ServerStatistics.getFileCounter() + " out of " + ServerStatistics.getConnectionCounter());

                    //Send N and -1 b/c file does not exists
                    streamWriter.println(ServerStatistics.getConnectionCounter() + "," + (-1)); 
                    streamWriter.flush();
                }

                return true;
            }catch (Exception e){//Catch exception if any
                streamWriter.println(ServerStatistics.getConnectionCounter() + ",-1");
                streamWriter.flush();
                System.err.println("Error: " + e.getMessage());
                return false;
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }
}

a.以下方面的明显变化:

String serverAddress= "127.0.0.1";
    int serverPort=  4444;


b我会开始检查192.168.1.2,你可以在你的服务器运行时做一个netstat-an,并验证它正在监听正确的端口,我还会在服务器中获取一个数据包捕获,以查看TCP请求是否来自客户端。

谢谢@spicyramen。我会再试一次,因为我以前做过,但有相同的错误。让你知道如果它这次工作。Soomeone请帮助!!!
String serverAddress= "127.0.0.1";
    int serverPort=  4444;
String serverAddress= "192.168.1.2";
    int serverPort=  4444;