0% found this document useful (0 votes)
27 views14 pages

L3 - Networking

Uploaded by

Heng Sovannarith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views14 pages

L3 - Networking

Uploaded by

Heng Sovannarith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Networking in Java

Client/Server Computing
◼ Communication over the network often
occurs between a client and a server
◼ A client connects to a server and then sends
and receives messages
◼ A server waits for connection requests from
clients, accepts them, and then responds to
their messages
◼ TCP/IP: abstract layer that simplifies the
above activities
Networking I
Slide 2
Hosts, Ports, and Sockets
◼ Host – a computer uniquely identified by hostname
and/or IP Address
◼ hostname: a String name (e.g., [Link])
◼ IP address: a set of 4 bytes (e.g., [Link])
◼ Type ipconfig or winipcfg to check your address
◼ Port – a number that specifies the type of connection you
want to make
◼ e.g., port 80 is for HTTP (web protocol),
port 23 is for Telnet (logging in to a UNIX account), etc.
◼ each server can listen to many ports
◼ each port can accommodate many clients
◼ Socket – object that encapsulates the communication
process
◼ hides the details of how things work Networking I
Slide 3
Networking in Java
◼ [Link] package
◼ import [Link].*;
◼ Most important classes
◼ Socket
◼ ServerSocket
◼ URL (discussed earlier)
◼ InetAddress

Networking I
Slide 4
The Socket class
◼ Constructor takes the host and port that the client wants
to connect to
◼ Useful Socket methods
◼ InputStream getInputStream()
◼ OutputStream getOutputStream()
◼ Given these Streams, you can send/receive data
◼ Writing to OutputStream sends the data to the other host
◼ To read the data, the other host must read from the InputStream
◼ You can/should chain other “filter” streams to these before using
them – just like you did for files (e.g., you can use BufferedReader,
etc.)
◼ You can use Serialization too! (Useful for your projects!)

Networking I
Slide 5
The ServerSocket class
◼ Misnomer
◼ it is-NOT-a Socket
◼ doesn’t extend Socket, and doesn’t have the same methods
◼ actually, it creates sockets, in response to a client connection
◼ maybe better called “ConnectionListener” or “SocketServer”
◼ Constructor takes port number that the server wishes to
listen to
◼ Call the accept() method to accept a client
◼ Thread that calls accept() blocks (waits or “sleeps”) until a client
connects
◼ Thread continues after client connects. The accept() method then
returns a Socket object connected to the client
Networking I
Slide 6
On the Server Program ...
◼ Create a ServerSocket object
◼ specify port
◼ Invoke accept() on that object
◼ Obtain Socket object
◼ returned by accept()
◼ Obtain I/O streams from the socket
◼ carry out communication using these streams

Networking I
Slide 7
On the Client Program ...
◼ Create Socket object
◼ specify host and port of server
◼ Obtain I/O streams from the socket
◼ carry out communication using these streams

* execute the server before the client

Networking I
Slide 8
A Simple Example myserver
ss
“Hello” out Socket connection in “Hello”
“Hi” in port out “Hi”
established
s 8888 s
// create ServerSocket, specify port
// Try to connect to server and port ServerSocket ss = new ServerSocket( 8888 );
// client thread waits here
// until server accepts // server thread waits here for client
Socket s = new Socket( “myserver”, 8888 ); Socket s = [Link]();

// get streams to allow us to read // get streams to allow us to read


// and write to client // and write to client
Scanner in = new Scanner( Scanner in = new Scanner(
new InputStreamReader( [Link]() ) new InputStreamReader( [Link]() )
); );
PrintStream out = new PrintStream( PrintStream out = new PrintStream(
[Link]() ); [Link]() );

// read and write to server // read and write to client


[Link]( “Hello” ); String t = [Link]();
[Link](); [Link]( t );
String t = [Link](); [Link]( “Hi” );
[Link]( t ); [Link]();

Networking I
Slide 9
Exercise ...
◼ Look at the Sample Code
◼ Run it with your partner on 2 machines
◼ Note: if you only have 1 machine, you can still test
your program as follows:
◼ open 2 DOS windows
◼ run the server in one window
◼ in the 2nd window, run the client and connect to either
“localhost” or “[Link]”
◼ this will make the machine connect to itself
◼ Do the ServerChat and ClientChat Exercise Networking I
Slide 10
A Simple Example myserver
ss
“Hello” out Socket connection in “Hello”
“Hi” in port out “Hi”
established
s 8888 s
// create ServerSocket, specify port
// Try to connect to server and port ServerSocket ss = new ServerSocket( 8888 );
// client thread waits here
// until server accepts // server thread waits here for client
Socket s = new Socket( “myserver”, 8888 ); Socket s = [Link]();

// get streams to allow us to read // get streams to allow us to read


// and write to client // and write to client
Scanner in = new Scanner( Scanner in = new Scanner(
new InputStreamReader( [Link]() ) new InputStreamReader( [Link]() )
); );
PrintStream out = new PrintStream( PrintStream out = new PrintStream(
[Link]() ); [Link]() );

// read and write to server // read and write to client


[Link]( “Hello” ); String t = [Link]();
[Link](); [Link]( t );
String t = [Link](); [Link]( “Hi” );
[Link]( t ); [Link]();

Networking I
Slide 11
A Simple Example…
◼ The previous slide demonstrates a very
simple way of making a client and server
program send and receive data once.
◼ What happens if you create a GUI for one
of these programs?
◼ Also, how do you make the client and
server simultaneously send and receive
data? Handle multiple connections?
Networking I
Slide 12
Handling Multiple Connections
◼ Use Threads
◼ Main Thread
◼ Have a loop that continuously calls accept()
◼ Create and start a new “connection thread” whenever
accept() returns a socket
◼ Connection Thread
◼ Thread for talking with 1 client (only)
◼ read, write client via socket’s input and output streams
◼ may have a loop, reading “requests” from client and
responding
Networking I
Slide 13
Multiple Clients while ( true )
{
myserver
ss Socket s = [Link]();
main CThread ct = new CThread( s );

connection thread
[Link]();
port }
thread connection
8888
public void run()
{
connection thread
// get input streams thread Notes:
// read/write client
◼ main thread just
}
// loop
s s accepts
s ◼ create and start 1
new thread per
client

client
client
client

s
s

s
Networking I
Slide 14

You might also like