Linux下兩個程式進行socket通訊的簡單例子

來源:互聯網
上載者:User

備忘:

這兩個程式,應該是在同一台機器上運行。需要事先建立/home/rangaofei/c_test/目錄。

今天下午學習了一下socket通訊編程這方面的知識,看了看書,找了找網上的講解。雖然還是比較暈,但做的小實驗還是成功了,打算明天再深入研究。

在Ubuntu環境下編寫了兩個簡單的小程式,分別是client.c和server.c。一個作為socket用戶端,另一個作為socket伺服器端。在GCC下編譯通過了,並且通訊也成功了。現將代碼貼在下面:


/****************** server program *****************/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <sys/un.h>

int main(){
   int sockfd, newfd, ret, recv_num, recv_num_total = 0;
   char buf[50];
   struct sockaddr_un server_addr;
   remove("/home/rangaofei/c_test/server");
   memset(&server_addr, 0, sizeof(server_addr));
   server_addr.sun_family = AF_UNIX;
   strcpy(server_addr.sun_path, "/home/rangaofei/c_test/server");
   sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
   if (sockfd < 0){
      printf("Invoke socket function to build socket handle error!\n");
      exit(1);
   }
   printf("Build socket handle success~\n");
   ret = bind(sockfd, (struct sockaddr *)(&server_addr), sizeof(server_addr));
   if (ret < 0){
      printf("Invoke bind func bind socket handle and address error!\n");
      exit(2);
   }
   printf("Bind socket handle and address specficed success~\n");
   ret = listen(sockfd, 4);
   if (ret < 0){
      printf("Invoke listen function to make sure server can respond the request error!\n");
      exit(3);
   }
   printf("Respond the request success~\n");
   newfd = accept(sockfd, NULL, NULL);  //newfd connect client which call connect function
   if (newfd < 0){
      printf("Call accept function error, build connection failed!\n");
      exit(4);
   }
   printf("Build connection with client success~\n");
   while(1){
      recv_num = recv(newfd, buf, 24, 0);
      if (recv_num < 0)
         printf("Call recv function receive message failed!\n");
      else{
         recv_num_total += recv_num;
         printf("Call recv seccess~, received %d words, message is \"%s\". Total date received is %d.\n", recv_num, buf, recv_num_total);
      }
      sleep(1);
   }
}



/****************** client program *****************/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <sys/un.h>

int main(){
   int sockfd, ret, send_num, send_num_total = 0;
   char buf[50] = "This is my socket data.";
   struct sockaddr_un server_addr;
   memset(&server_addr, 0, sizeof(server_addr));
   server_addr.sun_family = AF_UNIX;
   strcpy(server_addr.sun_path, "/home/rangaofei/c_test/server");
   sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
   if (sockfd < 0){
      printf("Invoke socket function to build socket handle error!\n");
      exit(1);
   }
   printf("Build socket handle success~\n");
   ret=connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(server_addr));
   if (ret < 0){
      printf("Invoke connect func to connect server faild!\n");
      exit(2);
   }
   printf("Connect server success~\n");
   while(1){
      send_num = send(sockfd, buf, sizeof(buf), MSG_DONTWAIT);
      if (send_num < 0)
         printf("Call send function to send message to server failed!\n");
      else{
         send_num_total += send_num;
         printf("Call send func seccess~, send %d words, message is \"%s\". Total date send is %d.\n", send_num, buf, send_num_total);
      }
      sleep(1);
   }
}


注意:這兩個程式運行時,要先運行伺服器端程式,否則會出錯。因為用戶端將無法找到伺服器。伺服器端運行後,將會阻塞以等待用戶端的串連。

參考:http://www.cnblogs.com/skynet/archive/2010/12/12/1903949.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.