1

I have to establish a TCP connection to a server, which requires that I send the credential to logon in the format:

<STX>username=fred&password=123456<ETX>

Let's say host: qstage.thetcphost.com and port:8999

I am new to socket programming and using the same to implement this. I have used java.net.Socket at the client side but I dont know how do I send the above string for authentication to the TCP Server in Java.

I am able to telnet the server now.

But how do I pass the credential string in the < STX >...< ETX > format after (or during): Socket socket = new Socket("mshxml.morningstar.com", 8999); I mean what is the piece of code that I have to write to authenticate myself to the TCP server?

I have searched this site for this info but could not find any. Help would be greatly appreciated.

Pratap
  • 655
  • 2
  • 10
  • 20
  • take a look :[java.net.ConnectException: Connection refused](http://stackoverflow.com/q/6876266/4290096) – Arun Xavier Mar 15 '16 at 06:26
  • The connection refused problem is resolved now. My ip had to be whitelisted before creating the connection. Now the main problem remains: how to pass the credential string to the server? – Pratap Mar 15 '16 at 06:49

1 Answers1

0

Establish the socket connection is the first step before you can send any creds information.

If this step is failing, consider the specs from your target server. Is the correct port provided? Any consideration on protocol? Usually, the authentication will be happen after you have already established successfully the connection.

Editted: Add source code for writing to socket from a socket client. Now, in this context, you're a socket client, try to send creds to server.

public class GreetingClient
{
   public static void main(String [] args)
   {
      String serverName = args[0];
      int port = Integer.parseInt(args[1]);
      try
      {
         System.out.println("Connecting to " + serverName +
         " on port " + port);
         Socket client = new Socket(serverName, port);
         System.out.println("Just connected to " 
         + client.getRemoteSocketAddress());
         OutputStream outToServer = client.getOutputStream();
         DataOutputStream out = new DataOutputStream(outToServer);
         out.writeUTF("Hello from "
                      + client.getLocalSocketAddress());
         InputStream inFromServer = client.getInputStream();
         DataInputStream in =
                        new DataInputStream(inFromServer);
         System.out.println("Server says " + in.readUTF());
         client.close();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

Sample found here: http://www.tutorialspoint.com/java/java_networking.htm

toantran
  • 1,789
  • 17
  • 25
  • I have the established the connection. My IP had to be whitelisted for this. Now can you please tell me how to send the authentication credential string to the server in the above mentioned format? – Pratap Mar 15 '16 at 10:39
  • I edited the answer to add sample `writing` action to socket server. @Pratap – toantran Mar 16 '16 at 03:41