Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 796
Aaron Avatar asked Apr 09 '12 04:04

Aaron


People also ask

Can a socket be both server and client?

You can use the same socket for whatever you want, as long as your protocol handles it.

Can processes on the same machine use sockets?

That's right, sockets are a way for two processes to communicate regardless of whether it's over the network or within the same machine. You could invent other mechanisms for communication within the same machine (and there are plenty), but why if sockets already serve that purpose perfectly fine? >Fine deceze.

Can multiple clients use the same socket?

Multiple clients can connect to the same port (say 80) on the server because on the server side, after creating a socket and binding (setting local IP and port) listen is called on the socket which tells the OS to accept incoming connections.

What is the difference between server socket and client socket?

A server (program) runs on a specific computer and has a socket that is bound to a specific port. The server waits and listens to the socket for a client to make a connection request. If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to a different port.


2 Answers

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();
    }
}
like image 81
ulmangt Avatar answered Oct 03 '22 20:10

ulmangt


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");
like image 37
Abdullah Jibaly Avatar answered Oct 03 '22 20:10

Abdullah Jibaly