Need Socket Help Please

Hey All,

I was wondering if someone would be so kind and help me achieve what I am trying to do, and this is it;

I would like to create a socket in java but get the host details from a textfield to create the connection for example

Enter IP: (Connect Button Here)

how do I get what the user entered into the text field above and place it as a variable into the socket so for example instead of this way;

new Socket(“”, 5001);

it would be a variable lets say host which would be from the text field

new Socket(host, 5001);
Many Thanks


Assuming you’re running servlets you can easily get the POST variable by simply doing this in your method:

public Socket startSocket() {
  String host = request.getParameter("host");
  return new Socket(host, 5001);

Your form should prob look something like this:

<form method="post">
  <input type="text" name="host" value="">
  <input type="submit" name="submit" value="Connect">

If you notice the “name” that was given to the input text matches the same parameter in the method.

If you’re building a webapp then what YaderBH suggested should get you on the right track.

If you’re building a swing app, then you’ll need to get a reference to your JTextarea and then call the getText() method on that reference to get the entered host information.