Octavian Niculescu
Octavian Niculescu

Reputation: 1327

Sending data through sockets in Java from different locations in code

This is my first time using Sockets in any programming language (I'm not familiar with the subject at all) and I'm having a hard time understanding how to send data from different places of code.

Okay I'm running a ServerSocket, and I want to send data from another place in my code rather than the ServerSockets class. How can I do that?

Here's my code:

Server.java

package socket;

import database.models.PersoanaEntity;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static final int PORT = 27015;

    public void start() {
        new Thread(() -> {
            try {
                ServerSocket serverSocket = new ServerSocket(PORT);
                Socket clientSocket = null;
                PersoanaEntity loggedInPerson = new PersoanaEntity();
                boolean isClose = false;

                System.out.println("Server is running");
                while (!isClose) {
                    clientSocket = serverSocket.accept();
                    new Thread(new ServerThread(clientSocket)).start();
                }
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }
}

ServerThread.java

package socket;

import org.json.JSONObject;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ServerThread extends Thread{
    private Socket socket = null;
    private ObjectInputStream in = null;
    private ObjectOutputStream out = null;

    public ServerThread(Socket socket) {
        this.socket = socket;
        try {
            //For receiving and sending data
            this.in = new ObjectInputStream(socket.getInputStream());
            this.out = new ObjectOutputStream(socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        try {
            String recievedData = this.in.readObject().toString();
            JSONObject jsonObject = new JSONObject(recievedData);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void sendMessage(String message) {
        try {
            this.out.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Let's say I want to call sendMessage from other classes inside my project. Is there any way to do that?

Thanks!

Upvotes: 0

Views: 56

Answers (1)

polo-language
polo-language

Reputation: 846

Keep a reference to the ServerThread object and change the visibility of sendMessage to public. Then it can be called from anywhere.

Aside: Since ServerThread extends Thread, you don't need to wrap it in a new Thread like this new Thread(new ServerThread(clientSocket)).

Upvotes: 1

Related Questions