0% found this document useful (0 votes)
42 views

Network Programming and Java Sockets Agenda: Internet Applications Serving Local and Remote Users

The document provides an agenda for a presentation on network programming and Java sockets. It includes an introduction to elements of client-server computing and networking basics. It also discusses understanding ports and sockets, implementing servers and clients in Java, and provides sample examples and conclusions. The document outlines topics like introduction to internet applications, increased demand for internet applications, elements of client-server computing, and networking basics including the TCP/IP stack and protocols like TCP and UDP.

Uploaded by

Dhivakar Arun
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Network Programming and Java Sockets Agenda: Internet Applications Serving Local and Remote Users

The document provides an agenda for a presentation on network programming and Java sockets. It includes an introduction to elements of client-server computing and networking basics. It also discusses understanding ports and sockets, implementing servers and clients in Java, and provides sample examples and conclusions. The document outlines topics like introduction to internet applications, increased demand for internet applications, elements of client-server computing, and networking basics including the TCP/IP stack and protocols like TCP and UDP.

Uploaded by

Dhivakar Arun
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Agenda

Network Programming and


Java Sockets Introduction
Elements of Client Server Computing
Networking Basics
Understanding Ports and Sockets
Java Sockets
Rajkumar Buyya


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

Internet Applications Serving Local


Introduction
and Remote Users
Recently Internet and WWW have emerged as PC client
global ubiquitous media for communication and
changing the way we conduct science,
engineering, and commerce. Internet
They also changed the way we learn, live, Server
enjoy, communicate, interact, engage, etc. It Local Area Network
appears like the modern life activities are
getting completely centered around the
Internet.
PDA

3 4

Increased demand for Internet


Internet & Web as a delivery Vehicle applications


To take advantage of opportunities presented by


the Internet, businesses are continuously seeking
new and innovative ways and means for offering
their services via the Internet.


This created a huge demand for software


designers with skills to create new Internet-enabled
applications or migrate existing/legacy applications
on the Internet platform.


Object-oriented Java technologies—Sockets,


threads, RMI, clustering, Web services-- have
emerged as leading solutions for creating portable,
efficient, and maintainable large and complex
Internet applications.
5 6
Elements of C-S Computing Networking Basics


Applications Layer 

TCP/IP Stack
a client, a server, and network 

Standard apps


HTTP


FTP


Telnet Application
st
ue

User apps (http,ftp,telnet,…)


q
Re

Transport Layer
Client Transport


TCP
Server 

UDP (TCP, UDP,..)


Network 

Programming Interface: Network


Re 

Sockets
su 

Network Layer (IP,..)


l t 

IP Link
Client machine 

Link Layer (device driver,..)


Server machine


Device drivers

7 8

Networking Basics Networking Basics




TCP (Transport Control 

TCP/IP Stack 

UDP (User Datagram 

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,..)


Example applications: no guarantees about


Network Network


HTTP arrival.
(IP,..) (IP,..)


FTP


Example applications:
Link Link


Telnet 

Clock server
(device driver,..) (device driver,..)


Ping

9 10

Understanding Ports Understanding Ports




The TCP and UDP P 

Port is represented by a positive (16-bit) integer


o TCP
protocols use ports to server
r Client
value
map incoming data to t


Some ports have been reserved to support


a particular process common/well known services:
running on a

ftp 21/tcp
computer.

telnet 23/tcp
app app app app

smtp 25/tcp

login 513/tcp
port port port port


User level process/services generally use port


TCP or UDP
number value >= 1024
Packet
Data port# data
11 12
Sockets Socket Communication


Sockets provide an interface for programming networks

A server (program) runs on a specific


at the transport layer.


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


The streams used in file I/O operation are also applicable to


socket-based I/O connection request.


Socket-based communication is programming language


independent.


That means, a socket program written in Java language can


also communicate to a program written in Java or non-Java
Connection request

port
socket program. server
Client

13 14

Socket Communication Sockets and Java Socket Classes

If everything goes well, the server accepts the

A socket is one endpoint of a two-way


connection. Upon acceptance, the server gets a new
socket bounds to a different port. It needs a new socket communication link between two
(consequently a different port number) so that it can programs running on the network.
continue to listen to the original socket for connection
requests while serving the connected client.

A socket is bound to a port number so


that the TCP layer can identify the
application that data destined to be sent.
port

server

Java’s .net package provides two


classes:
port

Client
port Connection

Socket – for implementing a client


ServerSocket – for implementing a server


15 16

Java Sockets Implementing a Server


Server ServerSocket(1234) 1. Open the Server Socket:
ServerSocket server;
DataOutputStream os;
DataInputStream is;
server = new ServerSocket( PORT );
2. Wait for the Client Request:
Socket client = server.accept();
3. Create I/O streams for communicating to the client
is = new DataInputStream( client.getInputStream() );
os = new DataOutputStream( client.getOutputStream() );
4. Perform communication with client
Receive from client: String line = is.readLine();
Send to client: os.writeBytes("Hello\n");
Output/write stream Client
5. Close sockets: client.close();
For multithreaded server:
Input/read stream while(true) {
i. wait for client requests (step 2 above)
Socket(“128.250.25.158”, 1234) ii. create a thread with “client” socket as parameter (the thread creates streams (as in step
(3) and does communication as stated in (4). Remove thread once service is provided.
It can be host_name like “mandroo.cs.mu.oz.au” 17 } 18
Implementing a Client A simple server (simplified code)
1. Create a Socket Object: // SimpleServer.java: a simple server program
import java.net.*;
client = new Socket( server, port_id ); import java.io.*;
2. Create I/O streams for communicating with the server. public class SimpleServer {
public static void main(String args[]) throws IOException {
is = new DataInputStream(client.getInputStream() );
// Register service on port 1234
os = new DataOutputStream( client.getOutputStream() ); ServerSocket s = new ServerSocket(1234);
3. Perform I/O or communication with the server: Socket s1=s.accept(); // Wait and accept a 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);

Send data to the server: // Send a string!


os.writeBytes("Hello\n"); dos.writeUTF("Hi there");
// Close the connection, but not the server socket
4. Close the socket when done: dos.close();
client.close(); s1out.close();
s1.close();
}
}
19 20

A simple client (simplified code) Run


// SimpleClient.java: a simple client program

Run Server on mundroo.cs.mu.oz.au


import java.net.*;

[raj@mundroo] java SimpleServer &


import java.io.*;
public class SimpleClient {

Run Client on any machine (including mundroo):


public static void main(String args[]) throws IOException {

[raj@mundroo] java SimpleClient


// Open your connection to a server, at port 1234 Hi there
Socket s1 = new Socket("mundroo.cs.mu.oz.au",1234);
// Get an input file handle from the socket and read the input

If you run client when server is not up:


InputStream s1In = s1.getInputStream();

[raj@mundroo] sockets [1:147] java SimpleClient


DataInputStream dis = new DataInputStream(s1In); Exception in thread "main" java.net.ConnectException: Connection refused
String st = new String (dis.readUTF()); at java.net.PlainSocketImpl.socketConnect(Native Method)
System.out.println(st); at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:320)
// When done, just close the connection and exit at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:133)
dis.close(); at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:120)
s1In.close(); at java.net.Socket.<init>(Socket.java:273)
s1.close(); at java.net.Socket.<init>(Socket.java:100)
}
at SimpleClient.main(SimpleClient.java:6)
}

21 22

Socket Exceptions ServerSocket & Exceptions


try { 

public ServerSocket(int port) throws IOException


Socket client = new Socket(host, port);

Creates a server socket on a specified port.


handleConnection(client);

A port of 0 creates a socket on any free port. You can use


} getLocalPort() to identify the (assigned) port on which this
socket is listening.
catch(UnknownHostException uhe) {
System.out.println("Unknown host: " + host);

The maximum queue length for incoming connection


uhe.printStackTrace(); indications (a request to connect) is set to 50. If a connection
indication arrives when the queue is full, the connection is
} refused.
catch(IOException ioe) { 

Throws:
System.out.println("IOException: " + ioe);

IOException - if an I/O error occurs when opening the socket.


ioe.printStackTrace();

SecurityException - if a security manager exists and its


} checkListen method doesn’t allow the operation.

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


Programming client/server applications in


Java is fun and challenging.


Programming socket programming in


Java is much easier than doing it in other
languages such as C.


Keywords:


Clients, servers, TCP/IP, port number,


sockets, Java sockets

27

You might also like