-3

I want to publish my socket server on a Window host or another for my project. However, I could not find how to do this because I could not find any examples. There is Server and Client in here, but I will use server one. Client just for testing it. And also this code can run in the Windows terminal. It is a good point. Please help me how can I solve it?

Server.JAVA

import java.net.*;
import java.io.*;

public class Server
{
    //initialize socket and input stream
    private Socket          socket   = null;
    private ServerSocket    server   = null;
    private DataInputStream in       =  null;

    // constructor with port
    public Server(int port)
    {
        // starts server and waits for a connection
        try
        {
            server = new ServerSocket(port);
            System.out.println("Server started");

            System.out.println("Waiting for a client ...");

            socket = server.accept();
            System.out.println("Client accepted");

            // takes input from the client socket
            in = new DataInputStream(
                    new BufferedInputStream(socket.getInputStream()));

            String line = "";

            // reads message from client until "Stop" is sent
            while (!line.equals("Stop"))
            {
                try
                {
                    line = in.readUTF();
                    System.out.println(line);

                }
                catch(IOException i)
                {
                    System.out.println(i);
                }
            }
            System.out.println("Closing connection");

            // close connection
            socket.close();
            in.close();
        }
        catch(IOException i)
        {
            System.out.println(i);
        }
    }

    public static void main(String args[])
    {
        Server server = new Server( 5000);
    }
}

Client.java

import java.net.*;
import java.io.*;

public class Client
{
    // initialize socket and input output streams
    private Socket socket            = null;
    private DataInputStream  input   = null;
    private DataOutputStream out     = null;

    // constructor to put ip address and port
    public Client(String address, int port)
    {
        // establish a connection
        try
        {
            socket = new Socket(address, port);
            System.out.println("Connected");

            // takes input from terminal
            input  = new DataInputStream(System.in);

            // sends output to the socket
            out    = new DataOutputStream(socket.getOutputStream());
        }
        catch(UnknownHostException u)
        {
            System.out.println(u);
        }
        catch(IOException i)
        {
            System.out.println(i);
        }

        // string to read message from input
        String line = "";

        // keep reading until "Stop" is input
        while (!line.equals("Stop"))
        {
            try
            {
                line = input.readLine();
                out.writeUTF(line);
            }
            catch(IOException i)
            {
                System.out.println(i);
            }
        }

        // close the connection
        try
        {
            input.close();
            out.close();
            socket.close();
        }
        catch(IOException i)
        {
            System.out.println(i);
        }
    }

    public static void main(String args[])
    {
        Client client = new Client("127.0.0.1", 5000);
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sunny
  • 33
  • 3
  • 10

1 Answers1

1

There's no such thing as "publish". Just run your server. That's it. Now clients can connect to it.

If there is a firewall between your client and server, then you'll have to configure the firewall appropriately. If you're on a network with private addresses (typical home LAN setup) then you'll have to arrange port forwarding in the NAT box ('router').

  • Then can not i do this on Windows host? This is what was required of me. – Sunny Jun 21 '22 at 07:51
  • 1
    @RozerinYıldız What do you mean by "publish"? Do you mean deploy to a server _machine_ (with Windows as the OS)? Because if you just want to run it on your own computer, just run the Java program. – Slaw Jun 21 '22 at 08:10
  • @Slaw My aim is not just to run it on my own computer. For example, I want to be able to see it on a windows host. If possible how can I deploy it? – Sunny Jun 21 '22 at 08:15
  • @RozerinYıldız install Java on the Windows host and run the program on the Windows host – user253751 Jun 21 '22 at 08:36
  • @RozerinYıldız Simplest approach, given your program? Just copy the JAR file to the other computer, make sure Java is installed on said computer, and then execute your program. But you might have issues with connecting to it, depending on the network(s) the devices are on, DNS configurations (if you're not just using the "raw" IP address), and so on. – Slaw Jun 21 '22 at 08:38