skip to Main Content

I have a server that has been implemented by java and a flutter code and I want to connect this two by sockets in both sides. but the flutter side does not connect to java.how can I do this?
here is my simplified code for flutter side:

import 'dart:io';
import 'dart:async';

void main() async{
    Socket socket = await Socket.connect("10.0.2.2",8080);
    print('the work has finished');
    socket.write("hello");
    print('ok');
}

this is the java endpoint implementation:


public class main {
    public static void main(String[] args) {
        Server server = new Server();
        try(Socket socket = server.serverSocket.accept(); DataInputStream dataInputStream = new DataInputStream(socket.getInputStream())){
            System.out.println("someone has connected to this device");
            String message = dataInputStream.readUTF();
            System.out.println(message);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
}


it is good to mention that the server class in used in java side just listens on a specified port =.

2

Answers


  1. Your answer is unclear. It does definitely need some improvement, to help you.

    • How is your Java endpoint implemented?
    • What web-server do you use?
    • How is the architecture?
    • […]
    Login or Signup to reply.
  2. Hoping that the java websocket works you should treat it as any websocket

    please go through

    https://docs.flutter.dev/cookbook/networking/web-sockets

    and that should really help

    i would also add that you can try connecting to demo websockets to see if your flutter implimentation works then now make sure the java one works

    have a good time 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search