I'm currently working on a TCP/IP socket request response system using java, I was able to setup some client/server interfaces where the client is able to send messages to the server and the server prints the message so the console. I want the server to also be able to send responses to the client and vis-versa. Is there a way possible I can achieve this ?
This is the client code
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {
public static void main(String[] args) throws IOException {
String localhost = "127.0.0.1";
int port = 8091;
sendMessage(localhost, port);
}
public static void sendMessage(String server, int port) throws IOException {
// Client
Socket socket = new Socket(server, port);
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while(true){
String input = bufferedReader.readLine();
output.writeUTF(input);
System.out.println("message sent " + input );
// receiveResponse(port);
if(input.equalsIgnoreCase("exit"))
break;
// if(output.writeUTF(input)){}
}
socket.close();
}
public static void receiveResponse(int port) {
try {
ServerSocket serverSocket = new ServerSocket(port);
Socket as = serverSocket.accept();
DataInputStream in = new DataInputStream(as.getInputStream());
String yoo = in.readUTF();
System.out.println("Client: " + yoo);
if(!yoo.isEmpty()){
System.out.println("message received");
}
if(yoo.equalsIgnoreCase("exit")){
System.out.println("Hello world");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}```
Below is the server code
```
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
int port = 8091;
receiveMessage(port);
}
public static void receiveMessage(int port) throws IOException {
ServerSocket serverSocket = new ServerSocket(port);
Socket as = serverSocket.accept();
System.out.println("Connected");
System.out.println("incoming = " + as.getInputStream());
DataInputStream in = new DataInputStream(as.getInputStream());
System.out.println(in.readUTF());
// ==========================
while(true){
String yoo = in.readUTF();
System.out.println("Client: " + yoo);
if(!yoo.isEmpty()){
// sendResult();
System.out.println("message received");
}
if(yoo.equalsIgnoreCase("exit"))
break;
}
as.close();
}
public static void sendResult( ) {
try {
System.out.println("HELLO WORLD");
System.out.println("Connected to client");
// ObjectOutputStream os = new ObjectOutputStream(as.getOutputStream());
//
// os.writeObject(result);
// System.out.println("Result sent");
String str = "Hello World";
OutputStreamWriter osw;
Socket socket = null;
socket = new Socket("localhost", 8090);
osw =new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
osw.write(str, 0, str.length());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
```
All things being equal (starting from what you already have) you could do something like this:
public static void receiveResponse(Socket socket) {
try {
DataInputStream in = new DataInputStream(socket.getInputStream());
String yoo = in.readUTF();
System.out.println("Client: " + yoo);
if(!yoo.isEmpty()){
System.out.println("message received");
}
if(yoo.equalsIgnoreCase("exit")){
System.out.println("Hello world");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
Some things you'll need to look at a little:
Main
class with a Client
class that manages the communication. This would allow you to maintain socket
, inStream
and outStream
as instance variables.DataInputStream
with a more specific / consistent transfer protocol method (byte count, delimiter, etc).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.