I want to implement interactive chatbot system with java TCP socket Programming.
I search basic Java TCP example in google, base code is this.
Client Side
// Client side
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class EasyTCPClient {
public static void main(String[] args) {
String hostname = "127.0.0.1";
int port = 20532;
while (true) {
try (Socket socket = new Socket(hostname, port)) {
OutputStream out = socket.getOutputStream();
String realStr = "This is woolbro dev Test";
out.write(realStr.getBytes());
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String time = reader.readLine();
System.out.println(time);
} catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
} catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}
}
}
Server Side
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class EasyTCPServer {
public static void main(String[] args) {
int port = 20532;
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("Server is listening on port " + port);
while (true) {
Socket socket = serverSocket.accept();
System.out.println("[ "+socket.getInetAddress()+" ] client connected");
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
writer.println(new Date().toString());
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
System.out.println("###### msg : "+reader.readLine());
}
} catch (IOException ex) {
System.out.println("Server exception: " + ex.getMessage());
ex.printStackTrace();
}
}
}
then, I change my code to success of this statement
TCP client will ‘connect’ only once at the beginning of the program. After that, even if the menu repeats, it simply uses that same connection.
I understand that statement is follows: Create one socket, and use that socket, client send many times to server without close connection.
so I change my client side code follows..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class EasyTCPClient {
public static void main(String[] args) {
String hostname = "127.0.0.1";
int port = 20532;
try (Socket socket = new Socket(hostname, port)) { // <------ change two line
while (true) { // <---- change two line
OutputStream out = socket.getOutputStream();
String realStr = "This is woolbro dev Test";
out.write(realStr.getBytes());
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String time = reader.readLine();
System.out.println(time);
}
} catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
} catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}
}
but this approach doesn't work correctly. client wait reader.readLine function infinitly. which point I missed in that approach? I appreicated with your questions. Thankyou
I search basic Java TCP example in google, base code is this.
I search socket.setKeepAlive(true) option. but doesn't work.
and I search thread's approach, but this approach support to multiple client I wanna support only one clinet, and this client has multiple write to server using for loop without close connection in socket.