编程服务器和客户端发送和接收字节数组

时间:2017-12-18 21:08:01

标签: java arrays sockets client datainputstream

以下是我的两个相互连接的程序:

  1. SERVER

    package authenticateddns;
    
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ThreadPoolExecutor;
    
    public class Server {
        public static final int PORT = 4444;
        int clientNumber;
        static String[][] url = new String[8][2];
    
        public static void main(String args[]) throws IOException {
            url[0][0] = "www.google.com";
            url[0][1] = "172.217.11.174";
            url[1][0] = "www.facebook.com";
            url[1][1] = "31.13.77.36";
            url[2][0] = "www.youtube.com";
            url[2][1] = "74.125.65.91";
            url[3][0] = "www.wikipedia.org";
            url[3][1] = "91.198.174.192";
            url[4][0] = "www.twitter.com";
            url[4][1] = "199.59.149.230";
            url[5][0] = "www.amazon.com";
            url[5][1] = "72.21.211.176";
            url[6][0] = "www.yahoo.com";
            url[6][1] = "98.137.149.56";
            url[7][0] = "www.abc.com";
            url[7][1] = "199.181.132.250";
    
            new Server().runServer();
        }
    
        public void runServer() throws IOException {
            ServerSocket serverSocket = new ServerSocket(PORT);
            System.out.println("Server up and ready for connection...");
            while (true) {
                ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newCachedThreadPool();
                Socket socket = serverSocket.accept();
                pool.submit(() -> new ServerThread(socket, clientNumber, url).start());
                System.out.println("New client has joined");
                clientNumber++;
            }
        }
    }
    
  2. SERVERTHREAD

    package authenticateddns;
    
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    public class ServerThread extends Thread {
    
        Socket socket;
        String clientName;
        int clientN;
        String url[][];
    
        byte[] message = new byte[4];
    
        ServerThread(Socket socket, int clientNumber, String[][] url)
        {
            this.socket = socket;
            clientN = clientNumber;
            this.url=url;
            message[0]=1;
            message[1]=2;
            message[2]=3;
            message[3]=4;
        }
    
        public void run()
        {
            try
            {
                String userURL;
                String ip = "";
                clientName = "Client" + clientN;
    
                //this is from the server
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    
                //this is from the client
                PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
    
                DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
                //dout.writeInt(message.length);
                //dout.write(message);
                //this reads in from client
                while(true)
                {
                    if((userURL = bufferedReader.readLine()) != null)
                    {
                        userURL = userURL.replace("\n", "").replace("\r", "");
    
                        for(int i = 0; i < 8; i++)
                        {
                            if(url[i][0].equals(userURL))
                            {
                                ip = url[i][1];
                                break;
                            }
                            if(!url[i][0].equals(userURL)&&i==7)
                            {
                                ip = "IP Address not found";
                            }
                        }
    
                        //System.out.println(clientName);
                        System.out.println("    " + clientName + ": Desired URL: " + userURL);
                        System.out.println("    " + clientName + ": Sending IP: " + ip);
                        System.out.println("");
    
                        //send this to client
                        printWriter.println("Server Recieved Message");
                        printWriter.println(ip);
                        dout.write(message);
                        System.out.println("worked");
                    }
    
                    //socket.close();    
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    
    }
    
  3. 然后你有客户计划

    package client;
    
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.InetAddress;
    
    public class Client {
    
        public static void main(String[] args) throws IOException
        {
            //InetAddress localIP;
            //localIP = InetAddress.getLocalHost();
            //Socket socket = new Socket("10.0.36.89", 4444);
            Socket socket = new Socket("10.1.43.10", 4444);
            String ip;
            String confirm;
            int length;
    
            // from client
            PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader buffedReader = new java.io.BufferedReader(new InputStreamReader(System.in));
    
            //from server
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    
            DataInputStream din = new DataInputStream(socket.getInputStream());
    
            while(true)
            {
    
                //this sends input            
                System.out.print("Enter Destired URL: ");
    
                String readerInput = buffedReader.readLine();
                readerInput = readerInput.replace("Enter Destired URL: ", "");
                printWriter.println(readerInput);
    
                if((confirm = bufferedReader.readLine()) != null)
                {
                    System.out.println(confirm);
                }
    
                //this reads in from server
                if((ip = bufferedReader.readLine()) != null)
                {
                    System.out.println("Server returns IP Address: " + ip);
                    System.out.println("");
                }
    
                int j = 4;
                byte[] message = new byte[4];
                if(j>0)
                {
                    din.readFully(message, 0, 4); // read the message
                    j=j-1;
                }
    
                for(int i = 0; i < 4; i++)
                {
                    System.out.print(message[i]);
                }
    
            }
        }
    
    }
    
  4. 问题是客户端没有收到字节数组。它仅在您在调试器中运行Server程序并在其写入Client的位置放置断点时才有效。

    有没有人有任何想法?

0 个答案:

没有答案