Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9758b3d

Browse files
author
Anton Yarkov
committed
Code cleanup.
1 parent 0ce0afd commit 9758b3d

File tree

4 files changed

+31
-30
lines changed

4 files changed

+31
-30
lines changed

‎networking/01 Multiprocess tcp-servers/Simple prefork network interaction/client.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <strings.h>
4-
#include <sys/types.h> /*для совместимости */
4+
#include <sys/types.h> // For compatibility
55
#include <sys/socket.h>
6-
#include <arpa/inet.h> // Для преобразования htonl, htons, ntohl, ntohs
7-
#include <netinet/in.h> // для sockaddr_in
6+
#include <arpa/inet.h> // For convertions htonl, htons, ntohl, ntohs
7+
#include <netinet/in.h> // For sockaddr_in
88
#include <fcntl.h>
99
#include <netdb.h>
10-
#include <unistd.h> // Для close()
11-
//#include <errno> - не используем, иногда может содержать ошибку другого процесса
10+
#include <unistd.h> // For close()
11+
//#include <errno> - don't use it because sometimes it may contains an error from another process
1212

1313
#define EXIT_FAILURE 1
1414
#define PORTNUM 1500 // Port > 1024 because program will not work not as root.
@@ -18,13 +18,13 @@ void main(int argc, char *argv[])
1818
struct hostent *hp;
1919
if ((hp = gethostbyname(argv[1])) == 0)
2020
{
21-
perror("Error of calling gethostbyname"); /* или strerror */
21+
perror("Error of calling gethostbyname"); /* or use strerror */
2222
exit(EXIT_FAILURE);
2323
}
2424

2525
struct sockaddr_in serv_addr;
26-
bzero(&serv_addr, sizeof(serv_addr)); // устаревшая, TODO use memset
27-
bcopy(hp->h_addr_list[0], &serv_addr.sin_addr, hp->h_length); // устаревшая, TODO use memcpy, memmove
26+
bzero(&serv_addr, sizeof(serv_addr)); // depricated function! TODO use memset
27+
bcopy(hp->h_addr_list[0], &serv_addr.sin_addr, hp->h_length); // depricated function! TODO use memcpy, memmove
2828
serv_addr.sin_family = hp->h_addrtype;
2929
serv_addr.sin_port = htons(PORTNUM);
3030

@@ -54,8 +54,8 @@ void main(int argc, char *argv[])
5454

5555
printf("Received from server: %s\n", buf);
5656

57-
// Если был вызыван fork, то дескриптор будет доступен и в процессе родителя и child-процессе.
58-
// Соединение разрывается только когда закрыты все дескрипторы, связанные с сокетом.
57+
// If fork was called then descriptor will be available in both parent- and child-processes.
58+
// Connection is closed only after closing both descriptions linked to socket.
5959
close(sockfd);
6060
printf("Client off!\n\n");
6161
}

‎networking/01 Multiprocess tcp-servers/Simple prefork network interaction/server.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <strings.h>
4-
#include <sys/types.h> // для совместимости
4+
#include <sys/types.h> // For compatibility
55
#include <sys/socket.h>
6-
#include <arpa/inet.h> // Для преобразования htonl, htons, ntohl, ntohs
6+
#include <arpa/inet.h> // For convertions htonl, htons, ntohl, ntohs
77
#include <fcntl.h>
88
#include <netdb.h>
9-
#include <unistd.h> // Для close()
10-
//#include <errno> - не используем, иногда может содержать ошибку другого процесса
9+
#include <unistd.h> // For close()
10+
//#include <errno> - don't use it because sometimes it may contains an error from another process
11+
12+
// Task:
13+
// Create an example of interaction algorithm between client and prefork TCP server
1114

1215
#define EXIT_FAILURE 1
1316
#define PORTNUM 1500 // Port > 1024 because program will not work not as root.
@@ -18,7 +21,7 @@ void main()
1821
int sockfd = -1;
1922
if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
2023
{
21-
perror("Error of calling socket"); /* или strerror */
24+
perror("Error of calling socket"); /* or use strerror */
2225
exit(EXIT_FAILURE);
2326
}
2427

@@ -32,7 +35,7 @@ void main()
3235

3336
// Set address
3437
struct sockaddr_in serv_addr;
35-
bzero(&serv_addr, sizeof(serv_addr)); // устаревшая, TODO use memset
38+
bzero(&serv_addr, sizeof(serv_addr)); // depticated function! TODO use memset
3639
serv_addr.sin_family = AF_INET;
3740
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); // 0.0.0.0
3841

@@ -63,7 +66,7 @@ void main()
6366
{
6467
socklen_t addrlen;
6568

66-
bzero(&clnt_addr, sizeof(clnt_addr)); // устаревшая, TODO use memset
69+
bzero(&clnt_addr, sizeof(clnt_addr)); // depticated function! TODO use memset
6770
addrlen = sizeof(clnt_addr);
6871

6972
if ((ns = accept(sockfd, (struct sockaddr *)&clnt_addr, &addrlen)) == -1)
@@ -97,8 +100,8 @@ void main()
97100
exit(0);
98101
}
99102

100-
// Если был вызыван fork, то дескриптор будет доступен и в процессе родителя и child-процессе.
101-
// Соединение разрывается только когда закрыты все дескрипторы, связанные с сокетом.
103+
// If fork was called then descriptor will be available in both parent- and child-processes.
104+
// Connection is closed only after closing both descriptions linked to socket.
102105
close(ns);
103106

104107
printf("Server off! To change this behavior, look in code!\n\n");

‎networking/01 Multiprocess tcp-servers/prefork_server.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void main()
2424
int sockfd = -1;
2525
if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
2626
{
27-
perror("Error of calling socket"); /* или strerror */
27+
perror("Error of calling socket"); /* or use strerror */
2828
exit(EXIT_FAILURE);
2929
}
3030

@@ -73,8 +73,8 @@ void main()
7373
switch (pid = fork())
7474
{
7575
case -1:
76-
perror("Error of calling fork");/* произошла ошибка */
77-
exit(EXIT_FAILURE); /*выход из родительского процесса*/
76+
perror("Error of calling fork");
77+
exit(EXIT_FAILURE);
7878
case 0:
7979
{
8080
int cpid = getpid();
@@ -103,8 +103,8 @@ void main()
103103
sleep(1);
104104
}
105105

106-
// Если был вызыван fork, то дескриптор будет доступен и в процессе родителя и child-процессе.
107-
// Соединение разрывается только когда закрыты все дескрипторы, связанные с сокетом.
106+
// Descriptor continues to be available in parent process after fork() call.
107+
// We need to close all descriptors linked to socket.
108108
close(ns);
109109
printf("CHILD %d: Exit!\n", cpid);
110110
exit(0);

‎networking/01 Multiprocess tcp-servers/process-per-request-server.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ void child_zombie_handler(int sig)
3232
{
3333
printf("No more child processes.\n", done);
3434
break;
35-
//if (errno == ECHILD)
36-
// break; // no more child processes
3735
}
3836
else
3937
{
@@ -60,7 +58,7 @@ void main()
6058
int sockfd = -1;
6159
if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
6260
{
63-
perror("Error of calling socket"); /* или strerror */
61+
perror("Error of calling socket"); /* or use strerror */
6462
exit(EXIT_FAILURE);
6563
}
6664

@@ -78,7 +76,7 @@ void main()
7876
serv_addr.sin_family = AF_INET;
7977
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); // 0.0.0.0 (INADDR_LOOPBACK - 127.0.0.1)
8078

81-
// Change bytes order to network'
79+
// Change bytes order to network
8280
serv_addr.sin_port = htons((int)PORTNUM);
8381

8482
// Link socket with address
@@ -98,7 +96,7 @@ void main()
9896
exit(EXIT_FAILURE);
9997
}
10098

101-
// О появлении зомби ядро уведомляет родительский процесс сигналом SIGCHLD. http://habrahabr.ru/post/141206/
99+
// Core notifies parent process about Zombie appearing by signal SIGCHLD.
102100
struct sigaction act;
103101
act.sa_handler = child_zombie_handler;
104102
sigemptyset(&act.sa_mask);
@@ -157,7 +155,7 @@ void main()
157155
}
158156

159157
// If fork was called then descriptor will be available in both parent- and child-processes.
160-
// Connection is closed only after closing both descriptions linked to socket.кетом.
158+
// Connection is closed only after closing both descriptions linked to socket.
161159
close(ns);
162160

163161
--counter;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /