| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 851 人关注过本帖
标题:本人大四在做毕设,马上答辩。急需知道下面这段程序的作用,忘高手前来帮助 ...
只看楼主 加入收藏
foxmansu
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-6-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
本人大四在做毕设,马上答辩。急需知道下面这段程序的作用,忘高手前来帮助啊
我的c语言基础为0000000
/*
 * SSSSimpleSocketServerTask()
 *
 * This MicroC/OS-II thread spins forever after first establishing a listening
 * socket for our sss connection, binding it, and listening. Once setup,
 * it perpetually waits for incoming data to either the listening socket, or
 * (if a connection is active), the sss data socket. When data arrives,
 * the approrpriate routine is called to either accept/reject a connection
 * request, or process incoming data.
 */
void SSSSimpleSocketServerTask()
{
  int fd_listen, max_socket;
  struct sockaddr_in addr;
  static SSSConn conn;
  fd_set readfds;
  
  /*
   * Sockets primer...
   * The socket() call creates an endpoint for TCP of UDP communication. It
   * returns a descriptor (similar to a file descriptor) that we call fd_listen,
   * or, "the socket we're listening on for connection requests" in our sss
   * server example.
   */
  if ((fd_listen = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  {
    alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE,"[sss_task] Socket creation failed");
  }
  
  /*
   * Sockets primer, continued...
   * Calling bind() associates a socket created with socket() to a particular IP
   * port and incoming address. In this case we're binding to SSS_PORT and to
   * INADDR_ANY address (allowing anyone to connect to us. Bind may fail for
   * various reasons, but the most common is that some other socket is bound to
   * the port we're requesting.
   */
  addr.sin_family = AF_INET;
  addr.sin_port = htons(SSS_PORT);
  addr.sin_addr.s_addr = INADDR_ANY;
  
  if ((bind(fd_listen,(struct sockaddr *)&addr,sizeof(addr))) < 0)
  {
    alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE,"[sss_task] Bind failed");
  }
   
  /*
   * Sockets primer, continued...
   * The listen socket is a socket which is waiting for incoming connections.
   * This call to listen will block (i.e. not return) until someone tries to
   * connect to this port.
   */
  if ((listen(fd_listen,1)) < 0)
  {
    alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE,"[sss_task] Listen failed");
  }

  /* At this point we have successfully created a socket which is listening
   * on SSS_PORT for connection requests from any remote address.
   */
  sss_reset_connection(&conn);
  printf("[sss_task] Simple Socket Server listening on port %d\n", SSS_PORT);
   
  while(1)
  {
    /*
     * For those not familiar with sockets programming...
     * The select() call below basically tells the TCPIP stack to return
     * from this call when any of the events I have expressed an interest
     * in happen (it blocks until our call to select() is satisfied).
     *
     * In the call below we're only interested in either someone trying to
     * connect to us, or data being available to read on a socket, both of
     * these are a read event as far as select is called.
     *
     * The sockets we're interested in are passed in in the readfds
     * parameter, the format of the readfds is implementation dependant
     * Hence there are standard MACROs for setting/reading the values:
     *
     *   FD_ZERO  - Zero's out the sockets we're interested in
     *   FD_SET   - Adds a socket to those we're interested in
     *   FD_ISSET - Tests whether the chosen socket is set
     */
    FD_ZERO(&readfds);
    FD_SET(fd_listen, &readfds);
    max_socket = fd_listen+1;

    if (conn.fd != -1)
    {
      FD_SET(conn.fd, &readfds);
      if (max_socket <= conn.fd)
      {
        max_socket = conn.fd+1;
      }
    }

    select(max_socket, &readfds, NULL, NULL, NULL);

    /*
     * If fd_listen (the listening socket we originally created in this thread
     * is "set" in readfs, then we have an incoming connection request. We'll
     * call a routine to explicitly accept or deny the incoming connection
     * request (in this example, we accept a single connection and reject any
     * others that come in while the connection is open).
     */
    if (FD_ISSET(fd_listen, &readfds))
    {
      sss_handle_accept(fd_listen, &conn);
    }
    /*
     * If sss_handle_accept() accepts the connection, it creates *another*
     * socket for sending/receiving data over sss. Note that this socket is
     * independant of the listening socket we created above. This socket's
     * descriptor is stored in conn.fd. If conn.fs is set in readfs... we have
     * incoming data for our sss server, and we call our receiver routine
     * to process it.
     */
    else
    {
      if ((conn.fd != -1) && FD_ISSET(conn.fd, &readfds))
      {
        sss_handle_receive(&conn);
      }
    }
  } /* while(1) */
}
搜索更多相关主题的帖子: forever either c语言 
2011-06-11 20:06
Qingtian_2
Rank: 2
来 自:天津
等 级:论坛游民
帖 子:50
专家分:96
注 册:2011-3-9
收藏
得分:4 
SocketServer,我确定我不会~
2011-06-11 20:17
foxmansu
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-6-11
收藏
得分:0 
回复 楼主 foxmansu
额 继续等啊
2011-06-11 20:21
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:4 
chinese please

                                         
===========深入<----------------->浅出============
2011-06-11 20:25
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:4 
代码里都已经有注释了,看不懂就自己查查字典多好
2011-06-11 20:27
foxmansu
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-6-11
收藏
得分:0 
回复 5楼 voidx
我查了 有些词还是不明白啊....高手帮帮我吧
2011-06-11 21:02
烟雾中的迷茫
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:621
专家分:1069
注 册:2011-2-9
收藏
得分:4 
学习
2011-06-11 21:14
劣质数轴
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:89
专家分:163
注 册:2010-11-19
收藏
得分:4 
一般的程序是80代码20注释,这个程序已经是80注释20代码,已经够详细了
LZ看不懂是没有socket的知识,甚至C语言知识过于贫乏,建议去看看C语言和网络编程的书吧
2011-06-12 00:52
快速回复:本人大四在做毕设,马上答辩。急需知道下面这段程序的作用,忘高手前来 ...
数据加载中...
 
   



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

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