Last active
June 3, 2022 12:24
-
-
Save mohammadkarbalaee/305c85936e132c037c8ca4a0930484d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
implementation 'com.google.code.gson:gson:2.9.0' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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