【问题标题】:Client and Server Socket Programming in C UnixC Unix 中的客户端和服务器套接字编程
【发布时间】:2014-03-03 02:24:43
【问题描述】:

我在同一台机器上有一个客户端和一个服务器(使用套接字)。一个命令应该从客户端发送到服务器,例如ls -l /

服务器应该接收到命令,在 shell 中运行它并将结果发送回客户端。用户应该在他的屏幕上看到结果(在这种情况下是ls -l的结果),例如在客户端的屏幕上。

当您键入 ls -l /dir / 时,文件列表会出现在服务器屏幕上,而不是客户端屏幕上。

有什么问题?

PS。要测试程序,您必须编译在单独的终端上运行的客户端和服务器 (gcc -o myclient client.c)。

/* client
** echoc.c -- the echo client for echos.c; demonstrates unix sockets
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#define SOCK_PATH "echo_socket"

int main(void)
{

  int s, t, len;
  struct sockaddr_un remote;
  char str[100];

  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }

  printf("Client is trying to connect...\n");

  remote.sun_family = AF_UNIX;
  strcpy(remote.sun_path, SOCK_PATH);
  len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  if (connect(s, (struct sockaddr *)&remote, len) == -1) {
    perror("connect");
    exit(1);
  }

  printf("Connected.\n");

  while(printf("> "), fgets(str, 100, stdin), !feof(stdin)) {
    if (send(s, str, strlen(str), 0) == -1) {
      perror("send");
      exit(1);
    }

    if ((t=recv(s, str, 100, 0)) > 0) {
      str[t] = '\0';
      printf("Result> %s", str);
    } else {
      if (t < 0) perror("recv");
      else printf("Server closed connection\n");
      exit(1);
    }
  }

  close(s);

  return 0;
}

服务器:

      /*
** echos.c -- the echo server for echoc.c; demonstrates unix sockets
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdbool.h>

#define SOCK_PATH "echo_socket"



void doit(char *client_message,char **argv)
{

    while (*client_message != '\0') //kör tills man stöter på '\0' (end of line)
    {
        while (*client_message == ' ' || *client_message == '\t' || *client_message == '\n') //så länge den nuvarande karaktären i filen är ' ', '\t' eller '\n'
            *client_message++ = '\0'; //sätt den till '\0'
        *argv++ = client_message; //gå vidare till nästa karaktär
        while (*client_message != '\0' && *client_message != ' ' &&
                *client_message != '\t' && *client_message != '\n')
            client_message++;
    }
    *argv = '\0';
}

int runit(char **argv)
{


    pid_t child;
    int status;
    bool waiting;
    child=fork();
    if (child==0)
    {
        if(execvp(*argv,argv)<0)
            return 0 ;
    }
    else if (child <0)
    {
        return -1;

    }
    else
    {
        waiting = true;
        while(waiting)
        {
            if(wait(&status))
                waiting = false;
        }
    }
    return 1;
}



int main(void)

{
    int s, s2, t, len,*argv[80];
    struct sockaddr_un local, remote;
    char str[100];

    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
    {
        perror("socket");
        exit(1);
    }

    local.sun_family = AF_UNIX;
    strcpy(local.sun_path, SOCK_PATH);
    unlink(local.sun_path);
    len = strlen(local.sun_path) + sizeof(local.sun_family);
    if (bind(s, (struct sockaddr *)&local, len) == -1)
    {
        perror("bind");
        exit(1);
    }

    if (listen(s, 5) < 0)
    {
        perror("listen");
        exit(1);
    }

    for(;;)
    {
        int done, n;
        printf("Waiting for a connection...\n");
        t = sizeof(remote);
        if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1)
        {
            perror("accept");
            exit(1);
        }

        printf("Connected.\n");

        done = 0;
        do
        {
            n = recv(s2, str, 100, 0);
            if (n <=0)
            {
                if (n < 0) perror("recv");
                done = 1;
            }

            if (!done)
            {

                /** a call doit from here **/

                doit(str,argv);
                runit(argv);



                if (send(s2, argv, n, 0) < 0)
                {
                    perror("send");
                    done = 1;
                }
            }
        }
        while (!done);

        close(s2);
    }

    return 0;
}

【问题讨论】:

  • 帮助我们帮助您 - 提供具体问题。 “我不能再进一步了”不是一个建设性的问题。
  • 首先,欢迎来到 StackOverflow。我想帮助你,但你没有详细说明你的问题是什么。这个特定的代码不起作用吗?如果不是,它是如何损坏的,您希望代码如何工作。尝试并做到具体。
  • 现在可以理解了吗?
  • ` 用户输入 ls -1 作为命令,屏幕上将显示文件列表,`您的客户端在哪里?
  • 代码在上面,伙计!看看PLZ! (服务器和客户端都在那里):))

标签: c sockets unix client


【解决方案1】:
  1. ls -l传输到服务器
  2. 做一个popen()。例如:fp = popen(cmd, "r");
  3. 将指针@​​987654323@的内容传递给客户端。

【讨论】:

    猜你喜欢
    • 2016-02-09
    • 2014-07-21
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 2016-02-23
    • 1970-01-01
    相关资源
    最近更新 更多