//The java.io classes allow input and output streams,etc, to be created
//to access and use input output devices, including sockets
//The java.net classes are required for socket communication
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
//TCPClient is the java client that will connect to the server
//The server must be present and running for the connnection and
//data exchange to take place.
class TCPClient {
public static void main(String args[]) throws Exception
{
String choice = " ";
//Example string variables, sentence is sent to the server
//All input variables (to server) can be defined here.
String sentence = null;
//Modified sentence is received back from the server
//All ouput variables (from server) can be defined here
String modifiedSentence = null;
//Declare socket before using it so exception can be checked later on.
Socket clientSocket = null;
//Accept input from the keyboard
//A swing or gui interface can go here
//BufferedReader inFromUser =
// new BufferedReader(new InputStreamReader(System.in));
//Client socket will be on the host computer (server) using port 6789
//String serverIP = "192.235.23.225"; //Server IP
String serverIP = JOptionPane.showInputDialog("Enter the Server IP Address to connect to, for SCCCC enter 192.235.23.225 : ");
int serverPort = 6789; //Server Port, only 1 server per port
//Loop continuously to
//1) Connect to server,
//2) Get message from screen
//3) Write message to server socket
//4) Read response from server socket
//5) Log the response
//6) Disconnect from server
while(!(choice.equalsIgnoreCase ("x"))){
//Establish the client TCPSocket socket connected to the Server
try{
clientSocket = new Socket(serverIP, serverPort);
}
catch (Exception e) //Check for exceptions, server may be down
{
System.out.println("Don't know about host: " + serverIP + " " + serverPort);
System.exit(1); //Stop program execution, server has problem
}
//Create an output stream to the server
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
//Create an input stream from the server
BufferedReader inFromServer =
new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//Read a sentence from the screen, GUI logic can go here, collect
//your server input, in this case is a simple string called sentence
//The developer may choose to get messages or transaction here to send
//to the remote server to be pocessed
// sentence = inFromUser.readLine();
sentence = JOptionPane.showInputDialog("Enter you message to the Server: ");
//****************Send Statement****************
//Placing the sentence from the client into the server
outToServer.writeBytes(sentence + '\n');
//***************Receive Statement****************
//Receiving back the server response from the reader
modifiedSentence = inFromServer.readLine();
//Display the response. The developer can add logic here to
//process the response.
// System.out.println("FROM SERVER: " + modifiedSentence);
String outmessage = "Here is the server response" + "\n"
+ modifiedSentence + "\n"
+ " Press Enter to continue or x for exit.";
choice = JOptionPane.showInputDialog(outmessage);
//Close the socket when finihed
clientSocket.close();
} //End Loop
}
}