Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
dotai315 authored Mar 7, 2022
1 parent e54a1ab commit b10f0d7
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 0 deletions.
Binary file added Socket/Client/bin/semaphore
Binary file not shown.
Binary file added Socket/Client/bin/socket_client
Binary file not shown.
8 changes: 8 additions & 0 deletions Socket/Client/inc/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>

#define PORT 8080
Binary file added Socket/Client/obj/main.o
Binary file not shown.
35 changes: 35 additions & 0 deletions Socket/Client/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "main.h"

int main(int argc, char *argv[])
{
int sock_1, v_read;
struct sockaddr_in address;
char buffer[1024] = {0};
char *hey = "Hey I am client!";

if ((sock_1 = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket creation failure!\n");
return -1;
}

address.sin_family = AF_INET;
address.sin_port = htons( PORT );

if (inet_pton(AF_INET, "127.0.0.1", &address.sin_addr) <= 0)
{
printf("Invalid IP Address\n");
return -1;
}

if (connect(sock_1, (struct sockaddr *)&address, sizeof(address)) < 0)
{
printf("connection failure!\n");
return -1;
}
send(sock_1, hey, strlen(hey), 0);
printf("Hello msg is sent: \n");
v_read = read(sock_1, buffer, 1024);
printf("buffer content: %s\n", buffer);

}
Binary file added Socket/Server/bin/semaphore
Binary file not shown.
Binary file added Socket/Server/bin/socket_server
Binary file not shown.
8 changes: 8 additions & 0 deletions Socket/Server/inc/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>

#define PORT 8080
Binary file added Socket/Server/obj/main.o
Binary file not shown.
53 changes: 53 additions & 0 deletions Socket/Server/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "main.h"

int main(int argc, char *argv[])
{
int serv_fd, n_socket, v_read;
struct sockaddr_in address;

int opt = 1;
int addr_length = sizeof(address);
char buffer[1024] = {0};

char *hey = "Hey I am server!";

if ((serv_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket failure!\n");
exit(0);
}

if (setsockopt(serv_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
{
perror("setsockopt error\n");
exit(0);
}

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );

if (bind(serv_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
{
perror("bind failure\n");
exit(0);
}

if (listen(serv_fd, 3) < 0)
{
perror("listen error\n");
exit(0);
}

if ((n_socket = accept(serv_fd, (struct sockaddr *)&address, (socklen_t *)&addr_length)) < 0)
{
perror("accept error\n");
exit(0);
}

v_read = read(n_socket, buffer, 1024);
printf("buffer content: %s\n", buffer);
send(n_socket, hey, strlen(hey), 0);
printf("Hello msg is sent: \n");

}

0 comments on commit b10f0d7

Please sign in to comment.