Skip to content

Instantly share code, notes, and snippets.

@ILoveBacteria
Created June 29, 2023 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ILoveBacteria/67620f76577d94081fdcf2922723240b to your computer and use it in GitHub Desktop.
Save ILoveBacteria/67620f76577d94081fdcf2922723240b to your computer and use it in GitHub Desktop.
Request and response through socket in Dart and Java
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;
}
}
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