-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpClient.c
More file actions
executable file
·59 lines (49 loc) · 1.86 KB
/
Copy pathtcpClient.c
File metadata and controls
executable file
·59 lines (49 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* Lab Sistemas Distribuidos - Prof. Fernando W. Cruz */
/* Calculadora distribuida TCP */
/* Arquivo: tcpClient.c */
/* ***************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_SIZE 80
int main(int argc,char * argv[]) {
struct sockaddr_in ladoServ; /* contem dados do servidor */
int sd; /* socket descriptor */
int n,k; /* num caracteres lidos do servidor */
char bufout[MAX_SIZE]; /* buffer de dados enviados */
char bufin[MAX_SIZE]; /* buffer de dados enviados */
/* confere o numero de argumentos passados para o programa */
if(argc != 4) {
printf("uso correto: %s <ip_do_servidor> <porta_do_servidor> <operacao sem espaco (ex: 3+4) ou usando aspas ex:('50 / 2') > \n", argv[0]);
exit(1);
}
memset((char *)&ladoServ,0,sizeof(ladoServ)); /* limpa estrutura */
memset((char *)&bufout,0,sizeof(bufout)); /* limpa buffer */
ladoServ.sin_family = AF_INET; /* config. socket p. internet*/
ladoServ.sin_addr.s_addr = inet_addr(argv[1]);
ladoServ.sin_port = htons(atoi(argv[2]));
/* Cria socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd < 0) {
fprintf(stderr, "Criacao do socket falhou!\n");
exit(1);
}
/* Conecta socket ao servidor definido */
if (connect(sd, (struct sockaddr *)&ladoServ, sizeof(ladoServ)) < 0) {
fprintf(stderr,"Tentativa de conexao falhou!\n");
exit(1);
}
send(sd,argv[3],strlen(argv[3]),0);
memset(&bufin, 0x0, sizeof(bufin));
n = recv(sd, &bufin, sizeof(bufin),0);
printf("Soma: %s\n", bufin);
//printf("------- encerrando conexao com o servidor -----\n");
close (sd);
return (0);
} /* fim do programa */