Skip to content

Commit 9a8d8fe

Browse files
author
Roman Loskutov
committed
first commit
0 parents  commit 9a8d8fe

File tree

8 files changed

+176
-0
lines changed

8 files changed

+176
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
client
2+
server
3+
*~
4+
*.swp
5+
*.swo
6+

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
```
2+
chmod a+x c.sh
3+
chmod a+x s.sh
4+
./c.sh
5+
./s.sh
6+
./server
7+
```
8+
9+
Then in new terminal:
10+
11+
```
12+
./client
13+
```
14+

c.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gcc client.c erproc.c -W -Wall -Werror -o client

client.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <sys/types.h>
2+
#include <sys/socket.h>
3+
#include <netinet/in.h>
4+
#include <arpa/inet.h>
5+
#include <stdlib.h>
6+
#include <stdio.h>
7+
#include <unistd.h>
8+
#include "erproc.h"
9+
10+
int main() {
11+
int fd = Socket(AF_INET, SOCK_STREAM, 0);
12+
struct sockaddr_in adr = {0};
13+
adr.sin_family = AF_INET;
14+
adr.sin_port = htons(34543);
15+
Inet_pton(AF_INET, "127.0.0.1", &adr.sin_addr);
16+
Connect(fd, (struct sockaddr *) &adr, sizeof adr);
17+
write(fd, "Hello\n", 6);
18+
char buf[256];
19+
ssize_t nread;
20+
nread = read(fd, buf, 256);
21+
if (nread == -1) {
22+
perror("read failed");
23+
exit(EXIT_FAILURE);
24+
}
25+
if (nread == 0) {
26+
printf("EOF occured\n");
27+
}
28+
write(STDOUT_FILENO, buf, nread);
29+
sleep(10);
30+
close(fd);
31+
return 0;
32+
}

erproc.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "erproc.h"
2+
#include <sys/types.h>
3+
#include <sys/socket.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <unistd.h>
7+
#include <arpa/inet.h>
8+
9+
int Socket(int domain, int type, int protocol) {
10+
int res = socket(domain, type, protocol);
11+
if (res == -1) {
12+
perror("socket failed");
13+
exit(EXIT_FAILURE);
14+
}
15+
return res;
16+
}
17+
18+
void Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
19+
int res = bind(sockfd, addr, addrlen);
20+
if (res == -1) {
21+
perror("bind failed");
22+
exit(EXIT_FAILURE);
23+
}
24+
}
25+
26+
void Listen(int sockfd, int backlog) {
27+
int res = listen(sockfd, backlog);
28+
if (res == -1) {
29+
perror("listen failed");
30+
exit(EXIT_FAILURE);
31+
}
32+
}
33+
34+
int Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
35+
int res = accept(sockfd, addr, addrlen);
36+
if (res == -1) {
37+
perror("accept failed");
38+
exit(EXIT_FAILURE);
39+
}
40+
return res;
41+
}
42+
43+
void Connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
44+
int res = connect(sockfd, addr, addrlen);
45+
if (res == -1) {
46+
perror("connect failed");
47+
exit(EXIT_FAILURE);
48+
}
49+
}
50+
51+
void Inet_pton(int af, const char *src, void *dst) {
52+
int res = inet_pton(af, src, dst);
53+
if (res == 0) {
54+
printf("inet_pton failed: src does not contain a character"
55+
" string representing a valid network address in the specified"
56+
" address family\n");
57+
exit(EXIT_FAILURE);
58+
}
59+
if (res == -1) {
60+
perror("inet_pton failed");
61+
exit(EXIT_FAILURE);
62+
}
63+
}
64+
65+

erproc.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef ERPROC_H
2+
#define ERPROC_H
3+
4+
#include <sys/types.h>
5+
#include <sys/socket.h>
6+
7+
int Socket(int domain, int type, int protocol);
8+
9+
void Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
10+
11+
void Listen(int sockfd, int backlog);
12+
13+
int Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
14+
15+
void Connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
16+
17+
void Inet_pton(int af, const char *src, void *dst);
18+
19+
#endif
20+

s.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gcc server.c erproc.c -W -Wall -Werror -o server

server.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <sys/types.h>
2+
#include <sys/socket.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <netinet/in.h>
6+
#include <arpa/inet.h>
7+
#include <unistd.h>
8+
#include "erproc.h"
9+
10+
int main() {
11+
int server = Socket(AF_INET, SOCK_STREAM, 0);
12+
struct sockaddr_in adr = {0};
13+
adr.sin_family = AF_INET;
14+
adr.sin_port = htons(34543);
15+
Bind(server, (struct sockaddr *) &adr, sizeof adr);
16+
Listen(server, 5);
17+
socklen_t adrlen = sizeof adr;
18+
int fd = Accept(server, (struct sockaddr *) &adr, &adrlen);
19+
ssize_t nread;
20+
char buf[256];
21+
nread = read(fd, buf, 256);
22+
if (nread == -1) {
23+
perror("read failed");
24+
exit(EXIT_FAILURE);
25+
}
26+
if (nread == 0) {
27+
printf("END OF FILE occured\n");
28+
}
29+
write(STDOUT_FILENO, buf, nread);
30+
write(fd, buf, nread);
31+
32+
sleep(15);
33+
34+
close(fd);
35+
close(server);
36+
return 0;
37+
}

0 commit comments

Comments
 (0)