| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 368 人关注过本帖
标题:求助:代码中服务端的设计思路用尽量文字描述。
只看楼主 加入收藏
maxin2601
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-11-3
收藏
 问题点数:0 回复次数:0 
求助:代码中服务端的设计思路用尽量文字描述。
// qiu.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>              
#include <winsock2.h>        
#include <conio.h>   //标准IO         
#include <windows.h>    //调用Sleep()
#include <process.h>
#include <stdlib.h>
#include <tchar.h>
#include "s_help.h"
   
#pragma    comment(    lib,    "ws2_32.lib"    )
#define     SOCKET_MAX_CONNECT     10//最大连接数
#define        Port    7600
//#define        nHOST_IP    "172.26.2.48"
unsigned __stdcall    ListenThread(LPVOID     pParam);//函数声明,没有用类         
unsigned __stdcall    ClientThread(LPVOID     pParam);
char nHOST_IP[20];
int _tmain(int argc, _TCHAR* argv[])
{   
           
        LPVOID     pParam=NULL; //传递给线程的参数         
        u_short     nTCP_Port= Port;     //服务器端口号      
        pParam =&nTCP_Port;           
        unsigned     dwThreadId;//开启的线程ID
        HANDLE    hThread = (HANDLE)_beginthreadex( NULL, 0, &ListenThread, pParam, 0, &dwThreadId );
     
        printf("Press ESCAPE to STOP SERVER!!\r\n");  
        strcpy(nHOST_IP,"172.26.2.48");
        GetLocalIP(nHOST_IP);
        printf("\n%s\n",nHOST_IP);
        while(_getch()!=27);      
        CloseHandle( hThread );
        return     0;      
}
unsigned __stdcall ListenThread(LPVOID     pParam)         
    {         
            //取得参数         
            int     nRetCode;         
            u_short     nTCP_Port;         
            nTCP_Port=     *((u_short*)pParam);         
   
            //调用WSAStartup初始化WINSOCK     API.

            WSADATA wsaData;
            WORD     wVersionRequested=MAKEWORD(2,2);//WINSOCK版本2.2         
            nRetCode=WSAStartup(wVersionRequested,&wsaData);         
            if(nRetCode!=0)     //init     failure         
            {         
                    return     nRetCode;         
            }         
   
            printf("Starting     up     TCP     server\r\n");     
   
            SOCKET     listenSocket;     
            listenSocket=socket(AF_INET,SOCK_STREAM,0);     
            if(listenSocket==INVALID_SOCKET)     //create     socket     failure     
      {     
        return     0;     
      }     
      SOCKADDR_IN serverAddr;//绑定到服务器的网卡和端口
      serverAddr.sin_family = AF_INET; //Address     family     
      serverAddr.sin_port = htons(nTCP_Port);
      serverAddr.sin_addr.s_addr = inet_addr(nHOST_IP);   
         
       if (bind(listenSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
     {
      printf("绑定端口失败!\n");
      return -1;
     }
     else
     {
      printf("绑定端口成功!\n");
     }
   
     if (listen(listenSocket, 5) == 0)
     {
      printf("已启动监听...\n");
     }
     else
     {
      printf("监听失败...\n");
      return -1;
     }   
      while(true)//一直循环     
      {     
       SOCKET     *     pClientSocket;     
       SOCKADDR_IN     from;     
       int     fromlen=sizeof(from);     
      
       //accept()等待连接,程序在此等待     
        pClientSocket     =     new     SOCKET();     
       *pClientSocket=accept(listenSocket, (struct sockaddr*)&from,&fromlen);     
      
       //为每个连接开一个新线程,把Socket传递给新线程     
       unsigned     dwThreadId;   
      //HANDLE     hThread=CreateThread(NULL,0,ClientThread,(LPVOID)pClientSocket,0,&dwThreadId);     
       HANDLE    hThread = (HANDLE)_beginthreadex( NULL, 0, &ClientThread,(LPVOID)pClientSocket, 0, &dwThreadId );
  
       //在服务器端console输出客户的ip。     
       printf("Connection     from %s \n",inet_ntoa(from.sin_addr));     
      }     
      
      //关闭服务器的socket     
      closesocket(listenSocket);     
      
      //清除winsock  
      WSACleanup();
      _endthreadex(0);
      return     0;     
    }     
   
unsigned __stdcall     ClientThread(LPVOID     pParam)//与客户交互的线程     
{     
     SOCKET     *     pClientSocket;//取得socket     
     pClientSocket     =     (SOCKET*)pParam;     
     const int BUF_SIZE = 512;
     char buf[BUF_SIZE] = {0};  
     bool checkAdmin = false;
     while(true)
     {     
      //send(*pClientSocket,buf,(int)strlen(buf),0);
      //rev
      if(checkAdmin == false)
      {
          int ret = recv(*pClientSocket, buf, sizeof(buf)-1, 0);
          if(!strcmp(buf,"123abc"))
          {
              checkAdmin = true;
              send(*pClientSocket, "成功\0", 10, 0);
          }
          else
              send(*pClientSocket, "失败", 10, 0);
              
      }
      else
      {
          dateprocesser(*pClientSocket);
      }
           
     }
     //在线程里关闭对话,释放资源     
     closesocket(*pClientSocket);     
     delete     pClientSocket;     
     _endthreadex(0);  
     return     0;     
}
搜索更多相关主题的帖子: 设计 应用程序 comment include 服务端 
2011-12-01 22:48
快速回复:求助:代码中服务端的设计思路用尽量文字描述。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016819 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved