免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2283 | 回复: 9
打印 上一主题 下一主题

关于linux下服务器端程序设计 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-05 10:11 |只看该作者 |倒序浏览
我想写一个LINUX下服务器程序,只想实现很简单的功能,此程序运行后,能够一直监听某一个端口,等待客房端的连接,客房端发送一个命令(如:ls),然后把服务器端目录在客房端上列出来就可以了。就想实现这个一个小小的功能,但自己能力有现,不知道应该怎么实现,希望大虾指点。如果有这方面的源码,也可以让小弟看看,研究一下。

论坛徽章:
0
2 [报告]
发表于 2006-05-05 12:48 |只看该作者
问一下你用来做什么?

论坛徽章:
0
3 [报告]
发表于 2006-05-05 13:25 |只看该作者
不需要什么,用守护程序去调用shell,

如果非要自己写,如果实现外部命令就直接学着linux的xinetd,否则应该修改shell的原代码。

论坛徽章:
0
4 [报告]
发表于 2006-05-05 21:13 |只看该作者
因为自己从来没做过,不知道应该怎么下手

论坛徽章:
0
5 [报告]
发表于 2006-05-05 22:12 |只看该作者
先连接,然后dup2 然后fork

论坛徽章:
0
6 [报告]
发表于 2006-05-06 13:29 |只看该作者
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>


#define S_PORT 6688
#define MAX_BUFFER 1024


int
main (int argc, char *argv[])
{
    char buffer[MAX_BUFFER];
    char cmd[MAX_BUFFER];
    struct sockaddr_in server;


    /* Our process ID and Session ID */
    pid_t pid, sid;


    if (argc < 2)
      {
          printf ("Start to receive message....\n");
      }

    s = socket (AF_INET, SOCK_DGRAM, 0);

    if (s == -1)
      {
          fprintf (stderr, "OPEN SOCKET ERROR!\n");
          exit (1);
      }

    addrlen = sizeof (server);
    memset (&server, '\0', addrlen);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl (INADDR_ANY);
    server.sin_port = htons (S_PORT);

    r = bind (s, (struct sockaddr *) &server, sizeof (server));

    if (r == -1)
      {
          fprintf (stderr, "BIND ERROR!");
          exit (0);
      }


    /* Fork off the parent process */
    pid = fork ();
    if (pid < 0)
      {
          exit (EXIT_FAILURE);
      }
    /* If we got a good PID, then
     *            we can exit the parent process. */
    if (pid > 0)
      {
          exit (EXIT_SUCCESS);
      }

    /* Change the file mode mask */
    umask (0);

    /* Open any logs here */
    openlog("Server", LOG_PID, LOG_USER);
    /* Create a new SID for the child process */
    sid = setsid ();
    if (sid < 0)
      {
          /* Log the failure */
          syslog(LOG_ERR , "SETSID error!");
          exit (EXIT_FAILURE);
      }

    /* Change the current working directory */
    if ((chdir ("/")) < 0)
      {
          /* Log the failure */
          exit (EXIT_FAILURE);
      }

    /* Close out the standard file descriptors */
    close (STDIN_FILENO);
    close (STDOUT_FILENO);
    close (STDERR_FILENO);

    while (1)
      {
          syslog(LOG_INFO, "Waiting for message....");
          r = recvfrom (s, &buffer, MAX_BUFFER, 0,
                        (struct sockaddr *) &server, &addrlen);

          if (r == -1)
            {
                fprintf (stderr, "RECEIVE ERROR!");
                exit (0);
            }
         

          /* put your code here */
      }

    close (s);

    exit (0);


}

论坛徽章:
0
7 [报告]
发表于 2006-05-06 23:39 |只看该作者
谢谢呀

论坛徽章:
1
操作系统版块每日发帖之星
日期:2016-04-19 06:20:00
8 [报告]
发表于 2006-05-07 23:13 |只看该作者

用C怎样关机和重启?

用C调用什么函数能重启rh9和关机?
最好给个例子,
请大家帮帮我,谢谢了。

论坛徽章:
0
9 [报告]
发表于 2006-05-08 13:23 |只看该作者
system ("reboot");

system ("poweroff");

论坛徽章:
0
10 [报告]
发表于 2006-05-08 13:36 |只看该作者
man 2 reboot
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP