• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Java Socket Programming

Blitzr

New Member
Joined
Jan 29, 2020
Messages
7 (0.00/day)
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.
 
Last edited:
Joined
Dec 21, 2016
Messages
98 (0.04/day)
Location
A dusty server room
System Name HP DL380 G5
Processor 2x Intel Xeon X5460 @3.16 Ghz
Motherboard HP DL 380
Cooling 6 60mm intake fans rated for 14k RPM max load
Memory 16GB of DDR2 667Mhz
Video Card(s) AMD XFX R7 360
Storage 3x 140GB 10K SAS JBOD
Display(s) 1x Hisense 42" TV
Case DL 380 G5
Audio Device(s) HDMI Out
Power Supply 2x 800w Proprietary PSUs
Mouse A cheap logitech wireless mouse
Keyboard Acer KU-0355
Software Windows 10!
Benchmark Scores Ill get around to em soon
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

New Member
Joined
Jan 29, 2020
Messages
7 (0.00/day)
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
 
Last edited:
Top