-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
|
||
} |