Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1727939
  • 博文数量: 782
  • 博客积分: 2455
  • 博客等级: 大尉
  • 技术积分: 4140
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-06 21:37
个人简介

Linux ,c/c++, web,前端,php,js

文章分类

全部博文(782)

文章存档

2015年(8)

2014年(28)

2013年(110)

2012年(307)

2011年(329)

分类:

2011-09-12 22:32:12

原文地址:socket通讯简化例子 作者:大隐隐于床

openSUSE 2.6 X86_64 下测试通过
 
1 服务器端简化.
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4. #include "netdb.h"

  5. int main(int argc, char *argv[])
  6. {
  7.     int servfd, clifd;
  8.     struct sockaddr_in servaddr, cliaddr;
  9.     
  10.     if((servfd = socket(AF_INET, SOCK_STREAM,0)) < 0) return;

  11.     bzero(&servaddr, sizeof(servaddr));
  12.     servaddr.sin_family = AF_INET;
  13.     servaddr.sin_port = htons(7777);
  14.     servaddr.sin_addr.s_addr = htons(INADDR_ANY);

  15.     if(bind(servfd, (struct sockaddr*)&servaddr, sizeof(servaddr))<0) return;
  16.     if(listen(servfd, 24) < 0) return;

  17.     while(1)
  18.     {
  19.         socklen_t length = sizeof(cliaddr);
  20.         if((clifd = accept(servfd, (struct sockaddr*)&cliaddr, &length)) < 0) continue;

  21.         #define BUFFER_SIZE 4096
  22.         int lenth = 0;
  23.         char sreceive[BUFFER_SIZE];
  24.         char ssend[BUFFER_SIZE];
  25.     
  26.         if((lenth = recv(clifd, sreceive, BUFFER_SIZE,0)) > 0)
  27.         {
  28.             printf("[receive]%s\n", sreceive);    
  29.             sprintf(ssend,"hello.\n");    
  30.             send(clifd, ssend, strlen(ssend),0);
  31.         }

  32.         close(clifd);
  33.     }
  34.     
  35.     close(servfd);
  36. }
 
2 客户端简化.

  1. char shost[128] = "";
  2. char sport[64] = "80";

  3. //socket
  4. struct hostent *host;
  5. int portnumber,nbytes;

  6. if( ( host = gethostbyname( shost)) == NULL) return 0;
  7. if( ( portnumber = atoi( sport)) < 0) return 0;

  8. int sockfd;
  9. struct sockaddr_in server_addr;
  10. if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
  11.     return 0;

  12. bzero(&server_addr,sizeof(server_addr));
  13. server_addr.sin_family=AF_INET;
  14. server_addr.sin_port=htons(portnumber);
  15. server_addr.sin_addr=*((struct in_addr *)host->h_addr);

  16. //select interval
  17. struct timeval timeo = {3, 0};
  18. socklen_t len = sizeof(timeo);
  19. setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeo, len);

  20. //connect
  21. if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
  22. {
  23.     printf("connect out time.\n");
  24.   return 0;
  25. }

  26. //send
  27. char oRequest[256] = "some thing.";
  28. if((nbytes = send( sockfd, oRequest, strlen(oRequest), 0)) == -1)
  29. {
  30.     printf("send out time.\n");
  31.   return 0;
  32. }

  33. //read
  34. char result[10240];
  35. char tmp[1024];
  36. memset( result, 0x0, sizeof(result));

  37. int cnt = 0;
  38. while( ( nbytes= read( sockfd, tmp, sizeof(result) - 1) ) > 0 )
  39. {
  40.     cnt += nbytes;
  41.     if( cnt < sizeof(result))
  42.         strcat( result, tmp);
  43.     else
  44.         return 0;
  45. }

  46. printf("[%s]\n", result);
  47. close(sockfd);


 

阅读(433) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~