Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------ UDP sender --------
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- int main(int argc, char **argv)
- {
- int sockfd;
- socklen_t addrlen;
- struct sockaddr_in dest;
- // check the number of args on command line
- if(argc != 4)
- {
- printf("USAGE: %s @dest port_num string\n", argv[0]);
- exit(-1);
- }
- // socket factory
- if((sockfd = socket(...)) == -1)
- {
- perror("socket");
- exit(EXIT_FAILURE);
- }
- // init remote addr structure and other params
- dest.sin_family = AF_INET;
- dest.sin_port = htons(atoi(argv[2]));
- addrlen = sizeof(struct sockaddr_in);
- // get addr from command line and convert it
- if(inet_pton(...) != 1)
- {
- perror("inet_pton");
- close(sockfd);
- exit(EXIT_FAILURE);
- }
- // send string
- if(sendto(...) == -1)
- {
- perror("sendto");
- close(sockfd);
- exit(EXIT_FAILURE);
- }
- // close the socket
- close(sockfd);
- return 0;
- }
- ----- UDP receiver -------
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- int main(int argc, char **argv)
- {
- int sockfd;
- char buf[1024];
- socklen_t addrlen;
- struct sockaddr_in my_addr;
- struct sockaddr_in client;
- // check the number of args on command line
- if(argc != 2)
- {
- printf("Usage: %s local_port\n", argv[0]);
- exit(-1);
- }
- // socket factory
- if((sockfd = socket(...)) == -1)
- {
- perror("socket");
- exit(EXIT_FAILURE);
- }
- // init local addr structure and other params
- my_addr.sin_family = AF_INET;
- my_addr.sin_port = htons(atoi(argv[1]));
- my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
- addrlen = sizeof(struct sockaddr_in);
- memset(buf,'\0',1024);
- // bind addr structure with socket
- if(bind(...) == -1)
- {
- perror("bind");
- close(sockfd);
- exit(EXIT_FAILURE);
- }
- // reception de la chaine de caracteres
- if(recvfrom(...) == -1)
- {
- perror("recvfrom");
- close(sockfd);
- exit(EXIT_FAILURE);
- }
- if(sendto(...) == -1)
- {
- perror("sendto");
- close(sockfd);
- exit(EXIT_FAILURE);
- }
- // print the received char
- printf("%s", buf);
- // close the socket
- close(sockfd);
- return 0;
- }
- ----------- TCP Sender ---------
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- int main(int argc, char **argv)
- {
- int sockfd;
- struct sockaddr_in server;
- socklen_t addrlen;
- // check the number of args on command line
- if(argc != 4)
- {
- printf("USAGE: %s @server port_num string\n", argv[0]);
- exit(-1);
- }
- // socket factory
- if((sockfd = socket(....)) == -1)
- {
- perror("socket");
- exit(EXIT_FAILURE);
- }
- // init remote addr structure and other params
- server.sin_family = AF_INET;
- server.sin_port = htons(atoi(argv[2]));
- addrlen = sizeof(struct sockaddr_in);
- // get addr from command line and convert it
- if(inet_pton(....) != 1)
- {
- perror("inet_pton");
- close(sockfd);
- exit(EXIT_FAILURE);
- }
- printf("Trying to connect to the remote host\n");
- if(connect(....) == -1)
- {
- perror("connect");
- exit(EXIT_FAILURE);
- }
- printf("Connection OK\n");
- // send string
- if(sendto(....) == -1)
- {
- perror("sendto");
- close(sockfd);
- exit(EXIT_FAILURE);
- }
- printf("Disconnection\n");
- // close the socket
- close(sockfd);
- return 0;
- }
- ---------- TCP receiver ----------
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- int main(int argc, char **argv)
- {
- int sockfd, sockfd2;
- socklen_t addrlen;
- char buf[1024];
- struct sockaddr_in my_addr;
- struct sockaddr_in client;
- // check the number of args on command line
- if(argc != 2)
- {
- printf("USAGE: %s port_num\n", argv[0]);
- exit(-1);
- }
- // socket factory
- if((sockfd = socket(....)) == -1)
- {
- perror("socket");
- exit(EXIT_FAILURE);
- }
- // init local addr structure and other params
- my_addr.sin_family = AF_INET;
- my_addr.sin_port = htons(atoi(argv[1]));
- my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
- addrlen = sizeof(struct sockaddr_in);
- memset(buf,'\0',1024);
- // bind addr structure with socket
- if(bind(....) == -1)
- {
- perror("bind");
- close(sockfd);
- exit(EXIT_FAILURE);
- }
- // set the socket in passive mode (only used for accept())
- // and set the list size for pending connection
- if(listen(....) == -1)
- {
- perror("listen");
- close(sockfd);
- exit(EXIT_FAILURE);
- }
- printf("Waiting for incomming connection\n");
- if((sockfd2 = accept(....)) == -1)
- {
- perror("accept");
- close(sockfd);
- exit(EXIT_FAILURE);
- }
- printf("Connection active\n");
- // reception de la chaine de caracteres
- if(recv(....) == -1)
- {
- perror("recv");
- close(sockfd);
- close(sockfd2);
- exit(EXIT_FAILURE);
- }
- // print the received char
- printf("%s", buf);
- // fermeture des sockets
- close(sockfd);
- close(sockfd2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement