Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Отчет Шайхенуров Роман #61

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added report.pdf
Binary file not shown.
253 changes: 253 additions & 0 deletions report.tex

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions simple_tcp/client_linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 2.8)
project(client_linux)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")

set(SOURCE_FILES main.c)
add_executable(client_linux ${SOURCE_FILES})
Binary file added simple_tcp/client_linux/clientv3
Binary file not shown.
93 changes: 93 additions & 0 deletions simple_tcp/client_linux/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <stdio.h>
#include <stdlib.h>

#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <signal.h>

int sockfd;

void closeApp() {
printf("Closing socket\r\n");
shutdown(sockfd, SHUT_RDWR);
close(sockfd);
exit(0);
}

int main(int argc, char *argv[]) {
signal(SIGINT,closeApp);
int n;
uint16_t portno;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];

if (argc < 3) {
fprintf(stderr, "usage %s hostname port\n", argv[0]);
exit(0);
}

portno = (uint16_t) atoi(argv[2]);

/* Create a socket point */
sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}

server = gethostbyname(argv[1]);

if (server == NULL) {
fprintf(stderr, "ERROR, no such host\n");
exit(0);
}

bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy(server->h_addr, (char *) &serv_addr.sin_addr.s_addr, (size_t) server->h_length);
serv_addr.sin_port = htons(portno);

/* Now connect to the server */
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR connecting");
exit(1);
}

/* Now ask for a message from the user, this message
* will be read by server
*/
while(1){
printf("Please enter the message: ");
bzero(buffer, sizeof(buffer));
fgets(buffer, sizeof(buffer)-1, stdin);
if (strstr(buffer, "\\q") != NULL) {
closeApp();
}
/* Send message to the server */
n = write(sockfd, buffer, strlen(buffer));


if (n <= 0) {
perror("ERROR writing to socket");
closeApp();
}

/* Now read server response */
bzero(buffer, 256);
n = read(sockfd, buffer, 255);

if (n <= 0) {
perror("ERROR reading from socket");
closeApp();
}

printf("%s\n", buffer);
}
return 0;
}
10 changes: 10 additions & 0 deletions simple_tcp/server_linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 2.8)
project(server_linux)
find_package (Threads)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")

set(SOURCE_FILES main.c)
add_executable(server_linux ${SOURCE_FILES})

target_link_libraries (server_linux ${CMAKE_THREAD_LIBS_INIT})
151 changes: 151 additions & 0 deletions simple_tcp/server_linux/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#include <stdio.h>
#include <stdlib.h>

#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <string.h>

#define OPEN 0
#define CLOSED 1

void *connection_f ();
void *connect_handler(void* args);

int sockfd;

int max_clients = 2;
int all_clients = 0;
int isClose = OPEN;

void clear_all(){
isClose = CLOSED;
printf("closing server\r\n");
while (all_clients != 0){
printf("Waiting for closing clients... \r\n");
sleep(1);
}
printf("Closing server...\n");
shutdown(sockfd, SHUT_RDWR);
close(sockfd);
}

void deleteConnection(int newsockfd){
printf("Closing socket = %d \r\n", newsockfd);
shutdown(newsockfd, SHUT_RDWR);
close(newsockfd);
all_clients--;
pthread_exit(0);
}
int sockfd;

int main(int argc, char *argv[]) {
//CTRL+C
signal(SIGINT, clear_all);

uint16_t portno;
struct sockaddr_in serv_addr;


/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd <= 0) {
perror("ERROR opening socket");
exit(1);
}

/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 5001;

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);

/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}

/* Making new thread for messaging with client */
pthread_t thread; //thread
int result; //result of thread creating
result = pthread_create(&thread, NULL, connection_f, NULL); //create new thread
if(result != 0) {
perror("Error while creating thread");
}

char cmd;

while(!isClose){
cmd = getchar();
if (cmd == 'q'){
break;
}
}
clear_all();
return 0;
}

void *connection_f (){
/* Accept actual connection from the client */
struct sockaddr_in cli_addr;
unsigned int clilen;
int newsockfd;
int n;

listen(sockfd, 5);
clilen = sizeof(cli_addr);

pthread_t threadCreation;

while (!isClose) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd <= 0) {
perror("ERROR on accept");
continue;
}
n = pthread_create(&threadCreation, NULL, connect_handler, (void*) newsockfd);
if (n != 0) {
printf("Error on creating client thread");
} else {
all_clients++;
}
}

pthread_exit(0);
}

void *connect_handler (void *args){
int newsockfd = (int*) args;
char buffer[256];
ssize_t n;

/* If connection is established then start communicating */
while(!isClose){
bzero(buffer, sizeof(buffer));
n = read(newsockfd, buffer, sizeof(buffer)); // recv on Windows
if (n <= 0) {
perror("ERROR reading from socket");
deleteConnection(newsockfd);
}
printf("Here is the message: %s\n", buffer);

/* Write a response to the client */
n = write(newsockfd, "I got your message", 18); // send on Windows

if (n <= 0) {
perror("ERROR writing to socket");
deleteConnection(newsockfd);
}
bzero(buffer, 256);

}
deleteConnection(newsockfd);


}
Binary file added simple_tcp/server_linux/serverv3
Binary file not shown.
63 changes: 39 additions & 24 deletions tcp_template/client_linux/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>

#include <pthread.h>
#include <string.h>
#include <signal.h>

int sockfd;

void closeApp() {
printf("Closing socket\r\n");
shutdown(sockfd, SHUT_RDWR);
close(sockfd);
exit(0);
}

int main(int argc, char *argv[]) {
int sockfd, n;
signal(SIGINT,closeApp);
int n;
uint16_t portno;
struct sockaddr_in serv_addr;
struct hostent *server;
Expand Down Expand Up @@ -51,28 +62,32 @@ int main(int argc, char *argv[]) {
/* Now ask for a message from the user, this message
* will be read by server
*/

printf("Please enter the message: ");
bzero(buffer, 256);
fgets(buffer, 255, stdin);

/* Send message to the server */
n = write(sockfd, buffer, strlen(buffer));

if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}

/* Now read server response */
bzero(buffer, 256);
n = read(sockfd, buffer, 255);

if (n < 0) {
perror("ERROR reading from socket");
exit(1);
while(1){
printf("Please enter the message: ");
bzero(buffer, sizeof(buffer));
fgets(buffer, sizeof(buffer)-1, stdin);
if (strstr(buffer, "\\q") != NULL) {
closeApp();
}
/* Send message to the server */
n = write(sockfd, buffer, strlen(buffer));


if (n <= 0) {
perror("ERROR writing to socket");
closeApp();
}

/* Now read server response */
bzero(buffer, 256);
n = read(sockfd, buffer, 255);

if (n <= 0) {
perror("ERROR reading from socket");
closeApp();
}

printf("%s\n", buffer);
}

printf("%s\n", buffer);
return 0;
}
5 changes: 4 additions & 1 deletion tcp_template/server_linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
cmake_minimum_required(VERSION 2.8)
project(server_linux)
find_package (Threads)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")

set(SOURCE_FILES main.c)
add_executable(server_linux ${SOURCE_FILES})
add_executable(server_linux ${SOURCE_FILES})

target_link_libraries (server_linux ${CMAKE_THREAD_LIBS_INIT})
Loading