Network Programming and Java Sockets Agenda: Internet Applications Serving Local and Remote Users
Network Programming and Java Sockets Agenda: Internet Applications Serving Local and Remote Users
Implementing a Server
Grid Computing and Distributed Systems (GRIDS) Laboratory
Implementing a Client
Dept. of Computer Science and Software Engineering Sample Examples
University of Melbourne, Australia Conclusions
http://www.cs.mu.oz.au/~raj or http://www.buyya.com
1 2
3 4
Applications Layer
TCP/IP Stack
a client, a server, and network
Standard apps
HTTP
FTP
Telnet Application
st
ue
Transport Layer
Client Transport
TCP
Server
Sockets
su
IP Link
Client machine
Device drivers
7 8
TCP/IP Stack
TCP/IP Stack
Protocol) is a Protocol) is a protocol
connection-oriented Application
that sends independent Application
protocol that provides a (http,ftp,telnet,…)
packets of data, called (http,ftp,telnet,…)
reliable flow of data datagrams, from one
Transport Transport
between two computers. computer to another with
(TCP, UDP,..) (TCP, UDP,..)
HTTP arrival.
(IP,..) (IP,..)
FTP
Example applications:
Link Link
Telnet
Clock server
(device driver,..) (device driver,..)
Ping
9 10
ftp 21/tcp
computer.
telnet 23/tcp
app app app app
smtp 25/tcp
login 513/tcp
port port port port
Network communication using Sockets is very much computer and has a socket that is bound
similar to performing file I/O to a specific port. The server waits and
In fact, socket handle is treated like file handle.
listens to the socket for a client to make a
port
socket program. server
Client
13 14
server
Client
port Connection
Receive data from the server: // Get a communication stream associated with the socket
OutputStream s1out = s1.getOutputStream();
String line = is.readLine(); DataOutputStream dos = new DataOutputStream (s1out);
21 22
Throws:
System.out.println("IOException: " + ioe);
23 24
Multithreaded Server: For Serving
Server in Loop: Always up
Multiple Clients Concurrently
// SimpleServerLoop.java: a simple server program that runs forever in a single thead
import java.net.*;
import java.io.*;
public class SimpleServerLoop {
public static void main(String args[]) throws IOException {
// Register service on port 1234 Server Process
ServerSocket s = new ServerSocket(1234);
Client 1 Process
while(true)
{
Server
Socket s1=s.accept(); // Wait and accept a connection
Threads
// Get a communication stream associated with the socket
OutputStream s1out = s1.getOutputStream();
Internet
DataOutputStream dos = new DataOutputStream (s1out);
// Send a string!
dos.writeUTF("Hi there");
// Close the connection, but not the server socket Client 2 Process
dos.close();
s1out.close();
s1.close();
}
}
}
25 26
Conclusion
Keywords:
27