Network/Socket Programming in Java: Rajkumar Buyya
Network/Socket Programming in Java: Rajkumar Buyya
in Java
Rajkumar Buyya
Elements of C-S Computing
t
es
qu
Re
Client
Server
Network
Re
su
lt
Client machine
Server machine
java.net
n Used to manage:
c URL streams
c Client/server sockets
c Datagrams
Part III - Networking
ServerSocket(1234)
Output/write stream
Input/read stream
Socket(“128.250.25.158”, 1234)
Server_name: “manjira.cs.mu.oz.au” 4
Server side Socket Operations
1. Open Server Socket:
ServerSocket server;
DataOutputStream os;
DataInputStream is;
server = new ServerSocket( PORT );
2. Wait for Client Request:
Socket client = server.accept();
3. Create I/O streams for communicating to clients
is = new DataInputStream( client.getInputStream() );
os = new DataOutputStream( client.getOutputStream() );
4. Perform communication with client
Receiive from client: String line = is.readLine();
Send to client: os.writeBytes("Hello\n");
5. Close sockets: client.close();
For multithreade server:
while(true) {
i. wait for client requests (step 2 above)
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.
}
Client side Socket Operations
import java.net.*;
import java.io.*;
public class ASimpleServer {
public static void main(String args[]) {
// Register service on port 1234
ServerSocket s = new ServerSocket(1234);
Socket s1=s.accept(); // Wait and accept a connection
// Get a communication stream associated with the socket
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream (s1out);
// Send a string!
dos.writeUTF(“Hi there”);
// Close the connection, but not the server socket
dos.close();
s1out.close();
s1.close();
}
} 7
A simple client (simplified code)
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[]) throws IOException {
// Open your connection to a server, at port 1234
Socket s1 = new Socket("130.63.122.1",1234);
// Get an input file handle from the socket and read the input
InputStream s1In = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1In);
String st = new String (dis.readUTF());
System.out.println(st);
// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();
}
}
8
Echo Server Client..
//client.java: client interface to server
import java.io.*;
import java.net.*;
public class client
{
int port_id;
String server; Socket slink;
DataOutputStream os;
DataInputStream is;
DataInputStream kbd;
public client( String args[] )
{
server = args[0];
port_id = Integer.valueOf(args[1]).intValue();
try
{
slink = new Socket( server, port_id );
os = new DataOutputStream( slink.getOutputStream() );
is = new DataInputStream( slink.getInputStream() );
kbd = new DataInputStream( System.in );
}
Echo Server Client..
catch( UnknownHostException e )
{
System.err.println( "Don't know about host: " );
System.exit(1);
}
catch( IOException e )
{
System.err.println( "Could not get I/O for the
connection to "+server);
System.exit(1);
}
}
void communicate()
{
while(true)
{
try {
System.out.print("Enter Input <end to stop>: ");
Echo Server Client..
if( line.equals("end") )
{ os.close(); is.close(); slink.close();
break;
}
String line2 = is.readLine();
System.out.println("Output: "+line2);
}
catch( IOException e )
{ System.out.println(e); }
}
}
public static void main( String [] args )
{
if( args.length < 2 )
{
System.out.println("Usage: java client
server_name port_id" );
System.exit(1);
}
Echo Server ...
// server.java: echo server
import java.io.*;
import java.net.*;
public class server
{
// public final static int PORT = 4779;
public static void main( String [] args )
{
ServerSocket server = null;
DataOutputStream os = null;
DataInputStream is = null;
boolean shutdown = false;
if( args.length < 1 )
{
System.out.println( "Usage: java server
port_num" );
System.exit( 1 );
}
int PORT = Integer.valueOf(args[0]).intValue();
Echo Server ...
catch( IOException e )
{
System.err.println( "Could not get I/O for the
connection to: ");
}
while(!shutdown)
{
if( server != null )
{
try
{
Socket client = server.accept();
System.out.println("Connected");
InetAddress cip = client.getInetAddress();
System.out.println( "Client IP Addr:
"+cip.toString());
is = new DataInputStream(
client.getInputStream() );
os = new DataOutputStream(
Echo Server ...
if( line.startsWith("end" ) )
{
shutdown = true;
break;
}
os.writeBytes(line.toUpperCase());
os.writeBytes("\n");
System.out.println(line);
}
is.close(); client.close();
}
catch( UnknownHostException e )
{
System.err.println( "Server Open fails" );
}
catch( IOException e )
{
System.err.println( "Could not get I/O for the connection
Echo Server
Server Process
Client Process
Server
Threads
Client Process
User Mode
Kernel Mode
Message Passing
Facility
Client/Server Computing
Rajkumar Buyya
Client Server Definition
t
es
qu
Re
Client
Server
Network
Re
su
lt
Client machine
Server machine
Where Operations are Done
n In client-server computing
major focus is on
SOFTWARE
Application Tasks
User
UserInterface
Interface
Presentation
PresentationLogic
Logic
Application
ApplicationLogic
Logic
Data
DataRequests
Requests&
&Results
Results
Physical
PhysicalData
DataManagement
Management
Client (dumb) - Server Model
e
Server
ok
Client
str
ey
K
Presentation Logic
Network
Di Application Logic
sp
lay
s
DBMS
True Client-Server Model
e
Server
ok
Client
str
ey
K
Application Logic
Presentation Logic Network
Pr
o
Re ces
DBMS
su sed
lts
Distributed Client-Server Model
Server
rie ed
Client
ue ss
Q oce
s
Pr
Application Logic
Application Logic
Network
Pr
o
Re ces
DBMS
Presentation Logic su sed
lts
n Client-server computing is distributed
access, not a distributed computing.
RPC Look and Feel like Local Calls
arguments
arguments
results
results
calling results=
procedure bar(arguments)
(client) client stub server stub
network transport
arguments
network transport
results
request message
request message
reply message
reply message
called
procedure
(client)
Network
Local Procedure Call Remote Procedure Call
Flow Control in a Sychronous RPC
Client
Program
RPC Call
Invoke Service
Service Executes
with Request Service Call
Client
Waiting
Netw
ork
return() answer
return ( )
reply Request Completed
Server Process
Client Process
Server
Threads
Client Process
User Mode
Kernel Mode
Message Passing
Facility
Categories of Servers
n File Server
n Data Server
n Compute Server
n Database Server
n Communication Server
n Video Server
File Server
F multiple slots
F fast processor to translate
networking protocols
Internet Server
PC client
Internet Server
Local Area
Network
UNIX workstations
Distributed processing
application connects to remote
database
SQL*
Forms
SQL *Net
TCP/IP
UNIX Server
SQL *
Forms
ORACL
E
SQL *Net
TCP/IP
ORACLE
Database Configurations
Client-Server Waves
Ethernet era Intergalactic era
client/server client/server
Database
servers
File Distributed
servers re
w a
u p objects
gro TPitor
s
n
mo
TP
NOS monitor
Directory Security Distributed file