Skip to content

Instantly share code, notes, and snippets.

@mohammadkarbalaee
Last active June 3, 2022 12:24
Show Gist options
  • Save mohammadkarbalaee/305c85936e132c037c8ca4a0930484d8 to your computer and use it in GitHub Desktop.
Save mohammadkarbalaee/305c85936e132c037c8ca4a0930484d8 to your computer and use it in GitHub Desktop.
import com.google.gson.Gson;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost",5000);
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
Gson gson = new Gson();
Request request = new Request("300");
String jsonRequest = gson.toJson(request);
dataOutputStream.writeUTF(jsonRequest);
dataOutputStream.flush();
dataOutputStream.close();
socket.close();
}
}
dependencies {
implementation 'com.google.code.gson:gson:2.9.0'
}
import com.google.gson.Gson;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(5000);
Socket socket = serverSocket.accept();
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
String jsonRequest = dataInputStream.readUTF();
Gson gson = new Gson();
Request request = gson.fromJson(jsonRequest,Request.class);
System.out.println(request.requestCode);
dataInputStream.close();
socket.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment