Java Unit Viii
-
Upload
chandrasekhar-uchiha -
Category
Documents
-
view
215 -
download
0
Transcript of Java Unit Viii
-
8/3/2019 Java Unit Viii
1/11
Basics of network programming
IP Address
32-bit identifier
Dotted-quad: 192.118.56.25 Identifies a host
192.118.56.25
-
8/3/2019 Java Unit Viii
2/11
Ports
Identifying the specific application
IP addresses identify hosts
Host has many applications Ports (16-bit identifier)
192.18.22.13
Port 80 25 23
Application WWW E-mail Telnet
-
8/3/2019 Java Unit Viii
3/11
Sockets Socket is an object used for network programming.
A socket is bound to a specific port number
Network communication using Sockets is very much
similar to performing file I/O
The streams used in file I/O operation are also
applicable to socket-based I/O
-
8/3/2019 Java Unit Viii
4/11
Socket Communication
A server (program) runs on a specific
computer and has a socket that is bound
to a specific port. The server waits andlistens to the socket for a client to make a
connection request.
serverClient
Connection requestport
-
8/3/2019 Java Unit Viii
5/11
Contd..
If everything goes well, the server accepts theconnection. Upon acceptance, the server gets a newsocket bounds to a different port. It needs a new socket(consequently a different port number) so that it can
continue to listen to the original socket for connectionrequests while serving the connected client.
server
Client
Connection
p
ort
port port
-
8/3/2019 Java Unit Viii
6/11
Client-Server communication
socket()
bind()
listen()
accept()
read()
write()
Server
block
processrequest
Client
socket()
connect()
write()
read()
-
8/3/2019 Java Unit Viii
7/11
Javas .net package
Javas .net package provides two classes:
Socket for implementing a client
ServerSocket for implementing a server
-
8/3/2019 Java Unit Viii
8/11
Implementing a Server1. Open the ServerSocket:
ServerSocket server;
DataOutputStream os;
DataInputStream is;
server = new ServerSocket( PORT );
2. Wait for the Client Request:
Socket s = server.accept();
3. Create I/O streams for communicating to the client
is = new DataInputStream( s.getInputStream() );
os = new DataOutputStream( s.getOutputStream() );
4. Perform communication with client
Receive from client: String line = is.readLine();
Send to client: os.write ("Hello\n");
5. Close sockets: client.close();For multithreaded 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.
}
-
8/3/2019 Java Unit Viii
9/11
Implementing a Client
1. Create a Socket Object:client = new Socket( server, port_id );
2. Create I/O streams for communicating with the server.
is = new DataInputStream(client.getInputStream() );
os = new DataOutputStream( client.getOutputStream() );
3. Perform I/O or communication with the server: Receive data from the server:
String line = is.readLine(); Send data to the server:
os.write ("Hello\n");4. Close the socket when done:client.close();
-
8/3/2019 Java Unit Viii
10/11
Simple client-serever program
A simple server (simplified code)
// SimpleServer.java:asimpleserverprogram
importjava.net.*;
importjava.io.*;
publicclassSimpleServer {
publicstaticvoidmain(Stringargs[])throws IOException {
// Registerserviceonport 1234ServerSocketserver = newServerSocket(1234);
Sockets=server.accept(); // Waitandacceptaconnection
// Getacommunicationstreamassociatedwiththesocket
OutputStreamsout = s.getOutputStream();
DataOutputStreamdos = newDataOutputStream(sout);
// Sendastring!
dos.write("Hithere");
// Closetheconnection,butnottheserversocket
dos.close();
s1out.close();
s1.close();
}
}
-
8/3/2019 Java Unit Viii
11/11
A simple client (simplified code)
// SimpleClient.java:asimpleclientprogram
importjava.net.*;
importjava.io.*;
publicclassSimpleClient {
publicstaticvoidmain(Stringargs[])throws IOException {
// Open yourconnectiontoaserver,atport 1234Socketclient = newSocket(server_IP",1234);
// Getaninput filehandle fromthesocketandreadtheinput
InputStreamsIn = client.getInputStream();
DataInputStreamdis = newDataInputStream(sIn);
Stringst = newString(dis.read());
System.out.println(st);
// Whendone,justclosetheconnectionandexit
dis.close();
sIn.close();
client.close();
}
}