Professional Documents
Culture Documents
CHNG 1: CHNG 2: CHNG 3: CHNG 4: GII THIU V TCP/IP THIT K GII THUT CHO CHNG TRNH CLIENT/SERVER LP TRNH MNG TRN CC MI TRUNG PH DNG LP TRNH MNG VI JAVA
Ni dung mn hc(tt)
CHNG 5: CHNG 6: CHNG 7: CHNG 8: LP TRNH WEB CGI LP TRNH WEB VI CC CNG NGH PH BIN
TCP/IP
Applications layer Telnet FTP SMTP DNS SNMP
TCP
UDP
Network
ICMP ARP IP
Ethernet
Token Ring
FDDI
WANs
nh v duy nht mt my Chiu di 32 bit Cu trc IP (netid, hostid), cc my trn mt mng c netid ging nhau. Do NIC cp Cch biu din:
10101100 00011100 172 28 172.28.16.5 00010000 16 00000101 5
9
10
11
g lo
a ic
le -e nd nd
s an tr
network data link physical application transport network data link physical
rt po
12
15
requesting host
surf.eurecom.fr
Lp trnh mng Chng 1
17 gaia.cs.umass.edu
19
Client : l phn t ch ng
Kt ni n server gi yu cu. Ch nhn kt qu tr v v x l kt qu.
20
21
23
internet
host or server
Lp trnh mng Chng 1
host or server
24
26
TCP
setup
create socket, connect to hostid, port=x clientSocket = Socket() send request using clientSocket
Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); }
32
Client
create socket, clientSocket = DatagramSocket() Create, address (hostid, port=x, send datagram request using clientSocket
read request from serverSocket write reply to serverSocket specifying client host address, port umber Lp trnh mng Chng 1
Create input stream Create client socket Translate hostname to IP address using DNS
sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); }
End of while loop, loop back and wait for another datagram
Lp trnh mng Chng 1 37
PC running Explorer
htt
htt
eq pr
st ue
t ht
es pr
nse po
1b. http server a ch www.dit.hcmut.edu.vn ang ch i kt ni port 80, chp nhn kt ni v notifying n client
40
time
5. http client nhn response message cha file html v hin th. Phn tch file html tm cc lin kt 6. Lp li cc bc 1-5 cho mi lin kt(object)
41
42
43
server
401: authorization req. WWW authenticate: usual http request msg + Authorization:line
usual http response msg usual http request msg + Authorization:line usual http response msg
time
45
Browser caches name & password so Lp trnh mng Chng 1 that user does not have to repeatedly enter it.
client
usual http request msg usual http response +
server
Set-cookie: #
usual http request msg
server matches presentedcookie with server-stored info authentication remembering user preferences, previous choices
cookie: #
usual http response msg usual http request msg
cookie: #
usual http response msg
client
http request msg
If-modified-since: <date>
server
object not modified
http response
HTTP/1.0 304 Not Modified
http response
HTTP/1.1 200 OK
Lp trnh mng Chng 1
object modified
<data>
47
user at host
Chuyn file t local n server hoc ly file t server v local. Hot ng theo c ch client/server FTP server chy port 21. Tham kho : RFC 959
Lp trnh mng Chng 1 48
49
51
SMTP SMTP
mail server outgoing message queue user mailbox
SMTP
user agent
user agent
user agent
52
V d v POP3
client commands: user: declare username pass: password server responses +OK -ERR
S: C: S: C: S: C: S: S: S: C: S: S: C: C: S: S: C: C: S:
+OK POP3 server ready user alice +OK pass hungry +OK user successfully logged list 1 498 2 912 . retr 1 <message 1 contents> . dele 1 retr 2 <message 1 contents> . dele 2 quit +OK POP3 server signing off
on
54
55
sin_zero
in_addr
sin_addr; sin_zero[8];
sin_len
sin_family
sin_port
sin_addr
sin_zero
sin_len
sin_family
sin_port
sin_addr
sin_zero
61
62
TCP
UDP IP
65
66
Dng cho chng trnh client thit lp kt ni n server. Tr v 0 nu thnh cng, -1 nu tht bi. Cc thng s :
sockfd: m t socket to bi hm socket() serveraddr:con tr a ch socket ca server serveraddrlen: chiu di ca a ch socket server
Lp trnh mng Chng 2 69
Hm ny dng cho chng trnh server connection-oriented t socket trng thi ch, lng nghe kt ni t pha client. Tr v 0 nu thnh cng, -1 nu tht bi. Cc thng s:
sockfd: m t socket to bi hm socket() backlog: s request c th queued.
70
Chp nhn kt ni t client, to socket mi. Gi tr l mt socket descriptor ca socket mi. Cc thng s :
sockfd: m t socket to bi hm socket() clientaddr:con tr a ch socket ca client kt ni n. clientaddrlen: chiu di ca clientaddr
Lp trnh mng Chng 2 71
c d liu t connection vo b nh. Tr v s bytes c c nu thnh cng, tr v 0 nu khng c d liu, tr v -1 nu tht bi. Cc thng s :
sockfd: m t socket to bi hm socket() buf: con tr n b m lu thng tin c c len: chiu di ca b m
Lp trnh mng Chng 2 72
Ghi d liu t b nh ln connection. Tr v s bytes ghi c nu thnh cng, tr v -1 nu tht bi. Cc thng s :
sockfd: m t socket to bi hm socket() buf: con tr n b m lu thng tin c c len: chiu di ca b m
Lp trnh mng Chng 2 73
sockfd, buf, len: ging cc hm gii thiu fromaddr, fromaddrlen: a ch socket gi n v chiu di.
Lp trnh mng Chng 2 75
77
78
79
82
83
84
87
89
3.2 Cc hm (tt)
a ch socket tng qut:
#include <sys/socket.h> struct sockaddr { short sa_family; char sa_data[14]; };
3.2 Cc hm (tt)
Cu trc a ch my t xa.
struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; #define h_addr h_addr_list[0]; }
Lp trnh mng Chng 3 92
3.2 Cc hm (tt)
To socket:
#include <sys/types.h> #include <sys/socket.h>
int socket(int family, int type, int protocol);
V d to socket:
int sockfd; //To stream socket sockfd = socket(AF_INET,SOCK_STREAM,0); //To datagram socket sockfd = socket(AF_INET,SOCK_DGRAM,0);
Lp trnh mng Chng 3 93
3.2 Cc hm (tt)
Lin kt socket vi a ch socket(ng k)
int bind(int sockfd, struct sockaddr *myaddr, int myaddrlen);
3.2 Cc hm (tt)
Chuyn socket v trng thi ch kt ni.
int listen(int sockfd, int backlog);
3.2 Cc hm (tt)
Hm kt ni n server
int connect(int sockfd, struct sockaddr *servaddr, int *addrlen); V d:
struct sockaddr_in servaddr; bzero((char*)&servaddr,sizeof(myaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(portno); servaddr.sin_addr.s_addr = inet_addr(serverIP); if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) error(Can not connect to server");
Lp trnh mng Chng 3 96
3.2 Cc hm (tt)
Cc hm truyn nhn d liu:
int read(int fd,char *buf, int nbytes); int write(int fd,char *buf,int nbytes); int send(int sockfd, char *buf,int nbytes,int flags); int recv(int sockfd, char *buf,int nbytes,int flags); int sendto(int sockfd, char *buf, int len, int flags, struct sockaddr_in *toaddr, int toaddrlen); int recvfrom(int sockfd, char *buf, int len, int flags, struct sockaddr_in *fromaddr, int fromaddrlen);
97
3.2 Cc hm (tt)
To process con x l tng kt ni:
int fork(void);
int pid; while(1){ newsockfd = accept(sockfd, (struct sockaddr*) &cli_addr, &clilen); if ((pid=fork())==0){ close(sockfd); process(newsockfd); close(newsockfd) exit(0); } close(newsockfd); }
Lp trnh mng Chng 3 98
100
Hm ly thng tin li :
int WSAGetLastError(void);
102
106
//
char message[100]; SOCKADDR_IN addr; addr.sin_family=AF_INET; addr.sin_port=htons(2000); addr.sin_addr.s_addr=htonl(INADDR_ANY); if(bind(s,(LPSOCKADDR)&addr,sizeof(addr))== SOCKET_ERROR){ wsprintf(message, Can not bind socket : %d, WSAGetLastError()); MessageBox(message); return TRUE; }
Lp trnh mng Chng 3 107
109
}
Lp trnh mng Chng 3 110
111
#define BUFSIZE (100) char buf[BUFSIZE]; int nByteRecv; nByteRecv = recv(s,buf, BUFSIZE,0); if (nByteRecv == SOCKET_ERROR) { MessageBox(Error receive data"); return; } //
Lp trnh mng Chng 3 114
115
lstrcpy(buf,Hello World);
nByteSend = sendto(s,buf,lstrlen(buf),0, (LPSOCKADDR)&to,sizeof(to)); if(nByteSend == SOCKET_ERROR ) //
Lp trnh mng Chng 3 116
117
Socket desciptor switch (message) { case WM_USER+1 : OnAccept(); return 1; case WSA_RDCLOSE : if (WSAGETSELECTEVENT(lParam) == FD_READ) Read_Process(wParam);
Lp trnh mng Chng 3 121
122
123
Chy :
java file-name
Lp trnh mng Chng 4 128
129
4.2 V d (tt)
Chng trnh Client
14. BufferedReader keyInput = new BufferedReader(new 15. InputStreamReader(System.in)); 16. System.out.print("Enter sentence to send to server:"); 17. String data = keyInput.readLine(); 18. output.writeBytes(data+"\n"); 19. int recvByte; 20. System.out.print("Data receicved: "); 21. System.out.println(input.readLine()); 22. clientsock.close(); 23. }//main 24. }//class
4.2 V d (tt)
Chng trnh Server
1. //file Server.java 2. import java.net.*; 3. import java.io.*; 4. public class Server{ 5. public static void main(String args[]) throws Exception{ 6. ServerSocket serversock = new ServerSocket(2000); 7. DataOutputStream output;//stream xuat du lieu 8. BufferedReader input;//stream doc du lieu 9. for(;;){ 10. Socket client = serversock.accept(); 11. output = new DataOutputStream( 12. client.getOutputStream());
131
4.2 V d (tt)
Chng trnh Server (tt)
13. input = new BufferedReader(new 14. InputStreamReader(client.getInputStream())); 15. String data = input.readLine(); 16. System.out.println("Recv from client: "+data); 17. output.writeBytes(data+"\n"); 18. output.flush(); 19. }//for 20. } //main 21. }//class
135
136
137
138
int getPort()
Returns the remote port to which this socket is connected.
141
142
144
145
146
147
public URL(String protocol, String host, int port, String file) throws MalformedURLException
Creates a URL object from the specified protocol, host, port number, and file. Specifying a port number of -1 indicates that the URL should use the default port for the protocol.
149