Source : Advanced Network Programming Principles and Tecniques Socket sock = servSock.accept; /*sock is a socket object.*/... out.println"Waiting..."; String msg = in.readLine; Close; i
Trang 1Oẳn Tù Tì Game Report
Trang 26.Demo 2.Model
Trang 31 Overview Game
3
Trang 5Client 2
Request
Response
2.Model
Trang 6 Clients need not be aware of physical location of server
• Mix and match of platforms
Client and server may have heterogeneous platforms
• Scalability
Provides scope for scalability and ease of maintenance
2.Model
Trang 8- Socket:
Sockets are network communication link end-points between two
applications (i.e., server and client).
Source : Advanced Network Programming Principles and Tecniques
8
3 Socket Client-Server
Trang 9- - There are two type of Sockets:
- - Transport layer socket: TCP and UDP
- - Application layer Socket: HTTP, SMTP
- Socket:
Sockets are network communication link end-points between two
applications (i.e., server and client).
Image from: http://bioinfo2.ugr.es/OReillyReferenceLibrary/java/fclass/ch15_js.htm
9
3 Socket Client-Server
Figure The java.net package
Trang 10 TCP Socket :Server Side
IP address, port number, and communication protocol (TCP).
Source : Advanced Network Programming Principles and Tecniques
Socket sock = servSock.accept();
/*sock is a socket object.*/
Trang 11 TCP Socket :Server Side
3 Socket Client-Server
11
BufferedReader in = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
Send and receive data.
out.println("Waiting ");
String msg = in.readLine();
Close;
important parameters:
IP address, port number, and communication protocol (TCP).
Source : Advanced Network Programming Principles and Tecniques
Trang 12 TCP Socket: Client side
IP address, port number, and communication protocol (TCP).
Source : Advanced Network Programming Principles and Tecniques
Trang 13 TCP Socket: Client side
13
3 Socket Client-Server
important parameters:
IP address, port number, and communication protocol (TCP).
Set input and output streams.
BufferedReader in = new BufferedReader
(new InputStreamReader(sock.getInputStream()));
PrintWriter out =new PrintWriter(sock.getOutputStream(), true);
Send and receive data.
out.println("");
String msgIn = in.readLine();
Close the connection
Source : Advanced Network Programming Principles and Tecniques
Trang 14 TCP Socket: Client side
14
3 Socket Client-Server
The packets are guaranteed to arrive (if lost, retransmission occurs) and are received in order at the destination
important parameters:
IP address, port number, and communication protocol (TCP).
Source : Advanced Network Programming Principles and Tecniques
Trang 16IP address, port number, and communication protocol (UDP).
Source : Advanced Network Programming Principles and Tecniques
Trang 17 UDP Socket: Server side
17
3 Socket Client-Server
important parameters:
IP address, port number, and communication protocol (UDP).
Create a datagram socket object.
DatagramSocket dgramSocket = new DatagramSocket(portno);
/*1024 <= portno <= 65535*/
Create a buffer to store the incoming datagrams:
byte[] buffer = new byte[256];
/*-128 <= byte value <= 127*/
Create a datagram packet object for incoming datagrams:
DatagramPacket inPkt = new DatagramPacket(buffer, buffer.length);
Trang 18 UDP Socket: Server side
18
3 Socket Client-Server
important parameters:
IP address, port number, and communication protocol (UDP).
Accept an incoming datagram:
dgramSocket.receive(inPkt);
Retrieve the data from the buffer:
String msgIn = new String(inPkt.getData(), 0, inPkt.getLength());
Get sender’s address and port number from the datagram:
InetAddress cliAddress = inPkt.getAddress();
int cliPort = inPkt.getPort();
Trang 19 UDP Socket: Server side
19
3 Socket Client-Server
important parameters:
IP address, port number, and communication protocol (UDP).
Close the datagram socket:
dgramSocket.close();
msgOut = ("Message out ");
DatagramPacket outPkt = new DatagramPacket(msgOut.getBytes(),msgOut.length(), cliAddress,cliPort);
Send the response datagram:
dgramSocket.send(outPkt);
Trang 20 UDP Socket: Client side
20
3 Socket Client-Server
important parameters:
IP address, port number, and communication protocol (TCP or UDP).
Create a datagram socket object:
DatagramSocket dgramSocket = new DatagramSocket();
BufferedReader userEntry = new BufferedReader(new InputStreamReader(System.in));
String msg = userEntry.readLine();
DatagramPacket outPkt = new DatagramPacket(msg.getBytes(),msg.length(), host, portno);
Trang 21 UDP Socket: Client side
21
3 Socket Client-Server
important parameters:
IP address, port number, and communication protocol (UDP).
Send the response datagram:
dgramSocket.send(outPkt);
Accept an incoming datagram:
dgramSocket.receive(inPkt);
Retrieve the data from the buffer:
String msgIn = new String(inPkt.getData(), 0, inPkt.getLength());
Close the datagram socket:
dgramSocket.close();
Trang 223 Socket Client-Server
Trang 23 Multicast Introduction
https://kb.meraki.com/knowledge_base/multicast-support
3 Socket Client-Server
23 Multi-casting involves one-to-many communications between a sender and a set of receivers
Trang 243 Socket Client-Server
24
-These receivers must belong to a multicast group
- All class D IP addresses are multicast addresses and range from
224.0.0.0 to 235.255.255.255
Multicast Introduction
Trang 25 Multicast introduction
Trang 30Layer – Protocol diagram
Advanced Network Programming - Principles and Techniques – March 2013, P.32
304.Extension
Trang 31Common types of protocols
Transmission Control Protocol (TCP)
User Datagram Protocol (UDP)
Internet Control Message Protocol (ICMP)
Hypertext Transfer Protocol (HTTP - 80)
Post Office Protocol (POP – 109,110)
File Transfer Protocol (FTP – 20,21)
Internet Message Access Protocol (IMAP)
Simple Mail Transport (SMTP – 25)
Secure Shell (SSH – 22)
31
Trang 32Common types of protocols (Cont)
Other instances of high level interaction
protocols are:
• General Inter-ORB Protocol (IIOP)
• Java remote method invocation (RMI)
• Distributed Component Object Model (DCOM)
• SOAP
32
Trang 33User Datagram Protocol
http://www.kashif-ali.com/2011/06/tcp-vs-udp/ (Date: 30 Aug 2014)
33
Trang 34User Datagram Protocol (Cont)
www.opensourceforu.com/2011/11/socket-api-part-4-datagrams/ (Date: 30 Aug 2014)
34
Trang 35User Datagram Protocol (Cont) import java.io.*;
import java.net.*;
class UDPServer
{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData());
System.out.println("RECEIVED: " + sentence);
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
} }
Trang 36User Datagram Protocol (Cont)import java.io.*;
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
String modifiedSentence = new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
36
Trang 37File Transfer Protocol [1]
FTP was developed in 1985 and is still widely used today
The protocol has been first defined in RFC 959, but then
several extensions have been proposed to enhance flexibility and security (RFC 1579, RFC 2228)
FTP works on top of TCP and in general uses port 21
Trang 38File Transfer Protocol [1]
38
Trang 39File Transfer Protocol [1]
Access control commands include:
• USER—indicates the user;
• PASS—indicates the password;
Trang 40File Transfer Protocol [1]
Transfer parameter commands include:
• PORT—publish local data port;
• PASV—server passive (listen);
• TYPE—indicate data representation (A-ASCII, EBCDIC, I-Image, LLocal);
E-• MODE—indicate transfer mode (S-Stream, B-Block, Compressed);
C-• STRU—establish file structure (F-FILE, R-RECORD, PAGE).
Trang 41File Transfer Protocol [1]
Service commands include:
• RETR (REVC) —retrieve file;
• STOR—send and store the file remotely;
• APPE—send file and append;
• DELE—delete the file;
• MKD—make a new directory;
• RMD—remove a directory;
• PWD—print working directory;
• LIST (ls)—list files.
Trang 42File Transfer Protocol [1]
Trang 43File Transfer Protocol [1]
43
Trang 445 Game Actvity
Server
Get ip server
Client
socket = new DatagramSocket( 4445 ) socket = new DatagramSocket( 4446 )
packet = new DatagramPacket( data ,
255.255.255.255 , 4445 ) socket.send(packet) socket.receive(packet)
packet = new DatagramPacket( data ,
ipClient , portClient )
socket.send(packet)
socket.receive(packet)
44
Trang 45Connect server
socketSV = new ServerSocket( 4447 )
socket = new Socket( addrServer , 4447 ) socket = socketSV.accept()
send request ‘ submit name ’
read request send name read request
validation, send list players
Trang 46send ‘ Begin Game ’
read request read request
call frame ‘ Ready ’ call frame ‘Ready’
5 Game Actvity
46
Trang 47read request
determine winner send result
5 Game Actvity
47
Trang 4848
1 Advanced Network Programming Principles and Techniques book
2 Java Socket Programming Examples,
http://cs.lmu.edu/~ray/notes/javanetexamples/, 01/08/2014
3 Broadcasting to Multiple Recipients,
http://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html, 08/08/2014
Trang 4949