270 likes | 494 Views
Java Networking. CSC 331. Networking. Sockets Hold two streams One input One output Java.net.Socket class Setting up the connection Similar to a telephone system. Networking with Java Technology. Setting up the connection Must know two things The address or name of remote machine
E N D
Java Networking CSC 331
Networking • Sockets • Hold two streams • One input • One output • Java.net.Socket class • Setting up the connection • Similar to a telephone system 20060807
Networking with Java Technology • Setting up the connection • Must know two things • The address or name of remote machine • The port number to identify purpose at the server • Port numbers • 1 to 65535 • Numbers below 1024 are “well-known” ports reserved for predefined services, e.g., port 443 is for secure web access. 20060807
Networking with Java Technology • Client and server must agree in advance on which port to use. • Author of server programs should determine from network administrator which server port to use. 20060807
Socket Connection: Client Side -Reading • Establish a Socket connection to the server by creating a Socket object. • Chain an InputStreamReader to the Socket’s input stream. • Chain a BufferedReader to the InputStreamReader. • Use BufferedReader methods such as readLine() to read server messages. 20060807
Socket Connection: Client Side -Writing • Establish a Socket connection to the server by creating a Socket object. • Chain a PrintWriter (or another suitable class) to the Socket output stream. • Use methods such as print() and println() to write data. 20060807
Exception Handling • Enclose the code to establish a connection and the I/O code, all within a try-catch block. 20060807
Need for Multithreading in Java • A socket connection between client and server can’t be shared. • The example server code handles one client at a time. • Processing of multiple clients is allowed by existence of multiple execution threads. • Each thread has its own stack. 20060807
Multithreading in Java • See java.lang.Thread class • A Thread object represents a thread of execution. • A thread, or execution context, is like a virtual CPU with its own code and data. 20060807
Three Parts of a Thread • A virtual CPU • The code being executed by the CPU • The data on which the code works 20060807
Creating a Thread • Runnable, a marker interface • (see java.lang in java API) • Threat t = new Thread(r); • Where r is an instance of Runnable • See ThreadTester.java • Start the thread. • t.start(); 20060807
Running a Thread • When t.start() is executed for thread t, the runnable object begins at its run() method. • Actually, thread t becomes eligible for scheduling by the JVM, not necessarily immediately. • Code following t.start() runs concurrently with the thread. 20060807
Concurrent Threads Multithreaded programming • Multiple threads from the same Runnable instance. • Thread t1=new Thread(r);Thread t2=new Thread(r); • These threads share the same data and code. 20060807
Thread Scheduling • Java technology usually uses preemptive threads. • Preemptive does not necessarily mean time-sliced. • One of many runnable threads runs until it is no longer runnable, or until another thread of higher priority becomes runnable. • In the latter case, the original thread is preempted and becomes blocked. 20060807
States of a Thread object • New • Runnable • Running • Blocked • Dead 20060807
The Thread Scheduler • Not controlled by the programmer. • Do not assume a certain scheduler algorithm! • JVMs do not implement the scheduler uniformly. • Therefore, the scheduler behavior is unpredictable. 20060807
“Control” of Threads • Testing threads • isAlive() • Accessing thread priority • getPriority() • setPriority() [may not work, depending on o/s] • Putting threads on hold • sleep() • join() • yield() 20060807
Sleepy Time in Threadville • A thread may call sleep(x) in order to “go to sleep” for x milliseconds. • This gives other threads with lower priority a chance to run. 20060807
Time to Yield • Thread.yield() • Give other runnable threads a chance to run. • Thread.join() • Causes current thread to wait until the thread on which join() is called terminates. 20060807
Concurrency Issues - 1 • When multiple threads access the same data, the possibility of data corruption exists. • One solution: • Provide exclusive access to code that affects shared data using a “lock flag” enabled by the synchronized keyword. 20060807
Concurrency Issues - 2 • Synchronization can lead to deadlock. • Deadlock: Two processes each hold exclusive access to resources needed by the other. 20060807