Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------------------------- CLIENT ----------------------------------------------
- // Un client trimite unui server doua numere.
- // Serverul va returna clientului suma celor doua numere.
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <stdlib.h>
- int main(int argc, char* argvs[]) {
- int c,l;
- struct sockaddr_in server;
- uint16_t a, b, r, suma = 0;
- char ip[20];
- strcpy(ip,argvs[1]);
- int port = atoi(argvs[2]);
- //VERIFICARI-------------------
- if(port<1024 || port>65535){
- printf("Portul nu convine!\n");
- return 1;
- }
- char verif[]="0123456789.";
- int i=0;
- for(i=0;i<strlen(ip);i++){
- if(strchr(verif,ip[i])==0){
- printf("Ip-ul nu convine!\n");
- return 1; //eroare
- }
- }
- //VERIFICARI------------------
- c = socket(AF_INET, SOCK_DGRAM, 0);
- if (c < 0) {
- printf("Eroare la crearea socketului client\n");
- return 1;
- }
- memset(&server, 0, sizeof(server));
- server.sin_port = htons(port);
- server.sin_family = AF_INET;
- server.sin_addr.s_addr = inet_addr(ip);
- scanf("%hu",&a); a=htons(a);
- sendto(c, &a, sizeof(a), 0, (struct sockaddr *) &server, sizeof(server));
- l = sizeof(server);
- scanf("%hu",&b);b=htons(b);
- sendto(c, &b, sizeof(b), 0, (struct sockaddr *) &server, sizeof(server));
- recvfrom(c, &suma, sizeof(suma), MSG_WAITALL, (struct sockaddr *) &server, &l);
- suma=ntohs(suma);
- printf("Suma este: %hu\n",suma);
- close(c);
- }
- //---------------------------------------------- SERVER ----------------------------------------------
- // Un client trimite unui server doua numere.
- // Serverul va returna clientului suma celor doua numere.
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- void deservire_client(int s,struct sockaddr_in client){
- }
- int main(int argc, char* argvs[]) {
- int s;
- struct sockaddr_in server, client;
- int l;
- s = socket(AF_INET, SOCK_DGRAM, 0);
- //VERIFICARE --------
- int port = atoi(argvs[1]);
- if(port<1024 || port>65535){
- printf("Portul nu convine!\n");
- return 1;
- }
- //VERIFICARE --------
- if (s < 0) {
- printf("Eroare la crearea socketului server\n");
- return 1;
- }
- memset(&server, 0, sizeof(server));
- server.sin_port = htons(port);
- server.sin_family = AF_INET;
- server.sin_addr.s_addr = INADDR_ANY;
- if (bind(s, (struct sockaddr *) &server, sizeof(server)) < 0) {
- printf("Eroare la bind\n");
- return 1;
- }
- while(1){
- l = sizeof(client);
- memset(&client, 0, sizeof(client));
- uint16_t a,b,suma;
- int l = sizeof(client);
- recvfrom(s, &a, sizeof(a), MSG_WAITALL, (struct sockaddr *) &client, &l);
- a=ntohs(a);
- recvfrom(s, &b, sizeof(b), MSG_WAITALL, (struct sockaddr *) &client, &l);
- b=ntohs(b);
- suma = a + b; suma=htons(suma);
- sendto(s, &suma, sizeof(suma), 0, (struct sockaddr *) &client, sizeof(client));
- }
- close(s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement