Download presentation
Presentation is loading. Please wait.
1
Java Networking CSC 331
2
Networking Sockets Setting up the connection Hold two streams
One input One output Java.net.Socket class Setting up the connection Similar to a telephone system
3
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.
4
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.
5
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. See illustration, text p. 478.
6
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. See illustration, text p. 478, and java code on X:
7
Exception Handling Enclose the code to establish a connection and the I/O code, all within a try-catch block.
8
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.
9
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.
10
Three Parts of a Thread A virtual CPU
The code being executed by the CPU The data on which the code works
11
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();
12
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.
13
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. See Example, p. 503.
14
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. Text, p. 496.
15
States of a Thread object
New Runnable Running Blocked Dead Diagram, SL275,
16
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.
17
“Control” of Threads Testing threads Accessing thread priority
isAlive() Accessing thread priority getPriority() setPriority() [may not work, depending on o/s] Putting threads on hold sleep() join() yield() SL275,
18
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.
19
Time to Yield Thread.yield() Thread.join()
Give other runnable threads a chance to run. Thread.join() Causes current thread to wait until the thread on which join() is called terminates.
20
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. Monica and Ryan example.
21
Concurrency Issues - 2 Synchronization can lead to deadlock.
Deadlock: Two processes each hold exclusive access to resources needed by the other. Monica and Ryan example.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.