Skip to content

Commit b10f0d7

Browse files
authored
Add files via upload
1 parent e54a1ab commit b10f0d7

File tree

10 files changed

+104
-0
lines changed

10 files changed

+104
-0
lines changed

Socket/Client/bin/semaphore

20.7 KB
Binary file not shown.

Socket/Client/bin/socket_client

20.6 KB
Binary file not shown.

Socket/Client/inc/main.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <sys/socket.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <arpa/inet.h>
7+
8+
#define PORT 8080

Socket/Client/obj/main.o

9.7 KB
Binary file not shown.

Socket/Client/src/main.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "main.h"
2+
3+
int main(int argc, char *argv[])
4+
{
5+
int sock_1, v_read;
6+
struct sockaddr_in address;
7+
char buffer[1024] = {0};
8+
char *hey = "Hey I am client!";
9+
10+
if ((sock_1 = socket(AF_INET, SOCK_STREAM, 0)) < 0)
11+
{
12+
perror("socket creation failure!\n");
13+
return -1;
14+
}
15+
16+
address.sin_family = AF_INET;
17+
address.sin_port = htons( PORT );
18+
19+
if (inet_pton(AF_INET, "127.0.0.1", &address.sin_addr) <= 0)
20+
{
21+
printf("Invalid IP Address\n");
22+
return -1;
23+
}
24+
25+
if (connect(sock_1, (struct sockaddr *)&address, sizeof(address)) < 0)
26+
{
27+
printf("connection failure!\n");
28+
return -1;
29+
}
30+
send(sock_1, hey, strlen(hey), 0);
31+
printf("Hello msg is sent: \n");
32+
v_read = read(sock_1, buffer, 1024);
33+
printf("buffer content: %s\n", buffer);
34+
35+
}

Socket/Server/bin/semaphore

20.7 KB
Binary file not shown.

Socket/Server/bin/socket_server

20.9 KB
Binary file not shown.

Socket/Server/inc/main.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <sys/socket.h>
4+
#include <stdlib.h>
5+
#include <netinet/in.h>
6+
#include <string.h>
7+
8+
#define PORT 8080

Socket/Server/obj/main.o

10.4 KB
Binary file not shown.

Socket/Server/src/main.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "main.h"
2+
3+
int main(int argc, char *argv[])
4+
{
5+
int serv_fd, n_socket, v_read;
6+
struct sockaddr_in address;
7+
8+
int opt = 1;
9+
int addr_length = sizeof(address);
10+
char buffer[1024] = {0};
11+
12+
char *hey = "Hey I am server!";
13+
14+
if ((serv_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
15+
{
16+
perror("socket failure!\n");
17+
exit(0);
18+
}
19+
20+
if (setsockopt(serv_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
21+
{
22+
perror("setsockopt error\n");
23+
exit(0);
24+
}
25+
26+
address.sin_family = AF_INET;
27+
address.sin_addr.s_addr = INADDR_ANY;
28+
address.sin_port = htons( PORT );
29+
30+
if (bind(serv_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
31+
{
32+
perror("bind failure\n");
33+
exit(0);
34+
}
35+
36+
if (listen(serv_fd, 3) < 0)
37+
{
38+
perror("listen error\n");
39+
exit(0);
40+
}
41+
42+
if ((n_socket = accept(serv_fd, (struct sockaddr *)&address, (socklen_t *)&addr_length)) < 0)
43+
{
44+
perror("accept error\n");
45+
exit(0);
46+
}
47+
48+
v_read = read(n_socket, buffer, 1024);
49+
printf("buffer content: %s\n", buffer);
50+
send(n_socket, hey, strlen(hey), 0);
51+
printf("Hello msg is sent: \n");
52+
53+
}

0 commit comments

Comments
 (0)