Java 套接字程序在客户端上卡住了

Java 套接字程序在客户端上卡住了,java,sockets,client,server,Java,Sockets,Client,Server,客户机/服务器程序,其中客户机向服务器发送文本文件。服务器获取文件,读取文件并计算数字、字符、空格等的数量,然后发送回客户端 如果将文件发送到服务器,或者让服务器在计数后将字符串发送到客户机,但不是同时发送,则该程序工作得非常好 客户端 import java.io.*; import java.net.*; import java.util.Scanner; class TCPClient { public static void main(String argv[]) throws

客户机/服务器程序,其中客户机向服务器发送文本文件。服务器获取文件,读取文件并计算数字、字符、空格等的数量,然后发送回客户端

如果将文件发送到服务器,或者让服务器在计数后将字符串发送到客户机,但不是同时发送,则该程序工作得非常好

客户端

import java.io.*;
import java.net.*;
import java.util.Scanner;
class TCPClient 
{
    public static void main(String argv[]) throws Exception
    {



System.out.println("I am client");


        Socket clientSocket = new Socket("localhost", 9881); 


        File file = new File("D:\\test.txt");
        byte[] bytearray = new byte[(int) file.length()];

        FileInputStream fis = new FileInputStream(file);

        BufferedInputStream bis = new BufferedInputStream(fis);

        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        BufferedOutputStream outToServer = new BufferedOutputStream(clientSocket.getOutputStream());


        bis.read(bytearray, 0, bytearray.length);
        outToServer.write(bytearray, 0, bytearray.length);
        outToServer.flush();


        System.out.println("From server: " + '\n');
        String x;

        while((x = inFromServer.readLine()) != null)
        {
            System.out.println(x);
        }



        clientSocket.close();


    }
}
服务器

class TCPServer 
{
    //***** Functions for counting *****
        public static int number(String x)
        {
            int numCount = 0;
            char temp;

            for( int i=0; i <x.length(); i++)
            {
                temp = x.charAt(i);
                if(Character.isDigit(temp))
                {
                    numCount++;
                }
            }
            return numCount;
        }

        public static int character(String x)
        {
            int charCount = 0;
            char temp;

            for( int i=0; i <x.length(); i++)
            {
                temp = x.charAt(i);
                if(Character.isLetter(temp))
                {
                    charCount++;
                }
            }
            return charCount;
        }

        public static int space(String x)
        {
            int spaceCount = 0;
            char temp;

            for (int i=0; i <x.length(); i++)
            {
                temp = x.charAt(i);
                if(temp==' ')
                {
                    spaceCount++;
                }
            }
            return spaceCount;
        }

        public static int special(String x)
        {
            int specialCount = 0;
            char temp;

            for (int i=0; i <x.length(); i++)
            {
                temp = x.charAt(i);
                if(temp=='!'||temp=='@'||temp=='#'||temp=='$'||temp=='%'||temp=='&'||temp==':'||temp=='^'||temp=='*')
                {
                    specialCount++;
                }   
            }
            return specialCount;
        }

        //***************************************

    public static void main(String argv[]) throws Exception
    {
        System.out.println("I am Server");

        ServerSocket welcomeSocket = new ServerSocket(9881);
        System.out.println("After Welcome");
        while(true) 
        {

            Socket connectionSocket = welcomeSocket.accept();
            System.out.println("After Accept");

            PrintWriter outToClient = new PrintWriter(connectionSocket.getOutputStream());



            byte[] aByte = new byte [1];
            int bytesRead;


            InputStream is = connectionSocket.getInputStream();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            FileOutputStream fos = new FileOutputStream ("D:\\server.txt");
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            bytesRead = is.read(aByte, 0, aByte.length);

            while (bytesRead != -1)
            {
                baos.write(aByte);
                bytesRead = is.read(aByte);
            }

            bos.write(baos.toByteArray());
            bos.flush();


            BufferedReader br = null;

            try
            {
                String sCurrentLine;

                br = new BufferedReader(new FileReader("D:\\server.txt"));

                while ((sCurrentLine = br.readLine()) != null) 
                {
                    System.out.println(sCurrentLine);
                }
            }

            catch (IOException e) 
            {
                e.printStackTrace();
            }
            finally 
            {
                try 
                {
                    if (br != null)br.close();
                } 

                catch (IOException ex)
                {
                    ex.printStackTrace();
                }
            }

            /*******Functions********/

                int numCount = 0;
                Scanner sc = new Scanner(new FileReader("D:\\server.txt"));
                while (sc.hasNextLine())
                {
                    String line;
                    line = sc.nextLine();
                                    numCount += number(line);
                }

                int charCount = 0;
                Scanner sc2 = new Scanner(new FileReader("D:\\server.txt"));
                while (sc2.hasNextLine())
                {
                    String line;
                    line = sc2.nextLine();
                    charCount += character(line);
                }

                int spaceCount = 0;
                Scanner sc3 = new Scanner(new FileReader("D:\\server.txt"));
                while(sc3.hasNextLine())
                {
                    String line;
                    line = sc3.nextLine();
                    spaceCount += space(line);

                }

                int specialCount = 0;
                Scanner sc4 = new Scanner(new FileReader("D:\\server.txt"));
                while(sc4.hasNextLine())
                {
                    String line;
                    line = sc4.nextLine();
                    specialCount += special(line);

                }

                int lineCount = 0;
                Scanner sc5 = new Scanner(new FileReader("D:\\server.txt"));
                while(sc5.hasNextLine())
                {
                    String line;
                    line = sc5.nextLine();
                    if(!"".equals(line.trim()))
                    {
                        lineCount++;
                    }
                }

                //System.out.println(numCount);
                //System.out.println(charCount);
                //System.out.println(spaceCount);
                //System.out.println(specialCount);
                //System.out.println(lineCount);
                String x="Numbers: " + numCount + "\nCharacters: " + charCount + "\nSpaces: " + spaceCount + "\nSpecials: " + specialCount + "\nLines: " + lineCount + '\n';
                //System.out.println(x);
                outToClient.println(x + '\n');
                outToClient.flush();
                outToClient.close();
                bos.close();





            /***********************/


            connectionSocket.close();
        }

    }
}
如果我的代码凌乱,我很抱歉,这是我第一次使用Java。所以我希望你们能帮助我,记住我对Java知之甚少

感谢您抽出时间。

尝试使用“相同”的阅读和写作方法。示例BufferedReader/BufferedWriter代替PrintWriter
I am client
From server: