Różnice między wybraną wersją a wersją aktualną.
Poprzednia rewizja po obu stronach Poprzednia wersja Nowa wersja | Poprzednia wersja | ||
studia:magisterskie:1sem:systemy_rownolegle_i_rozproszone [2016/05/19 10:26] 149.156.112.6 [Client C] |
studia:magisterskie:1sem:systemy_rownolegle_i_rozproszone [2016/05/19 11:13] (aktualna) 149.156.112.6 [tmp/opencl_mat_transp/mat_transp/mat_transp.c] |
||
---|---|---|---|
Linia 2780: | Linia 2780: | ||
}</code> | }</code> | ||
+ | ==== Kod wogu ==== | ||
+ | === server.c ==== | ||
+ | <code c> | ||
+ | /* | ||
+ | C socket server example, handles multiple clients using threads | ||
+ | Compile | ||
+ | gcc server.c -lpthread -o server | ||
+ | */ | ||
+ | |||
+ | #include<stdio.h> | ||
+ | #include<string.h> //strlen | ||
+ | #include<stdlib.h> //strlen | ||
+ | #include<sys/socket.h> | ||
+ | #include<arpa/inet.h> //inet_addr | ||
+ | #include<unistd.h> //write | ||
+ | #include<pthread.h> //for threading , link with lpthread | ||
+ | #include <unistd.h> | ||
+ | |||
+ | //the thread function | ||
+ | void *connection_handler(void *); | ||
+ | |||
+ | int main(int argc , char *argv[]) | ||
+ | { | ||
+ | int counter = 0; | ||
+ | pid_t pid = fork(); | ||
+ | |||
+ | if (pid == 0) | ||
+ | { | ||
+ | int sock; | ||
+ | struct sockaddr_in server; | ||
+ | char message[1000] , server_reply[2000]; | ||
+ | int counter; | ||
+ | unsigned int ip[] = { 192, 168, 102, 1 }; | ||
+ | char* ip_final; | ||
+ | char *buf; | ||
+ | size_t sz; | ||
+ | |||
+ | //Create socket | ||
+ | |||
+ | for ( counter = 0; counter < 255; ++counter ) { | ||
+ | |||
+ | sock = socket(AF_INET , SOCK_STREAM , 0); | ||
+ | if (sock == -1) | ||
+ | { | ||
+ | printf("Could not create socket"); | ||
+ | } | ||
+ | puts("Socket created"); | ||
+ | |||
+ | //printf("192.168.102.%d", counter); | ||
+ | |||
+ | sz = snprintf(NULL, 0, "192.168.102.%d", counter); | ||
+ | buf = (char *)malloc(sz + 1); /* make sure you check for != NULL in real code */ | ||
+ | | ||
+ | server.sin_addr.s_addr = inet_addr(buf); | ||
+ | server.sin_family = AF_INET; | ||
+ | server.sin_port = htons( 65001 ); | ||
+ | |||
+ | //Connect to remote server | ||
+ | if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) | ||
+ | { | ||
+ | perror("connect failed. Error"); | ||
+ | continue; | ||
+ | } | ||
+ | |||
+ | puts("Connected\n"); | ||
+ | |||
+ | //keep communicating with server | ||
+ | while(1) | ||
+ | { | ||
+ | //Send some data | ||
+ | if( send(sock , "Werset 6-client" , 15 , 0) < 0) | ||
+ | { | ||
+ | puts("Send failed"); | ||
+ | return 1; | ||
+ | } | ||
+ | |||
+ | //Receive a reply from the server | ||
+ | if( recv(sock , server_reply , 2000 , 0) < 0) | ||
+ | { | ||
+ | puts("recv failed"); | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | puts(server_reply); | ||
+ | |||
+ | break; | ||
+ | } | ||
+ | |||
+ | close(sock); | ||
+ | } | ||
+ | } | ||
+ | else if (pid > 0) | ||
+ | { | ||
+ | int socket_desc , client_sock , c; | ||
+ | struct sockaddr_in server , client; | ||
+ | |||
+ | //Create socket | ||
+ | socket_desc = socket(AF_INET , SOCK_STREAM , 0); | ||
+ | if (socket_desc == -1) | ||
+ | { | ||
+ | printf("Could not create socket"); | ||
+ | } | ||
+ | puts("Socket created"); | ||
+ | |||
+ | //Prepare the sockaddr_in structure | ||
+ | server.sin_family = AF_INET; | ||
+ | server.sin_addr.s_addr = INADDR_ANY; | ||
+ | server.sin_port = htons( 65001 ); | ||
+ | |||
+ | //Bind | ||
+ | if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) | ||
+ | { | ||
+ | //print the error message | ||
+ | perror("bind failed. Error"); | ||
+ | return 1; | ||
+ | } | ||
+ | puts("bind done"); | ||
+ | |||
+ | //Listen | ||
+ | listen(socket_desc , 3); | ||
+ | |||
+ | //Accept and incoming connection | ||
+ | puts("Waiting for incoming connections..."); | ||
+ | c = sizeof(struct sockaddr_in); | ||
+ | |||
+ | |||
+ | //Accept and incoming connection | ||
+ | puts("Waiting for incoming connections..."); | ||
+ | c = sizeof(struct sockaddr_in); | ||
+ | pthread_t thread_id; | ||
+ | |||
+ | while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) ) | ||
+ | { | ||
+ | puts("Connection accepted"); | ||
+ | |||
+ | if( pthread_create( &thread_id , NULL , connection_handler , (void*) &client_sock) < 0) | ||
+ | { | ||
+ | perror("could not create thread"); | ||
+ | return 1; | ||
+ | } | ||
+ | |||
+ | //Now join the thread , so that we dont terminate before the thread | ||
+ | //pthread_join( thread_id , NULL); | ||
+ | puts("Handler assigned"); | ||
+ | } | ||
+ | |||
+ | if (client_sock < 0) | ||
+ | { | ||
+ | perror("accept failed"); | ||
+ | return 1; | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | // fork failed | ||
+ | printf("fork() failed!\n"); | ||
+ | return 1; | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * This will handle connection for each client | ||
+ | * */ | ||
+ | void *connection_handler(void *socket_desc) | ||
+ | { | ||
+ | //Get the socket descriptor | ||
+ | int sock = *(int*)socket_desc; | ||
+ | int read_size; | ||
+ | char *message , client_message[2000]; | ||
+ | |||
+ | //Receive a message from client | ||
+ | while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 ) | ||
+ | { | ||
+ | printf("%s\n", client_message); | ||
+ | |||
+ | //end of string marker | ||
+ | client_message[read_size] = '\0'; | ||
+ | |||
+ | //Send the message back to client | ||
+ | write(sock , "Werset 6" , 8); | ||
+ | |||
+ | //clear the message buffer | ||
+ | memset(client_message, 0, 2000); | ||
+ | } | ||
+ | |||
+ | if(read_size == 0) | ||
+ | { | ||
+ | puts("Client disconnected"); | ||
+ | fflush(stdout); | ||
+ | } | ||
+ | else if(read_size == -1) | ||
+ | { | ||
+ | perror("recv failed"); | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | }</code> | ||
+ | === Makefile === | ||
+ | <code bash># kompilator c | ||
+ | CC = gcc | ||
+ | |||
+ | # konsolidator | ||
+ | CFLAGS=-g | ||
+ | |||
+ | all: server client | ||
+ | |||
+ | server: server.o | ||
+ | $(CC) $(LFLAGS) server.o -lpthread -o server | ||
+ | |||
+ | server.o: server.c | ||
+ | $(CC) -c server.c -o server.o | ||
+ | |||
+ | client: client.o | ||
+ | $(CC) client.o -o client | ||
+ | |||
+ | client.o: client.c | ||
+ | $(CC) -c client.c -o client.o | ||
+ | |||
+ | clean: | ||
+ | rm -f *.o</code> | ||
===== Zajęcia 9 ===== | ===== Zajęcia 9 ===== | ||
<code c>/* | <code c>/* | ||
Linia 2871: | Linia 3093: | ||
int read_size; | int read_size; | ||
char *message , client_message[2000]; | char *message , client_message[2000]; | ||
- | + | ||
- | //Send some messages to the client | + | |
- | message = "Greetings! I am your connection handler\n"; | + | |
- | write(sock , message , strlen(message)); | + | |
- | + | ||
- | message = "Now type something and i shall repeat what you type \n"; | + | |
- | write(sock , message , strlen(message)); | + | |
- | + | ||
//Receive a message from client | //Receive a message from client | ||
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 ) | while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 ) | ||
{ | { | ||
- | //end of string marker | + | //end of string marker |
- | client_message[read_size] = '\0'; | + | client_message[read_size] = '\0'; |
- | + | ||
- | //Send the message back to client | + | //Send the message back to client |
- | write(sock , client_message , strlen(client_message)); | + | write(sock , "Werset 6" , strlen(client_message)); |
- | + | ||
- | //clear the message buffer | + | //clear the message buffer |
- | memset(client_message, 0, 2000); | + | memset(client_message, 0, 2000); |
} | } | ||
Linia 2966: | Linia 3181: | ||
return 0; | return 0; | ||
}</code> | }</code> | ||
+ | |||
+ | |||
+ | ==== Kod Maciek Gonzo itp ==== | ||
+ | <code c> | ||
+ | #include<stdio.h> | ||
+ | #include<string.h> //strlen | ||
+ | #include<stdlib.h> //strlen | ||
+ | #include<sys/socket.h> | ||
+ | #include<arpa/inet.h> //inet_addr | ||
+ | #include<unistd.h> //write | ||
+ | #include<pthread.h> //for threading , link with lpthread | ||
+ | |||
+ | //the thread function | ||
+ | void *connection_handler(void *); | ||
+ | |||
+ | void *client_function(void *socket_desc){ | ||
+ | int sock; | ||
+ | struct sockaddr_in server_id; | ||
+ | char message[1000] , server_reply[2000]; | ||
+ | |||
+ | //Create socket | ||
+ | sock = socket(AF_INET , SOCK_STREAM , 0); | ||
+ | if (sock == -1) | ||
+ | { | ||
+ | printf("Could not create socket"); | ||
+ | } | ||
+ | puts("Socket created"); | ||
+ | // 192.168.102.63 wogu | ||
+ | server_id.sin_addr.s_addr = inet_addr("localhost"); | ||
+ | server_id.sin_family = AF_INET; | ||
+ | server_id.sin_port = htons( 65000 ); | ||
+ | |||
+ | //Connect to remote server | ||
+ | |||
+ | if (connect(sock , (struct sockaddr *)&server_id , sizeof(server_id)) < 0) | ||
+ | { | ||
+ | perror("connect failed. Error"); | ||
+ | |||
+ | }else{ | ||
+ | |||
+ | puts("Connected\n"); | ||
+ | |||
+ | //keep communicating with server | ||
+ | while(1) | ||
+ | { | ||
+ | printf("Enter message : "); | ||
+ | scanf("%s" , message); | ||
+ | |||
+ | //Send some data | ||
+ | if( send(sock , message , strlen(message) , 0) < 0) | ||
+ | { | ||
+ | puts("Send failed"); | ||
+ | |||
+ | } | ||
+ | |||
+ | //Receive a reply from the server | ||
+ | if( recv(sock , server_reply , 2000 , 0) < 0) | ||
+ | { | ||
+ | puts("recv failed"); | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | puts("Server reply :"); | ||
+ | puts(server_reply); | ||
+ | } | ||
+ | |||
+ | close(sock); | ||
+ | } | ||
+ | |||
+ | }; | ||
+ | |||
+ | |||
+ | void *server_function(void * sock){ | ||
+ | printf("SERVER Start"); | ||
+ | fflush(stdout); | ||
+ | int socket_desc , client_sock , c; | ||
+ | pthread_t thread_id; | ||
+ | struct sockaddr_in server , client; | ||
+ | |||
+ | //Create socket | ||
+ | socket_desc = socket(AF_INET , SOCK_STREAM , 0); | ||
+ | if (socket_desc == -1) | ||
+ | { | ||
+ | printf("Could not create socket"); | ||
+ | } | ||
+ | puts("Socket created"); | ||
+ | |||
+ | //Prepare the sockaddr_in structure | ||
+ | server.sin_family = AF_INET; | ||
+ | server.sin_addr.s_addr = INADDR_ANY; | ||
+ | server.sin_port = htons( 65000); | ||
+ | |||
+ | //Bind | ||
+ | if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) | ||
+ | { | ||
+ | //print the error message | ||
+ | perror("bind failed. Error"); | ||
+ | return; | ||
+ | } | ||
+ | puts("bind done"); | ||
+ | |||
+ | |||
+ | |||
+ | //Listen | ||
+ | listen(socket_desc , 3); | ||
+ | |||
+ | //Accept and incoming connection | ||
+ | puts("Waiting for incoming connections..."); | ||
+ | c = sizeof(struct sockaddr_in); | ||
+ | fflush(stdout); | ||
+ | |||
+ | while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) ) | ||
+ | { | ||
+ | puts("Connection accepted"); | ||
+ | |||
+ | if( pthread_create( &thread_id , NULL , connection_handler , (void*) &client_sock) < 0) | ||
+ | { | ||
+ | perror("could not create thread"); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | //Now join the thread , so that we dont terminate before the thread | ||
+ | //pthread_join( thread_id , NULL); | ||
+ | puts("Handler assigned"); | ||
+ | } | ||
+ | |||
+ | if (client_sock < 0) | ||
+ | { | ||
+ | perror("accept failed"); | ||
+ | return; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | int main(int argc , char *argv[]) | ||
+ | { | ||
+ | pthread_t thread_id; | ||
+ | //Create server | ||
+ | if( pthread_create( &thread_id , NULL , server_function , NULL) != 0) | ||
+ | { | ||
+ | perror("could not create server thread"); | ||
+ | fflush(stdout); | ||
+ | return 1; | ||
+ | } | ||
+ | pthread_t thread_id2; | ||
+ | |||
+ | // Create client | ||
+ | if( pthread_create( &thread_id2 , NULL , client_function , NULL) != 0) | ||
+ | { | ||
+ | perror("could not create client thread"); | ||
+ | fflush(stdout); | ||
+ | return 1; | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * This will handle connection for each client | ||
+ | * */ | ||
+ | void *connection_handler(void *socket_desc) | ||
+ | { | ||
+ | //Get the socket descriptor | ||
+ | int sock = *(int*)socket_desc; | ||
+ | int read_size; | ||
+ | char *message , client_message[2000]; | ||
+ | |||
+ | //Receive a message from client | ||
+ | while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 ) | ||
+ | { | ||
+ | //end of string marker | ||
+ | client_message[read_size] = '\0'; | ||
+ | |||
+ | //Send the message back to client | ||
+ | write(sock , "Werset 5" , strlen(client_message)); | ||
+ | |||
+ | //clear the message buffer | ||
+ | memset(client_message, 0, 2000); | ||
+ | } | ||
+ | |||
+ | if(read_size == 0) | ||
+ | { | ||
+ | puts("Client disconnected"); | ||
+ | fflush(stdout); | ||
+ | } | ||
+ | else if(read_size == -1) | ||
+ | { | ||
+ | perror("recv failed"); | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </code> |