-1

I am coding a java app. I'm trying to send data and receive data on localhost with same program and same port. My code is working but only first data is received. For example I try to send "1" first, it is printed to console. Then I try to send "2" but it is not printed.

Main.java:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 360, 150));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Controller.java:

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Controller {

    @FXML
    TextField gonderilecekVeri = new TextField();
    TextField gonder_ip = new TextField();

    int port = 12345;
    ServerSocket dinle;

    public void portuDinle() {
        System.out.println("Portu Dinliyor...");

        new Thread(() -> {
            Socket baglanti;
            String veri = null;
            try {
                dinle = new ServerSocket(port);
                baglanti = dinle.accept();
                BufferedReader gelen = new BufferedReader(new InputStreamReader(baglanti.getInputStream()));
                veri = gelen.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (veri != null) {
                System.out.println(veri);
            }

        }).start();
    }

    public void dinlemeDurdur() {
        System.out.println("Dinleme Durdu");

        try {
            dinle.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void veriGonder() {
        String veri = gonderilecekVeri.getText();
        String ipAdresi = gonder_ip.getText();
        int gondermePortu = 12345;

        try {
            Socket gonder = new Socket(ipAdresi, gondermePortu);
            DataOutputStream outToServer = new DataOutputStream(gonder.getOutputStream());
            outToServer.writeBytes(veri + '\n');
            outToServer.flush();
            outToServer.close();
            gonder.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

What I've tried?

*I said received only first data because I try 2 different app to send data and result is the same both of them.

*I try close socket after receive data and start it again. If I do this manually (with javafx gui which I code. Triggered the dinlemeDurdur func. in Controller.java) it works. But when I try to close and re-start with code (after print the data) it doesn't work.

I mean above (portuDinle function in Controller.java):

            if (veri != null) {
            System.out.println(veri);
            try {
                dinle.close();
                dinle = new ServerSocket(port);
                baglanti = dinle.accept();
                BufferedReader gelen = new BufferedReader(new InputStreamReader(baglanti.getInputStream()));
                veri = gelen.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

This works but I am curious about I have to restart server like this? (Controller.java portuDinle func.)

        if (veri != null) {
            System.out.println(veri);
            dinlemeDurdur();
            portuDinle();
        }

By the way, there is no error. Console is clean. Only first data is print.

1 Answers1

0

You only accept one connection. You need to call accept() in a loop. Depending on your application protocol you probably also need to call readLine() in a loop, until it returns null. And if you want to service clients concurrently, you need to start a new thread per accepted connection, rather than handling its I/O inline in the accept thread.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • You should start a new thread per client if you want them to be serviced concurrently. I already said that. The comment facility is not here to teach you how to start a thread. There are plenty of examples. See for example the Custom Networking section of the Java Tutorial. – user207421 Mar 06 '16 at 00:23
  • Look for NIO. Start with http://tutorials.jenkov.com/java-nio/index.html. Have a look at related SE questions: http://stackoverflow.com/questions/4752130/java-thread-per-connection-model-vs-nio and http://stackoverflow.com/questions/10113902/old-i-o-thread-per-client-model-or-nio-reactor-pattern. – Ravindra babu Mar 06 '16 at 00:49
  • @RasimAndıran You start a new thread every time you accept a new client. I've now said that three times. What part of it don't you understand? 1000 threads is nothing by the way, there are servers out there with 100k threads. You should ignore the advice to look at NIO until you know you have a threading problem. – user207421 Mar 06 '16 at 01:46