Home > Blockchain >  Why my ServerSocket doesn't receive message from socket?
Why my ServerSocket doesn't receive message from socket?

Time:01-14

My ServerSocket can't get message from socket.

My class World with ServerSocket:

package main;

import panels.MainFrameConsole;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

public class World {
    ServerSocket serverSocket;
    Thread threadServer;

    public static void main(String[] args) {
        MainFrameConsole mainFrameConsole = new MainFrameConsole("World");
        World world = new World();
        world.startConsole();
    }


    public void startConsole()
    {
        threadServer = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    serverSocket = new ServerSocket(2002);
                    while (!serverSocket.isClosed())
                    {
                        System.out.println("here");
                        Socket socketShip = serverSocket.accept();
                        System.out.println("here2");
                        BufferedReader reader = new BufferedReader(new InputStreamReader(socketShip.getInputStream()));
                        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socketShip.getOutputStream()));
                        String str = reader.readLine();

                        System.out.println(str);
                        if (str.startsWith("initialize"))
                           writer.write(initializeShip(str));
                    }
                }
                catch (IOException e)
                {

                }
            }
        });
        threadServer.start();
    }
}

My class with Socket:

package main;

import panels.MainFrameConsole;
import panels.PanelInside;
import panels.PanelRegion;

import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.io.*;

public class Ship {
    int x;
    int y;
    String id;
    MainFrameConsole frameConsole;

    public static void main(String[] args) {
        Ship ship = new Ship();
        ship.createFrame();
    }

    public void createFrame()
    {
        frameConsole = new MainFrameConsole("Ship "   id,new OptionPanel());
    }
    public void initialize()
    {
        Thread threadShip = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Socket socketToWorld = new Socket("127.0.0.1",2002);
                    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socketToWorld.getOutputStream()));
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socketToWorld.getInputStream()));
                    bufferedWriter.write("initialize");
                    bufferedWriter.newLine();

                    String str = bufferedReader.readLine();
                    String[] array = str.split(" ");
                    id = array[0];
                    y = Integer.parseInt(array[1]);
                    x = Integer.parseInt(array[2]);

                    frameConsole.getPanelConsole().getArrayOfPanelsMain()[countRegionY(y)][countRegionX(x)].getPanels()[y%5][x%5].setShape(1);
                    frameConsole.getPanelConsole().getArrayOfPanelsMain()[countRegionY(y)][countRegionX(x)].getPanels()[y%5][x%5].repaint();

                    bufferedWriter.flush();
                    bufferedWriter.close();
                    bufferedReader.close();
                    socketToWorld.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        });
        threadShip.start();
    }
}

I'm running 2 apps: Ship and World In frameConsole, i've got button which run ship.initialize(). When I click it, in console I've got only output: here here2

Why serverSocket can't get message from socket ("initialize")?

CodePudding user response:

Never mind, I found a bug. bufferedWriter.flush() should be after bufferedWriter.newLine();

Socket socketToWorld = new Socket("127.0.0.1",2002);
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socketToWorld.getOutputStream()));
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socketToWorld.getInputStream()));
                bufferedWriter.write("initialize");
                bufferedWriter.newLine();
bufferedWriter.flush();
  • Related