Skip to content
Snippets Groups Projects
Commit 172cf00a authored by Ramiro Facundo Lorenzo Rodriguez Inthamoussu's avatar Ramiro Facundo Lorenzo Rodriguez Inthamoussu
Browse files

cli

parent ae1f09d4
No related branches found
No related tags found
No related merge requests found
......@@ -15,11 +15,37 @@ using namespace std;
#include <stdlib.h>
#include <stdio.h>
#define PORT "9999"
#define HOST "localhost"
#define MAXLEN 1024
int main() {
void error(const char *);
void TCP(const char* PORT, const char* HOST);
void UDP(int PORT, const char* HOST);
int main(int argc, char *argv[]) {
printf("argv[0]: %s\n" , argv[0] );
printf("argv[1]: %s\n" , argv[1] );
if (strcmp(argv[1],"TCP") == 0){
TCP("9999", argv[2]);
}else if (strcmp(argv[1],"UDP") == 0){
UDP(9998 ,argv[2]);
}else{
printf("ERROR: first argument must be TCP or UDP.\n");
return -1;
}
}
void error(const char *msg)
{
perror(msg);
exit(0);
}
void TCP(const char* PORT, const char* HOST){
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
//obtenemos la direccion con getaddrinfo
......@@ -59,8 +85,46 @@ int main() {
freeaddrinfo(res);
}
void UDP(int PORT, const char* HOST){
int sock, n;
unsigned int length;
struct sockaddr_in server, from;
struct hostent *hp;
char buffer[256];
sock= socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
error("socket");
server.sin_family = AF_INET;
hp = gethostbyname(HOST);
if (hp==0)
error("Unknown host");
bcopy((char *)hp->h_addr,
(char *)&server.sin_addr,
hp->h_length);
server.sin_port = htons(PORT);
length=sizeof(struct sockaddr_in);
bzero(buffer,256);
sprintf(buffer, "subscription");
if (sendto(sock,buffer, strlen(buffer),0,(const struct sockaddr *)&server,length) < 0)
error("Error on send first datagram");
n = recvfrom(sock,buffer,256,0,(struct sockaddr *)&from, &length);
if (n < 0)
error("recvfrom");
write(1,"Got an ack: ",12);
write(1,buffer,n);
close(sock);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment