(Java)TCP客户端不';无法连接到服务器

(Java)TCP客户端不';无法连接到服务器,java,network-programming,tcpclient,tcpserver,connectexception,Java,Network Programming,Tcpclient,Tcpserver,Connectexception,我正在从事一个项目,该项目将在服务器和客户端之间发送一个文件100次,并使用到特定IP的非持久连接(它不能是localhost或类似的东西)。无论出于何种原因,当我尝试运行客户机程序时,它会超时,并出现“java.net.ConnectException:Connection timed out:connect”错误。它发生在第33行。我不确定我是否使用了一个不起作用的IP或者什么,也不确定我将如何找到一个起作用的IP。寻找任何潜在的编码错误的帮助,这可能是一个问题,因为我最近刚刚开始学习这个主

我正在从事一个项目,该项目将在服务器和客户端之间发送一个文件100次,并使用到特定IP的非持久连接(它不能是localhost或类似的东西)。无论出于何种原因,当我尝试运行客户机程序时,它会超时,并出现“java.net.ConnectException:Connection timed out:connect”错误。它发生在第33行。我不确定我是否使用了一个不起作用的IP或者什么,也不确定我将如何找到一个起作用的IP。寻找任何潜在的编码错误的帮助,这可能是一个问题,因为我最近刚刚开始学习这个主题

客户端代码:

import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;


public class TCPClient
{
   public static void main(String argv[]) throws Exception
   {

   String fileName = "Test1.txt"; //the file that will be sent. Must be in the same folder as this program.

   System.out.println("Looking for " + fileName + " file....");
   System.out.println("\n-----------------------\n");

   File clientFile = new File(fileName); //creating the file variable

   long totalTime = 0; //variable for the total time

   //this loops sends the file 100 times
   for (int i=0; i<100; i++)
   {
       long fileTime = 0;

       long startTime = System.currentTimeMillis(); //variable for the start of the transmission

       Scanner clientFileReader = new Scanner(clientFile); //creates a scanner for the file being sent

       //creates the client socket to connect to the server socket
       Socket clientSocket = new Socket("192.168.1.10", 5005); //IP address and host
       //displays the IP address that is being used
       System.out.println("I am connecting to the Server side: " + clientSocket);

       System.out.println("\nSending " + clientFile + " file for the " + (i+1) + "th time");

       System.out.println("File transmission start time: " + startTime); //displays the start time of file transmission

       //creates output stream that will be used to send data to the server
       DataOutputStream toServer = new DataOutputStream(clientSocket.getOutputStream());

       //reads in the file and replaces the "\n" dropped by the nextLine() function.
       while(clientFileReader.hasNextLine())
       {
           toServer.writeBytes(clientFileReader.nextLine() + "\n");
       }

       clientFileReader.close(); //closes the file

       clientSocket.close(); //closes the client socket

       long endTime = System.currentTimeMillis(); //variable for the end of the file transmission

       System.out.println("File transmission end time: " + endTime);

       fileTime = endTime - startTime; //the time to send the file

       System.out.println("The file took " + fileTime + " milliseconds");

       totalTime += fileTime; //the total time to send all of the files

       System.out.println("The " + clientFile + " file sent " + (i+1) + " times");

       System.out.println("\n-----------------------\n");
   }
   System.out.println("The total time to send the file 100 times: " + totalTime + " milliseconds"); //displays the total time
   System.out.println("The average time to send the file: " + totalTime/100 + "." + totalTime%100 + " milliseconds"); //displays the average
   System.out.println("I am done");
   }
}
import java.io.*;
导入java.net。*;
导入java.util.Scanner;
导入java.io.File;
导入java.io.InputStream;
导入java.io.OutputStream;
公共类TCPClient
{
公共静态void main(字符串argv[])引发异常
{
String fileName=“Test1.txt”;//将要发送的文件。必须与此程序位于同一文件夹中。
System.out.println(“查找”+fileName+“文件…”);
System.out.println(“\n-------------------\n”);
File clientFile=新文件(fileName);//创建文件变量
long totalTime=0;//总时间的变量
//这将循环发送文件100次
对于(int i=0;i
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.io.File;
import java.io.InputStream;

public class TCPServer
{
   public static void main(String argv[]) throws Exception
   {

   String clientSentence; //variable for each sentence in the file from the client.

   Socket connectionSocket; //Socket variable

   BufferedReader fromClient; //Buffered Reader variable

   long totalTime = 0; //variable for the total time it takes to receive the files

   String comparedFile = "Test1.txt"; //the file that will be used for this program

   String outputFileName = "receivedFile"; //the name of the copies that will be made from the text file

   ServerSocket serSock = new ServerSocket(5005); //opens up the server socket so the client can connect

   System.out.println("I am waiting for the connection from the Client Side....");

   //this loop makes the server listen for 100 files from the client side.
   for(int i=0; i<100; i++)
   {
       long fileTime = 0;
       long startTime = System.currentTimeMillis();

       //this makes the server wait till the client tries to connect then accepts the connection
       connectionSocket = serSock.accept();

       System.out.println("I am starting to receive the " + comparedFile + " file for the " + (i+1) + "th time.");

       //this creates the stream that will be used to receive the file.
       fromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

       //creates a file that the received file will be written to
       PrintWriter out = new PrintWriter(outputFileName+(i+1)+ ".txt");

       int lineCounter = 0; //variable to keep track of the number of lines.

       //this loop checks to see if the client's file has ended and it prints how many lines have been received.
       while(true)
       {
           clientSentence = fromClient.readLine();

           if(clientSentence == null) break;

           out.println(clientSentence);
           lineCounter++;
           System.out.println("I have received: " + lineCounter + " lines");
       }

       //closes the file
       out.close();

       //closes the server side of the client connection
       connectionSocket.close();

       long endTime = System.currentTimeMillis();

       fileTime = endTime - startTime;

       System.out.println("The file took " + Math.abs(fileTime) + " milliseconds to be received.");

       totalTime += fileTime;

       System.out.println("I am finishing receiving the " + comparedFile + " file for the " + (i+1) + "th time.");
       System.out.println("\n-----------------------\n");
    }
    System.out.println("I am done receiving files.");

    //closes the server socket.
    serSock.close();

    int failCount = 0;

    File serverFile = new File(comparedFile); //the file in the server side folder.

    //this loop reads the server file and the files received from the client to compare them and find any errors
   for(int i=0; i<100; i++)
   {
       File clientFile = new File(outputFileName + (i+1) + ".txt");
       Scanner serverFileReader = new Scanner(serverFile);
       Scanner clientFileReader = new Scanner(clientFile);

       boolean passedTheTest;
       while(serverFileReader.hasNextLine() && clientFileReader.hasNextLine())
       {
           passedTheTest = serverFileReader.nextLine().equals(clientFileReader.nextLine());

           //prints out any error found.
           if(!(passedTheTest))
       {
           System.out.println("File " + outputFileName+(i+1)+ ".txt has error");
           failCount++;
           break;
        }
    }
    serverFileReader.close();
    clientFileReader.close();
}
System.out.println("The failure rate is " + failCount + "/100"); //the failure rate when comparing the files
System.out.println("The average time to receive the file: " + (totalTime/100) + " milliseconds"); //the average time it took to receive a file.
System.out.println("I am done");
  }
}