找到你要的答案

Q:Socket program gets stuck on client

Q:套接字程序在客户端卡住

A client/server program where the client sends a text file to server. The server takes the file, reads it and counts number of numbers, characters, spaces, etc, and sends back to the client.

The program works great when either send the file to the server, or make the server send string to the client after counting, but not at the same time.

Client

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();


    }
}

Server

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();
        }

    }
}

The output on the Client side would be

I am client
From server: 

I apologies if my code is messy, this is my first time with Java. So I hope you guys can help me out keeping in mind I don't know much about Java.

Thank you for your time.

客户端/服务器程序,客户端将文本文件发送到服务器。服务器取出文件,读取并计数数字、字符、空格等,并返回给客户端。

当将文件发送到服务器时,或者使服务器在计算后发送字符串给客户端,但不同时。

顾客

import java.io.*;
import java.net.*;
import java.util.Scanner;
class TCP顾客 
{
    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 inFrom服务器 = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        BufferedOutputStream outTo服务器 = new BufferedOutputStream(clientSocket.getOutputStream());


        bis.read(bytearray, 0, bytearray.length);
        outTo服务器.write(bytearray, 0, bytearray.length);
        outTo服务器.flush();


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

        while((x = inFrom服务器.readLine()) != null)
        {
            System.out.println(x);
        }



        clientSocket.close();


    }
}

服务器

class TCP服务器 
{
    //***** 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 服务器");

        服务器Socket welcomeSocket = new 服务器Socket(9881);
        System.out.println("After Welcome");
        while(true) 
        {

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

            PrintWriter outTo顾客 = 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);
                outTo顾客.println(x + '\n');
                outTo顾客.flush();
                outTo顾客.close();
                bos.close();





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


            connectionSocket.close();
        }

    }
}

The output on the 顾客 side would be

I am client
From server: 

我道歉,如果我的代码是混乱的,这是我第一次用java。所以我希望你们能帮助我记住我不知道很多关于java。

谢谢,占您时间了。

java  sockets  client  server