Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I can successfully connect to the server using this CMD command: java TCPAsk time.nist.gov 13

My code is supposed to print the date and time but nothing is being returned, all I get is a few lines of empty space. I cant attach an image unfortunately but it says this:
time.nist.gov says:


Can someone help me figure out what is wrong with my TCPClient which I have written myself? Please note that no wrapper classes such as bufferedreader/dataoutputstream etc. is allowed and TCPClient must return bytes. TCPAsk cannot be changed.

What I have tried:

TCPClient:

<pre lang="Java"><pre>package tcpclient;

import java.net.*;
import java.io.*;

public class TCPClient
{

    public TCPClient()
    {}

    public byte[] askServer(String hostname, int port, byte[] toServerBytes) throws IOException
    {
            Socket clientSocket = new Socket(hostname, port);

            OutputStream outToServer = clientSocket.getOutputStream();
            outToServer.write(toServerBytes);

            InputStream inFromServer = clientSocket.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            int bufferSize = 1024;
            byte[] bufferArray = new byte[bufferSize];

            int result;
            int responseLength = 0;

            try
            {
                while (responseLength == inFromServer.read())
                {
                    if (responseLength <= bufferSize && responseLength != -1) {
                        result = inFromServer.read(bufferArray, 0, responseLength);
                        baos.flush();
                        baos.write(result);
                    } else
                    {
                        result = inFromServer.read(bufferArray, 0, bufferSize);
                        baos.flush();
                        baos.write(result);
                        if (inFromServer.available() == 0) {
                            byte bytesLeft = (byte) inFromServer.read();
                            if (bytesLeft != -1) {
                                baos.write(bytesLeft);
                            }
                        }
                    }
                    break;
                }
            }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        inFromServer.close();
        clientSocket.close();
        return baos.toByteArray();
    }
}



TCPAsk:
import java.net.*;
import java.io.*;
import tcpclient.TCPClient;
 
public class TCPAsk {
    /*
     * Usage: explain how to use the program, then exit with failure status
     */
    private static void usage() {
        System.err.println("Usage: TCPAsk host port <data to server>");
        System.exit(1);
    }
 
    /*
     * Main program. Parse arguments on command line and call TCPClient
     */
    public static void main( String[] args) {
        String hostname = null;
        int port = 0;
        byte[] userInputBytes = new byte[0];
 
        try {
            // Get mandatory command line arguments: hostname and port number
            int argindex = 0;
            hostname = args[argindex++];
            port = Integer.parseInt(args[argindex++]);
 
            // Remaining arguments, if any, are string to send to server
            if (argindex < args.length) {
                // Collect remaining arguments into a string with single space as separator
                StringBuilder builder = new StringBuilder();
                boolean first = true;
                while (argindex < args.length) {
                    if (first)
                        first = false;
                    else
                        builder.append(" ");
                    builder.append(args[argindex++]);
                }
                builder.append("\n");
                userInputBytes = builder.toString().getBytes();
            }
        } catch (ArrayIndexOutOfBoundsException | NumberFormatException ex) {
            // Exceeded array while parsing command line, or could
            // not convert port number argument to integer -- tell user
            // how to use the program
            usage();
        }
 
        try {
            TCPClient tcpClient = new tcpclient.TCPClient();
            byte[] serverBytes  = tcpClient.askServer(hostname, port, userInputBytes);
            String serverOutput = new String(serverBytes);
            System.out.printf("%s:%d says:\n%s", hostname, port, serverOutput);
        } catch(IOException ex) {
            System.err.println(ex);
            System.exit(1);
        }
    }
}
Posted
Updated 7-Mar-22 8:55am
v3
Comments
Richard MacCutchan 7-Mar-22 14:10pm    
Where is the server code?
boabaloo 7-Mar-22 14:12pm    
There is no server code. TCPClient and TCPAsk is a package program. TCPAsk helps TCPClient retrieve information from the time.nist.gov server.
Richard MacCutchan 7-Mar-22 14:36pm    
OK, so what data are you trying to send to the server?
boabaloo 7-Mar-22 14:50pm    
No data. You open up cmd, compile TCPAsk and TCPClient. Then you type in: java TCPAsk time.nist.gov 13 and the server should send the time and date which my TCPClient should print on the cmd. But instead of printing the time and date I get empty lines. I cant attach an image but I have written exactly what the cmd has returned.
Richard MacCutchan 7-Mar-22 15:04pm    
Yes, but your client code tries to send something to the server, and that causes problems. Remove the two lines of code at the top that send the toServerBytes array.

1 solution

I just tried this and the response was as follows:
59645 22-03-07 19:52:59 57 0 0 711.4 UTC(NIST) *

Note, you do not need to send any data to the server, just receive from it.
 
Share this answer
 
Comments
boabaloo 7-Mar-22 14:56pm    
ok wow. that is exactly what it is supposed to say. but when i try nothing happens. did you change anything in the code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900