Created
June 29, 2023 15:05
-
-
Save ILoveBacteria/67620f76577d94081fdcf2922723240b to your computer and use it in GitHub Desktop.
Request and response through socket in Dart and Java
This file contains hidden or 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 'dart:convert'; | |
import 'dart:core'; | |
import 'dart:io'; | |
class MySocket { | |
final int port = 5000; | |
final String host = "10.0.2.2"; | |
final String data; | |
MySocket(this.data); | |
Future<String> sendAndReceive() async { | |
var socket = await Socket.connect(host, port); | |
socket.writeln(data); | |
String response = await utf8.decoder.bind(socket).join(); | |
socket.close(); | |
return response; | |
} | |
} |
This file contains hidden or 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 java.io.IOException; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.io.DataOutputStream; | |
import java.util.Scanner; | |
public class Server { | |
public static void main(String[] args) { | |
try (ServerSocket serverSocket = new ServerSocket(5000)) { | |
while (true) { | |
Socket socket = serverSocket.accept(); | |
Scanner in = new Scanner(socket.getInputStream(), "UTF-8"); | |
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream()); | |
String input = in.nextLine(); | |
byte[] response = null; | |
outputStream.write(response); | |
outputStream.flush(); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment