Download - Socket Programming Part 2
![Page 1: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/1.jpg)
Socket ProgrammingPart 2
Sop, [email protected]
Reference: Daniel SpangenbergerComputer Networks
PPT-4 Socket Programming
![Page 2: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/2.jpg)
TopicsConcurrency Review
Why should we use …? Our experiences?
Socket Programming related System requirement(robustness…) How to cooperate with socket API?
Example How to modify the example to fit further
requirement?
![Page 3: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/3.jpg)
Clients
User 1
(goes to lunch)
Server
A scenario…connect()
accept()
fgets()
User 2read()
connect()
Blocked!
Blocks!
![Page 4: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/4.jpg)
How did we add concurrency?
Processes Uses fork() Easy to understand(actually we have implemented
one version!) A lot to consider about causing complexity(zombie,
syscall…)Threads
Natural concurrency (new thread per connection) Easier to understand (you know it already) Complexity is increased (possible race conditions)
Use non-blocking I/O Uses select() Explicit control flow (no race conditions!) Explicit control flow more complicated though
![Page 5: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/5.jpg)
Multi-processFork()
Use Pid to verify different process Assign different task flow accordingly
Signal & waitpid(…) Tracing child processes, kill the zombies
Other process control methods?
![Page 6: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/6.jpg)
Multi-threadpthread_create
Create thread according to detailed settings
Pthread(series: join, detach, cancel…) Imply different polices.
Other thread control methods?
![Page 7: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/7.jpg)
I/O MultiplexingMonitor sockets with select()
int select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout);
So what’s an fd_set? Bit vector with FD_SETSIZE bits
maxfd – Max file descriptor + 1readfs – Bit vector of read descriptors to monitorwritefds – Bit vector of write descriptors to monitorexceptfds – Read the manpage, set to NULLtimeout – How long to wait with no activity before returning, NULL for eternity
![Page 8: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/8.jpg)
So what about bit vectors?void FD_ZERO(fd_set *fdset);
Clears all the bitsvoid FD_SET(int fd, fd_set *fdset);
Sets the bit for fdvoid FD_CLR(int fd, fd_set *fdset);
Clears the bit for fdint FD_ISSET(int fd, fd_set *fdset);
Checks whether fd’s bit is set
![Page 9: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/9.jpg)
Robustness?Requirements
Mass users User Experience
Incidents Server/Network/Client breakdown? Hacking? …
![Page 10: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/10.jpg)
What happened if…?ServerClient(s)
socket()
connect()
write()
read()
close()
socket()
bind()
listen()
select()
write()
read()
close()
read()
Hacking! Hacking!!!
Exceptions here!
Server breakdown!
FD_ISSET(sfd)
accept()
check_clients() main loop
…
![Page 11: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/11.jpg)
Solutions?Socket API offers variable settings to meet different demands. (methods settings)
Programming concurrency with different detailed settings .
Exception/ error cases handling
![Page 12: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/12.jpg)
Socket setting exampleAddress re-use
Non-blocking
int sock, opts;sock = socket(…);// getting the current optionssetsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts));
// getting current optionsif (0 > (opts = fcntl(sock, F_GETFL)))
printf(“Error…\n”);// modifying and applyingopts = (opts | O_NONBLOCK);if (fcntl(sock, F_SETFL, opts))
printf(“Error…\n”);bind(…);
![Page 13: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/13.jpg)
Server Example
An easy model.
![Page 14: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/14.jpg)
Code – part 1struct sockaddr_in saddr, caddr;int sockfd, clen, isock;unsigned short port = 80;if (0 > (sockfd=socket(AF_INET, SOCK_STREAM, 0)))
printf(“Error creating socket\n”);
memset(&saddr, '\0', sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(INADDR_ANY); saddr.sin_port = htons(port);
if (0 > (bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)))
printf(“Error binding\n”);if (listen(sockfd, 5) < 0) { // listen for incoming connections
printf(“Error listening\n”);clen = sizeof(caddr)
![Page 15: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/15.jpg)
Code – part 2// Setup your read_set with FD_ZERO and the server socket descriptorwhile (1) {
pool.ready_set = &pool.read_set;pool.nready = select(pool.maxfd+1, &pool.ready_set,
&pool.write_set, NULL, NULL);if (FD_ISSET(sockfd, &pool.ready_set)) {
if (0 > (isock = accept(sockfd, (struct sockaddr *) &caddr, &clen)))
printf(“Error accepting\n”);add_client(isock, &caddr, &pool);
}check_clients(&pool);
}// close it up down here
![Page 16: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/16.jpg)
Change to an IRC server?
Your suggestions?
![Page 17: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/17.jpg)
Internet Relay Chat Network
Architecture
![Page 18: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/18.jpg)
What was pool?A struct something like this:typedef struct s_pool {
int maxfd; // largest descriptor in setsfd_set read_set; // all active read descriptorsfd_set write_set; // all active write descriptorsfd_set ready_set; // descriptors ready for readingint nready; // return of select()
int clientfd[FD_SETSIZE]; // max index in client array
// might want to write thisread_buf client_read_buf[FD_SETSIZE];
// what else might be helpful for project 1?} pool;
![Page 19: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/19.jpg)
Commands to Implement
Basic Commands NICK USER QUIT
Channel Commands JOIN PART LIST
Advanced Commands PRIVMSG WHO
![Page 20: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/20.jpg)
Back to that lifecycle…ServerClient(s)
socket()
connect()
write()
read()
close()
socket()
bind()
listen()
select()
write()
read()
close()
read()EOF
Connection Request
Client / Server Session(s)
FD_ISSET(sfd)
accept()
check_clients() main loop
![Page 21: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/21.jpg)
SuggestionsStrong I/O skills will be a great assistant.
Read more about this field if interested Books as ‘UNIX Network Programming – The Sockets
Networking API’ will be a good tutorial
![Page 22: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/22.jpg)
Project relatedDeadline: 2011-4-10 Checkpoint: 2011-3-27Detailed information will be put on the FTP:ftp://10.132.141.33/classes/08/102 计算机网络 /PROJECT/project 1
![Page 23: Socket Programming Part 2](https://reader035.vdocuments.us/reader035/viewer/2022062302/56816767550346895ddc4b99/html5/thumbnails/23.jpg)
Thanks !Questions?