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
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://typedef.blog.51cto.com/3209600/901482
好久没有写这边的文章了, 作为一个IT界的新人. 最近因为升职的原因, 一直都好忙. 因为没有做过管理, 忽然到了这个位置上. 不适应是肯定的! 因为更喜欢钻研技术, 管理上欠缺的太多. 呵呵, 废话太多了. 切入正题.
最近需要计划做一个服务器的程序. 和我们用VC++开发的客户端程序做对接, 我在LINUX服务器下用C写了一段简单的服务器, 代码如下:
首先是头文件: CCServer.h
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <time.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <pthread.h>
- #define BUFFLEN 1024
- #define SERVER_PORT 9696
- #define BACKLOG 5
- static void * handle_request(void * argv);
-
- static void handle_connect(int);
然后是CCServer.c
- #include "CCServer.h"
- #include "bt_iconv.h"
-
- int main(int argc, char * argv[])
- {
- int s_s;
- struct sockaddr_in local;
- s_s = socket(AF_INET, SOCK_STREAM, 0);
-
- memset(&local, 0, sizeof(local));
- local.sin_family = AF_INET;
- local.sin_addr.s_addr = htonl(INADDR_ANY);
- local.sin_port = htons(SERVER_PORT);
-
- int err = bind(s_s, (struct sockaddr *) &local, sizeof(local));
- err = listen(s_s, BACKLOG);
- handle_connect(s_s);
- close(s_s);
-
- return 0;
- }
-
- static void * handle_request(void * argv)
- {
- int s_c = * ((int *) argv);
- time_t now;
- char buff[BUFFLEN];
- char m_buff[BUFFLEN];
- while (1) {
- int n = 0;
- memset(buff, 0, BUFFLEN);
- memset(m_buff, 0, BUFFLEN);
- n = recv(s_c, buff, BUFFLEN, 0);
-
- if (n > 0)
- {
- GB2312ToUTF8(buff, m_buff, strlen(buff) + 100);
- printf("接收到:%s\n", m_buff);
- send(s_c, buff, strlen(buff), 0);
- }
- close(s_c);
- }
- }
-
- static void handle_connect(int s_s)
- {
- int s_c;
- struct sockaddr_in from;
- int len = sizeof(from);
- pthread_t thread_do;
-
- while (1)
- {
- s_c = accept(s_s, (struct sockaddr *) &from, &len);
- if (s_c > 0)
- {
- int err = pthread_create(&thread_do,
- NULL,
- handle_request,
- (void *) &s_c);
- }
- }
- }
中间因为LINUX使用的是UTF-8的编码, 而在win下我用的是多字节字符集. 就必须要一段字符集转换的代码. 主要是因为我对VC++不熟悉, 所以只能是在LINUX下转换了. 上代码 bt_iconv.h
- #include <stddef.h>
- #include <iconv.h>
- #include <assert.h>
- #include <string.h>
-
- extern int GB2312ToUTF8(char * srcStr, char * desBuff, size_t desBuffLength);
-
- extern int UTF8ToGB2312(char * srcStr, char * desBuff, size_t desBuffLength);
最近是 bt_iconv.c:
- #include "bt_iconv.h"
-
- extern int UTF8ToGB2312(char * srcStr, char * desBuff, size_t desBuffLength)
- {
- assert(strlen(srcStr) > 0);
- size_t iLen = strlen(srcStr);
-
- iconv_t cd;
-
- cd = iconv_open("gb2312", "utf-8");
- assert(cd != 0);
- iconv(cd, &srcStr, &iLen, &desBuff, &desBuffLength);
- iconv_close(cd);
-
- return desBuffLength;
- }
-
-
-
- extern int GB2312ToUTF8(char * srcStr, char * desBuff, size_t desBuffLength)
- {
- assert(strlen(srcStr) > 0);
- size_t iLen = strlen(srcStr);
- iconv_t cd;
-
- cd = iconv_open("utf-8", "gb2312");
- assert(cd != 0);
- iconv(cd, &srcStr, &iLen, &desBuff, &desBuffLength);
- iconv_close(cd);
-
- return desBuffLength;
- }
然后就是编译下就OK了, 测试没基本没有问题.
[cwork@localhost CCServer]$ gcc -o socketServerDemo CCServer.h CCServer.c -lpthread
完成 !
转载请注明来自派啦网.
本文出自 “Typedef” 博客,请务必保留此出处http://typedef.blog.51cto.com/3209600/901482