This is default featured post 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

Sabtu, 31 Mei 2014

Pemrograman Jaringan : Stream Socket


Stream Socket

Socket adalah mekanisme komunikasi yang memungkinkan terjadinya pertukaran data antar program atau proses baik dalam satu mesin maupun antar mesin. Gaya pemrograman socket sendiri berawal dari sistem Unix BSD yang terkenal dengan kepeloporannya pada bidang penanganan jaringan, sehingga sering disebut BSD Socket. Socket pertama kali diperkenalkan di sistem Unix BSD versi 4.2 tahun 1983 sebagai kelanjutan dari implementasi protokol TCP/IP yang muncul pertama kali pada sistem Unix BSD 4.1 pada akhir 1981. Hampir setiap variant Unix dan Linux mengadopsi BSD Socket. Pada lingkungan Unix, socket memberikan keleluasaan pemrograman gaya Unix yang terkenal dengan ideologinya, Semua di Unix/Linux adalah file. Komunikasi antar program dapat berlangsung lewat penggunaan deskriptor file standar Unix dengan bantuan socket.

Client



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <netdb.h>
  7. #include <sys/types.h>
  8. #include <netinet/in.h>
  9. #include <sys/socket.h>
  10. #define PORT 3333
  11. #define MAXDATASIZE 100
  12. int main(int argc, char const *argv[])
  13. {
  14.         int sockfd, numbytes;
  15.         char buf[MAXDATASIZE];
  16.         struct hostent *he;
  17.         struct sockaddr_in their_addr;
  18.         if (argc != 2)
  19.         {
  20.                 fprintf(stderr, "usage: client hostname\n");
  21.                 exit(1);
  22.         }
  23.         if ((he=gethostbyname(argv[1])) == NULL)
  24.         {
  25.                 perror("gethostbyname");
  26.                 exit(1);
  27.         }
  28.         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  29.         {
  30.                 perror("socket");
  31.                 exit(1);
  32.         }
  33.         their_addr.sin_family = AF_INET;
  34.         their_addr.sin_port = htons(PORT);
  35.         their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  36.         memset(&(their_addr.sin_zero), '\0', 8);
  37.         if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)
  38.         {
  39.                 perror("connect");
  40.                 exit(1);
  41.         }
  42.         if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1)
  43.         {
  44.                 perror("recv");
  45.                 exit(1);
  46.         }
  47.         buf[numbytes] = '\0';
  48.         printf("Received: %s", buf);
  49.         close(sockfd);
  50.         return 0;
  51. }

Server



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <sys/wait.h>
  11. #include <signal.h>
  12. #define MYPORT 3333
  13. #define BACKLOG 10
  14. void sigchld_handler(int s)
  15. {
  16.         while(wait(NULL) > 0);
  17. }
  18. int main(void)
  19. {
  20.         int sockfd, new_fd;
  21.         struct sockaddr_in my_addr;
  22.         struct sockaddr_in their_addr;
  23.         int sin_size;
  24.         struct sigaction sa;
  25.         int yes=1;
  26.         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  27.         {
  28.                 perror("socket");
  29.                 exit(1);
  30.         }
  31.         if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
  32.         {
  33.                 perror("setsockopt");
  34.                 exit(1);
  35.         }
  36.         my_addr.sin_family = AF_INET;
  37.         my_addr.sin_port = htons(MYPORT);
  38.         my_addr.sin_addr.s_addr = INADDR_ANY;
  39.         memset(&(my_addr.sin_zero), '\0', 8);
  40.         if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
  41.         {
  42.                 perror("bind");
  43.                 exit(1);
  44.         }
  45.         if (listen(sockfd, BACKLOG) == -1)
  46.         {
  47.                 perror("listen");
  48.                 exit(1);
  49.         }
  50.         sa.sa_handler = sigchld_handler;
  51.         sigemptyset(&sa.sa_mask);
  52.         sa.sa_flags = SA_RESTART;
  53.         if (sigaction(SIGCHLD, &sa, NULL) == -1)
  54.         {
  55.                 perror("sigaction");
  56.                 exit(1);
  57.         }
  58.         while(1)
  59.         {
  60.                 sin_size = sizeof(struct sockaddr_in);
  61.                 if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
  62.                 {
  63.                         perror("accept");
  64.                         continue;
  65.                 }
  66.                 printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr));
  67.                 if (!fork())
  68.                 {
  69.                         close(sockfd);
  70.                         if (send(new_fd, "Pengiriman data dengan stream socket berhasil!\n", 14, 0) == -1)
  71.                         {
  72.                                 perror("send");
  73.                         }
  74.                         close(new_fd);
  75.                         exit(0);
  76.                 }
  77.                 close(new_fd);
  78.         }
  79.         return 0;
  80. }

langkah langkah menjalankan program


1. simpan program dengan nama client.c dan untuk server dengan nama server.c

2. lakukan instalasi compiler untuk c pada linux
3. #yum install gcc
4. lakukan kompilasi progaram
5. gcc client.c -o client
6. gcc server.c -o server
7. jalankan program server dan client
8 #./server
9. #.client <ip server>

output server






















output client





sumber : http://salmaann.blogspot.com/2014/05/program-stream-socket.html
http://dzikrylazuardy.blogspot.com/2014/05/program-stream-socket-socket-adalah.html

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites