/*
* Java socket programming client example with TCP
* socket programming at the client side, which provides example of how to define client socket, how to send message to
* the server and get response from the server with DataInputStream and DataOutputStream
*
* Author: Wei Song
* Date: 2021-09-28
* */
import java.net.*;
import java.io.*;
public class TCPClient {
// server host and port number, which would be acquired from command line parameter
private static String serverHost;
private static Integer serverPort;
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.out.println("===== Error usage: java TCPClient SERVER_IP SERVER_PORT =====");
return;
}
serverHost = args[0];
serverPort = Integer.parseInt(args[1]);
// define socket for client
Socket clientSocket = new Socket(serverHost, serverPort);
// define DataInputStream instance which would be used to receive response from the server
// define DataOutputStream instance which would be used to send message to the server
DataInputStream dataInputStream = new DataInputStream(clientSocket.getInputStream());
DataOutputStream dataOutputStream = new DataOutputStream(clientSocket.getOutputStream());
// define a BufferedReader to get input from command line i.e., standard input from keyboard
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("===== Please input any message you want to send to the server: ");
// read input from command line
String message = reader.readLine();
// write message into dataOutputStream and send/flush to the server
dataOutputStream.writeUTF(message);
dataOutputStream.flush();
// receive the server response from dataInputStream
String responseMessage = (String) dataInputStream.readUTF();
System.out.println("[recv] " + responseMessage);
System.out.println("Do you want to continue(y/n) :");
String answer = reader.readLine();
if (answer.equals("n")) {
System.out.println("Good bye");
clientSocket.close();
dataOutputStream.close();
dataInputStream.close();
break;
}
}
}
}
Resource created Saturday 04 September 2021, 10:47:45 AM, last modified Monday 04 October 2021, 08:17:22 AM.
file: TCPClient.java