# Java Socket Programming



## Blitzr (Jan 29, 2020)

Hello

I'm a computer science student and was wondering if anyone here has any experience, advice, or real world practical applications on Java socket programming, thread grouping, JVM mechanics, etc. How does a programmer go about determining thread priorities and daemon/non-daemon threads. Still attempting to adapt my thinking from C++ pointers to Java references. I'm fairly new to the java.net.* and java.io.* libraries so any advice is much appreciated.

Thank You.


----------



## Ensefalon (Feb 23, 2020)

Blitzr said:


> Hello
> 
> I'm a computer science student and was wondering if anyone here has any experience, advice, or real world practical applications on Java socket programming, thread grouping, JVM mechanics, etc. How does a programmer go about determining thread priorities and daemon/non-daemon threads. Still attempting to adapt my thinking from C++ pointers to Java references. I'm fairly new to the java.net.* and java.io.* libraries so any advice is much appreciated.
> 
> Thank You.



No problem at all!

Java IO is a great tool. It's great if you're managing lots of data throughput, securely*, and reliably over a network, or between separate applications on your computer.

Sockets are an end to end connection. I like to think a pair of garden hoses that branch off from the main server to the connecting clients.

With sockets, we use streams; an input stream, and an output stream, both of which can be used to send data from application to application in a "duplex" manner.

ObjectInputStream.read() will read whatever data was sent from the client/server.
ObjectOutputStream.write() followed by .flush() will write to the output stream and send to client/server

In real world practice, Java IO is becoming more and more phased at in place of NIO due to its higher concurrency scalability and resource usages.

NIO stands for "new input output" and implements non blocking methods as well as the use of buffers and channels versus blocking streams.

Blocking refers to the blocking of a thread or "pausing" of code execution as the blocker waits for whatever input it needs from the stream. NIO grants us the ability to use buffers that can be read from at any time containing whatever relevant data we need from either the client or the server, and only need to use one thread to manage tens of thousands of users vs. using individual threads for each client connection which gets _VERY_ inefficient due to stack size of each thread, as well as whether or not the OS supports so many threads going on at once.

Thread.setPriority()** can be used to very much establish priority of threads. Thread priority is on a scale from 1-10, one being lowest priority, and 10 being highest.

So whenever you create a new thread via calling its constructor using "new Thread()" you can set the priority of that thread by using the method I stated previously.


TL;DR I wouldn't bother too much with java.io, and rather gear myself towards java.nio. In spite of how much more intuitive it is, you can do a lot more with it and it scales WAY better than java.io .

You have a great day.

You're welcome ~ Sal

*As an aforementioned grey hatter, not that secure. Experiment with SSL if you like.
**I pulled that method out of my arse, but it may or may not be right, refer to the Javadoc in case I am stupid.


----------



## Blitzr (Apr 8, 2020)

Hey thanks for the reply Ensefalon and thanks for the information on Java NIO channel/buffer/selector interface. I did some quick research and found that a channel works much like a bit stream where the channel data is read into a buffer and vice versa. I'm now working with node.js and found the single threaded callbacks with event handling approaches thread priority in a different manner (asynchronous non-blocking execution). The NIO you mentioned seems to be structured in the same way that java.io is with byte, char, double, float, etc. buffer and file, datagram?, socket, and ServerSocketChannel


----------

