Is it possible to run a socket server and socket client on the same machine?
Asked Answered
E

4

19

In java it is possible to create a socket server and a socket client, is it possible to have an instance of the socket server running and a socket/server client that is receiving data from the socket server on the same machine?

e.g the socket server runs on port 60010 and the socket client is running on the same machine connecting to that port through a socket or will I need to by a new machine and add it to my network? If it has a unique IP Address and port number running on the TCP/IP layer.

Excitor answered 9/4, 2012 at 4:58 Comment(1)
Yes this is definitely possible. It's easy to try it out and see for yourself.Prettify
P
23

Here's a simple runnable example to get you started. It starts two threads, one with a ServerSocket and one which makes a Socket connection. One continuously sends strings and the other prints them.

You should simply be able to run this class as-is.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketTest {
    public static void main(String[] args) throws IOException {

        startServer();
        startSender();
    }

    public static void startSender() {
        (new Thread() {
            @Override
            public void run() {
                try {
                    Socket s = new Socket("localhost", 60010);
                    BufferedWriter out = new BufferedWriter(
                            new OutputStreamWriter(s.getOutputStream()));

                    while (true) {
                        out.write("Hello World!");
                        out.newLine();
                        out.flush();

                        Thread.sleep(200);
                    }

                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void startServer() {
        (new Thread() {
            @Override
            public void run() {
                ServerSocket ss;
                try {
                    ss = new ServerSocket(60010);

                    Socket s = ss.accept();

                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(s.getInputStream()));
                    String line = null;
                    while ((line = in.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
Prettify answered 9/4, 2012 at 5:8 Comment(1)
Well, could there be a problem if client and server live in different processes?Christiechristin
R
3

Yes, you can have the following on the same machine:

ServerSocket server = new ServerSocket(60010);
Socket client = server.accept();

Somewhere else:

Socket socket = new Socket("localhost", 60010);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("Hello server");
Romansh answered 9/4, 2012 at 5:2 Comment(4)
You cant create 2 sockets on the same portGareth
You can't bind two server sockets to the same port, but you can have multiple client sockets connecting to the same port (as in a web server).Romansh
In a web server it's a Server socket... Anyway, if you say so I believe in you. From my experience, I always let client sockets to have "random" port number.Gareth
That's what I described above, one Server Socket and one client socket. How is that different?Romansh
A
1

Yes, you can run a client and server on the same machine. I do it all the time for development. If you are having troubles though, some routers have problems forwarding packets back to themselves. Try using localhost instead of your external IP for development.

Arsenious answered 9/4, 2012 at 5:2 Comment(0)
T
1

Yes it is completely possible. Every OS has a loopback interface. You can have multiple clients connect to one server on your computer. This kind of communication takes place over the loopback interface.

Trotta answered 9/4, 2012 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.