linux 网络的一些链接资源

来源:互联网 发布:网络小额贷款运营模式 编辑:程序博客网 时间:2024/05/20 10:23

http://www.farsight.com.cn/news/emb188.htm

http://www.vimer.cn/2009/11/%E5%A4%9A%E8%BF%9B%E7%A8%8Bfork%E7%9A%84%E6%A1%86%E6%9E%B6%E7%A4%BA%E4%BE%8B.html

 关于LINUX下C语言多线程并发Socket服务器的例子
2012-06-18 12:18:28
标签:LINUX C 多线程 Socket 最好的网购网站 网站购物大全
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://typedef.blog.51cto.com/3209600/901482

    好久没有写这边的文章了, 作为一个IT界的新人.  最近因为升职的原因, 一直都好忙. 因为没有做过管理, 忽然到了这个位置上. 不适应是肯定的!  因为更喜欢钻研技术, 管理上欠缺的太多. 呵呵,  废话太多了.  切入正题.

    最近需要计划做一个服务器的程序. 和我们用VC++开发的客户端程序做对接, 我在LINUX服务器下用C写了一段简单的服务器, 代码如下:

首先是头文件: CCServer.h

  1. #include <sys/types.h> 
  2. #include <sys/socket.h> 
  3. #include <netinet/in.h> 
  4. #include <time.h> 
  5. #include <string.h> 
  6. #include <stdlib.h> 
  7. #include <stdio.h> 
  8. #include <pthread.h> 
  9. #define BUFFLEN 1024 
  10. #define SERVER_PORT 9696 
  11. #define BACKLOG 5 
  12. static void * handle_request(void * argv); 
  13.  
  14. static void handle_connect(int); 

然后是CCServer.c

 

  1. #include "CCServer.h" 
  2. #include "bt_iconv.h" 
  3.  
  4. int main(int argc, char * argv[]) 
  5.         int s_s; 
  6.         struct sockaddr_in local; 
  7.         s_s = socket(AF_INET, SOCK_STREAM, 0); 
  8.  
  9.         memset(&local, 0, sizeof(local)); 
  10.         local.sin_family = AF_INET; 
  11.         local.sin_addr.s_addr = htonl(INADDR_ANY); 
  12.         local.sin_port = htons(SERVER_PORT); 
  13.  
  14.         int err = bind(s_s, (struct sockaddr *) &local, sizeof(local)); 
  15.         err = listen(s_s, BACKLOG); 
  16.         handle_connect(s_s); 
  17.         close(s_s); 
  18.  
  19.         return 0; 
  20.  
  21. static void * handle_request(void * argv) 
  22.         int s_c = * ((int *) argv); 
  23.         time_t now; 
  24.         char buff[BUFFLEN]; 
  25.         char m_buff[BUFFLEN]; 
  26.         while (1) { 
  27.                 int n = 0; 
  28.                 memset(buff, 0, BUFFLEN); 
  29.                 memset(m_buff, 0, BUFFLEN); 
  30.                 n = recv(s_c, buff, BUFFLEN, 0); 
  31.                 //printf("接收到%s \r\n", buff); 
  32.                 if (n > 0) 
  33.                 { 
  34.                         GB2312ToUTF8(buff, m_buff, strlen(buff) + 100); 
  35.                         printf("接收到:%s\n", m_buff); //测试的代码 
  36.                         send(s_c, buff, strlen(buff), 0); 
  37.                 } 
  38.                 close(s_c); 
  39.         } 
  40.  
  41. static void handle_connect(int s_s) 
  42.         int s_c; 
  43.         struct sockaddr_in from; 
  44.         int len = sizeof(from); 
  45.         pthread_t thread_do; 
  46.  
  47.         while (1) 
  48.         { 
  49.                 s_c = accept(s_s, (struct sockaddr *) &from, &len); 
  50.                 if (s_c > 0) 
  51.                 { 
  52.                         int err = pthread_create(&thread_do, 
  53.                                         NULL, 
  54.                                         handle_request, 
  55.                                         (void *) &s_c); 
  56.                 } 
  57.         } 

  中间因为LINUX使用的是UTF-8的编码, 而在win下我用的是多字节字符集. 就必须要一段字符集转换的代码. 主要是因为我对VC++不熟悉, 所以只能是在LINUX下转换了. 上代码 bt_iconv.h

 

  1. #include <stddef.h> 
  2. #include <iconv.h> 
  3. #include <assert.h> 
  4. #include <string.h> 
  5.  
  6. extern int GB2312ToUTF8(char * srcStr, char * desBuff, size_t desBuffLength); 
  7.  
  8. extern int UTF8ToGB2312(char * srcStr, char * desBuff, size_t desBuffLength); 

最近是 bt_iconv.c:

 

  1. #include "bt_iconv.h" 
  2.  
  3. extern int UTF8ToGB2312(char * srcStr, char * desBuff, size_t desBuffLength) 
  4.         assert(strlen(srcStr) > 0); 
  5.         size_t iLen = strlen(srcStr); 
  6.  
  7.         iconv_t cd; 
  8.  
  9.         cd = iconv_open("gb2312""utf-8"); 
  10.         assert(cd != 0); 
  11.         iconv(cd, &srcStr, &iLen, &desBuff, &desBuffLength); 
  12.         iconv_close(cd); 
  13.  
  14.         return desBuffLength; 
  15.  
  16.  
  17.  
  18. extern int GB2312ToUTF8(char * srcStr, char * desBuff, size_t desBuffLength) 
  19.         assert(strlen(srcStr) > 0); 
  20.         size_t iLen = strlen(srcStr); 
  21.         iconv_t cd; 
  22.  
  23.         cd = iconv_open("utf-8""gb2312"); 
  24.         assert(cd != 0); 
  25.         iconv(cd, &srcStr, &iLen, &desBuff, &desBuffLength); 
  26.         iconv_close(cd); 
  27.  
  28.         return desBuffLength; 

然后就是编译下就OK了, 测试没基本没有问题.

[cwork@localhost CCServer]$ gcc -o socketServerDemo CCServer.h CCServer.c -lpthread

完成 !

转载请注明来自派啦网.

本文出自 “Typedef” 博客,请务必保留此出处http://typedef.blog.51cto.com/3209600/901482


原创粉丝点击