Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ############# DAYTIME Z WYKORZYSTANIEM UDP
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <arpa/inet.h>
- #define BUFSIZE 10000
- char bufor[BUFSIZE];
- int resp;
- char *service = "daytime";
- char *protocol = "udp";
- int main (int argc, char** argv) {
- struct sockaddr_in sck_addr;
- struct hostent *host_ptr;
- struct protoent *protocol_ptr;
- struct servent *service_ptr;
- int sck, odp;
- if (argc < 2) {
- printf ("Podaj nazwę serwera\n");
- exit (EXIT_FAILURE);
- }
- printf ("Usługa %d na %s z serwera %s :\n", service, protocol, argv[1]);
- fflush(stdout);
- if ((host_ptr = gethostbyname (argv[1])) == NULL) {
- perror("Nie znaleziono serwera");
- exit(EXIT_FAILURE);
- }
- if ((protocol_ptr = getprotobyname (protocol)) == NULL) {
- perror ("Nie znaleziono protokołu");
- exit(EXIT_FAILURE);
- }
- if ((service_ptr = getservbyname (service, protocol)) == NULL) {
- perror ("Nie znaleziono uslugi");
- exit(EXIT_FAILURE);
- }
- memset (&sck_addr, 0, sizeof sck_addr);
- sck_addr.sin_family = host_ptr -> h_addrtype;
- memcpy ((char*) &sck_addr.sin_addr, (char*) host_ptr -> h_addr, host_ptr -> h_length);
- sck_addr.sin_port = service_ptr ->s_port;
- if ((sck = socket (PF_INET, SOCK_DGRAM, protocol_ptr -> p_proto)) < 0) {
- perror ("Nie można utworzyć gniazdka");
- exit (EXIT_FAILURE);
- }
- if (connect (sck, (struct sockaddr*) &sck_addr, sizeof sck_addr) < 0) {
- perror ("Brak połączenia");
- exit (EXIT_FAILURE);
- }
- send(sck, "", 1, 0);
- resp = recv(sck, bufor, BUFSIZE, 0);
- printf (bufor);
- fflush (stdout);
- close (sck);
- exit (EXIT_SUCCESS);
- }
- ################ DAYTIME Z WYKORZYSTANIEM UDP (BEZ CONNECT'A)
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <arpa/inet.h>
- #define BUFSIZE 10000
- char bufor[BUFSIZE];
- int resp;
- char *service = "daytime";
- char *protocol = "udp";
- int main (int argc, char** argv) {
- struct sockaddr_in sck_addr;
- struct hostent *host_ptr;
- struct protoent *protocol_ptr;
- struct servent *service_ptr;
- int sck, odp;
- if (argc < 2) {
- printf ("Podaj nazwę serwera\n");
- exit (EXIT_FAILURE);
- }
- printf ("Usługa %d na %s z serwera %s :\n", service, protocol, argv[1]);
- fflush(stdout);
- if ((host_ptr = gethostbyname (argv[1])) == NULL) {
- perror("Nie znaleziono serwera");
- exit(EXIT_FAILURE);
- }
- if ((protocol_ptr = getprotobyname (protocol)) == NULL) {
- perror ("Nie znaleziono protokołu");
- exit(EXIT_FAILURE);
- }
- if ((service_ptr = getservbyname (service, protocol)) == NULL) {
- perror ("Nie znaleziono uslugi");
- exit(EXIT_FAILURE);
- }
- memset (&sck_addr, 0, sizeof sck_addr);
- sck_addr.sin_family = host_ptr -> h_addrtype;
- memcpy ((char*) &sck_addr.sin_addr, (char*) host_ptr -> h_addr, host_ptr -> h_length);
- sck_addr.sin_port = service_ptr ->s_port;
- if ((sck = socket (PF_INET, SOCK_DGRAM, protocol_ptr -> p_proto)) < 0) {
- perror ("Nie można utworzyć gniazdka");
- exit (EXIT_FAILURE);
- }
- sendto(sck, "", 1, 0, (struct sockaddr*)&sck_addr, sizeof sck_addr);
- int sck_len = sizeof sck_addr;
- resp = recvfrom(sck, bufor, BUFSIZE, 0, (struct sockaddr*)&sck_addr, &sck_len);
- printf (bufor);
- fflush (stdout);
- close (sck);
- exit (EXIT_SUCCESS);
- }
- ############# SERWER ECHO UDP
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include <time.h>
- #define SERVER_PORT 1234
- #define QUEUE_SIZE 5
- int main(int argc, char* argv[])
- {
- int nSocket, nClientSocket;
- int nBind, nListen;
- int nFoo = 1;
- socklen_t nTmp;
- int BUFSIZE = 10000;
- char bufor [BUFSIZE];
- struct sockaddr_in stAddr, stClientAddr;
- /* address structure */
- memset(&stAddr, 0, sizeof(struct sockaddr));
- stAddr.sin_family = AF_INET;
- stAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- stAddr.sin_port = htons(SERVER_PORT);
- /* create a socket */
- nSocket = socket(AF_INET, SOCK_DGRAM, 0);
- if (nSocket < 0)
- {
- fprintf(stderr, "%s: Can't create a socket.\n", argv[0]);
- exit(1);
- }
- setsockopt(nSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&nFoo, sizeof(nFoo));
- /* bind a name to a socket */
- nBind = bind(nSocket, (struct sockaddr*)&stAddr, sizeof(struct sockaddr));
- if (nBind < 0)
- {
- fprintf(stderr, "%s: Can't bind a name to a socket.\n", argv[0]);
- exit(1);
- }
- while(1) {
- memset(&bufor, 0, BUFSIZE);
- int sck_len = sizeof stAddr;
- recvfrom(nSocket, bufor, BUFSIZE, 0, (struct sockaddr*) &stAddr, &sck_len);
- sendto(nSocket, bufor, BUFSIZE, 0, (struct sockaddr*) &stAddr, sizeof stAddr);
- }
- close(nSocket);
- return(0);
- }
- ######## CLIENT ECHO UDP
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <arpa/inet.h>
- #define BUFSIZE 10000
- char *server = "127.0.0.1"; /* adres IP pętli zwrotnej */
- char *protocol = "udp";
- short service_port = 1234; /* port usługi daytime */
- char bufor[BUFSIZE];
- int main ()
- {
- struct sockaddr_in sck_addr;
- int sck, resp;
- printf ("Usługa %d na %s z serwera %s :\n", service_port, protocol, server);
- memset (&sck_addr, 0, sizeof sck_addr);
- sck_addr.sin_family = AF_INET;
- inet_aton (server, &sck_addr.sin_addr);
- sck_addr.sin_port = htons (service_port);
- if ((sck = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
- perror ("Nie można utworzyć gniazdka");
- exit (EXIT_FAILURE);
- }
- gets(bufor);
- sendto(sck, bufor, BUFSIZE, 0, (struct sockaddr*)&sck_addr, sizeof sck_addr);
- int sck_len = sizeof sck_addr;
- memset(&bufor, 0, BUFSIZE);
- resp = recvfrom(sck, bufor, BUFSIZE, 0, (struct sockaddr*)&sck_addr, &sck_len);
- printf (bufor);
- fflush (stdout);
- close (sck);
- exit (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement