Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ############################################################################
- ########### PROGRAM 1
- #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];
- char *service = "daytime";
- char *protocol = "tcp";
- 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_STREAM, 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);
- }
- while ((odp = read (sck, bufor, BUFSIZE)) > 0)
- write (1, bufor, odp);
- close (sck);
- exit (EXIT_SUCCESS);
- }
- ################################################################################
- ############ PROGRAM 2
- #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];
- char *service = "echo";
- char *protocol = "tcp";
- 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;
- char message[1000], server_reply[2000];
- 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_STREAM, 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);
- }
- while (1) {
- printf("\nWiadomość przesyłana: ");
- fflush(stdout);
- gets(message);
- if (send(sck, message, strlen(message), 0) < 0) {
- perror ("Nie mozna wyslac");
- exit (EXIT_FAILURE);
- }
- bzero(server_reply, 2000);
- if ( recv(sck, server_reply, 2000, 0) < 0) {
- perror("Nie mozna odebrac");
- exit (EXIT_FAILURE);
- }
- printf("Wiadomość zwracana: ");
- printf(server_reply);
- fflush(stdout);
- }
- close (sck);
- exit (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement