#🧦 🦶🏻Sockets java 101

1 messages · Page 1 of 1 (latest)

slate steeple
#
 /**
 * This program makes a socket connection to the atomic clock in Boulder, Colorado, and prints the
 * time that the server sends.
 * @version 1.20 2004-08-03
 * @author Cay Horstmann
 */
public class SocketTest
{
   public static void main(String[] args)
   {
      try
      {
         Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13);
         try
         {
            InputStream inStream = s.getInputStream();
            Scanner in = new Scanner(inStream);

            while (in.hasNextLine())
            {
               String line = in.nextLine();
               System.out.println(line);
            }
         }
         finally
         {
            s.close();
         }
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
   }
}

When I try this program, I don't get anything printed out to the terminal.. What's wrong?

covert templeBOT
#

<@&987246399047479336> please have a look, thanks.

covert templeBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

slate steeple
#

I debugged it, seems like I'm not getting any input stream from taht socket..

#

But idk why

slate coral
slate steeple
#

The network I'm using blocks some websites o.o

slate coral
slate steeple
#

Right

#

And yes I jsut started learning about socket programming in java

#

Apparently I can make my machine a server 😮

#

Using ServerSocket class

#

var server = new ServerSocket(8189);

#

And then

#

Socket incoming = server.accept();

#

These 2 lines of code, turns my machine into a server that waits for clients to connect to its 8189 port 😮

#

--

#

--

#

--

copper shore
#

a server is nothing else than a regular computer

#

so this isn't really sth surprising or magic

slate steeple
#
Public class EchoServer {
    public static void main(String[] args) throws IOException {

 try (var server = new ServerSocket(8189) ) {
            Socket incoming = server.accept();
            System.out.println("hi");
            InputStream inputStream = incoming.getInputStream();
            OutputStream outputStream = incoming.getOutputStream();
            
            var in = new Scanner(inputStream, StandardCharsets.UTF_8);
            var out = new PrintWriter(outputStream, true, StandardCharsets.UTF_8);
            out.println("hello! Enter Bye to exit.");
            var done = false;
            while (!done) {
                String line = in.nextLine();
                out.println("Echo: " + line);
                if (line.trim().equals("Bye"))
                    done = true;
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
}```
covert templeBOT
slate steeple
#

On the 4th line

#

I'm also confused about the 2 input and output streams,

#

I think the output stream is used by the server

#

And the input stream is used by the client?

#

Oh wait I'm not sure but I think what's happening is that in this line
Scoket incoming = server.accept();
basically the server accepts "incoming" as his client, so now "incoming" is the client..?

copper shore
#

input stream, from the perspective of the server code, is the clients request

#

and output stream is for sending ur response

slate steeple
#

👍🏻

covert templeBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍

slate steeple
#

I don't get how the server is returning the same message that we're sending to it....

#

There is nothing in the code specified how the server should work

#

We're just creating 2 objects of output and input streams for the client side..

slate coral
#

Does this help? Notice that the server's InputStream complements the client's OutputStream and vice versa.

#

@slate steeple

slate steeple
#

But the difference is that we instantiated the input and output streams for the client, we did not do that for the server

#

So basically for the client, we not just instantiated the output stream we wrapped it with a printerWriter to push out data..

#

I just don't get how the server randomly functions as "echoing server"

#

We did not write the logic for the server as to how it should functions..
All we did in the code is for the client side.

slate coral
#

Do you have the Client code? You should be calling socket.getInputStream() to my knowledge.

slate steeple
#

#1071763855698563142 message

#

incoming is the client

slate coral
slate steeple
#

Oh wait a second, are u saying the printWriter in this code, is for the server?

slate coral
#

All that code is for the server 🙂

slate steeple
#

Then wait, incoming is the socket in the server side?

#

Cuz I know there are 2 sockets,
1 at the server side, and 1 on the client's side

slate coral
#

Yeah that's right

slate steeple
#

Sockets connects to ports, ports connects to the network, right?

slate steeple
slate coral
# slate steeple Sockets connects to ports, ports connects to the network, right?

Well, the analogy is that like an electrical socket plugs into a port, a software socket plugs into a port. But you do also need the IP address to use a software socket too.

Network is imagine you have a billion boxes all with 65,536 ports. Some of the ports have sockets plugged into them and most are empty. Between the sockets there are trillions of cables sprawled across the floor. The cables themselves sometimes contain 2 wires one for sending and one for receiving, although which side is sending and which is receiving flips depending on the perspective of server or client. It's obviously just an analogy and I'm sure you can poke holes in it.

slate steeple
#

There is something weird happening though

#

on my pc I did this first:
ServerSocket server = new ServerSocket(8189);
Socket serverSocket = server.accept();
I know the above code will just stay waiting for a client to connect to this server..

and then on my laptop I did the following:
Socket client = new Socket("127.0.0.1", 8189);
^ I expected this code to establish a connection between both ends my laptop and the pc, which will cause the pc code to stop running since it found a client...

However, on my laptop I get ->
Exception in thread "main" java.net.ConnectException: Connection refused

slate coral
#

127.0.0.1 is a loopback address. It's an address for the computer you're currently using. So when you connect to 127.0.0.1 on your laptop, you're connecting to your laptop, which doesn't have a server running.

slate steeple
#

Uh

slate coral
#

You could run both the server and client on the same machine and then use 127.0.0.1 but if you want to connect to different computers then you'll need to run ipconfig to find the server's IP address on your local network. That assumes that there's no network firewalls blocking you though.

#

Might be worth studying networking though so you can fill the knowledge gap a little.

slate steeple
#

DUDE

#

WTF u r AMAZING IT FUCKING WORKED

#

I used my pc's ip adress 192.168.100.2

#

and it worked 😄

copper shore
#

u have to differnetiate between ur internal IP and external

#

cause the external doesnt point to ur pc, only to ur router

slate steeple
#

Yeah

#

Studied that on networking

copper shore
#

then u have to tell the router via port forwarding to which of the 10 machines in ur household it should forward the traffic to