0

我有一个使用 2 个 C++ 文件的客户端 - 服务器模型。客户端和服务器通过套接字进行通信。用户向客户端的标准输入输入字符串,然后客户端将其传递给服务器。但是,我想使用 python 程序模拟用户。这是我的尝试:

from subprocess import Popen, PIPE, STDOUT

host_name = raw_input("Enter host name: ")
port = raw_input("Enter port: ")

p = Popen(["./client", host_name, port], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
p.stdin.write("DEVICE:3:MALFUNCTIONING")

这行不通。它启动客户端进程和参数,但不向其写入字符串 DEVICE:3:MALFUNCTIONING。有什么建议吗?

这是我的客户代码:

if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
        error("ERROR connecting");

printf("Please enter the message: ");
memset(buffer, 0, 256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
        error("ERROR writing to socket");
memset(buffer, 0, 256);
n = read(sockfd,buffer,255);
if (n < 0)
        error("ERROR reading from socket");
printf("%s\n",buffer);
close(sockfd);
4

1 回答 1

0

我不确定您是如何创建客户端和服务器的,但这是我的实现,它与模拟用户的 python 程序的实现几乎相同。

Github 要点:https ://gist.github.com/yanhan/5791613

根据您为客户提供的代码片段,我怀疑可能出了什么问题:

  1. server_addr 变量未正确设置
  2. 服务器代码中的错误

就像 mata 所说,如果您可以显示 stdout/stderr 和堆栈跟踪的任何输出,那将会更有帮助。

编辑:只是想补充一点,以下链接可能会有所帮助:

为了方便起见,我在这里粘贴我的代码。

sim.py:

from subprocess import Popen, PIPE

def run():
    # this should work if you change to
    #   host_name = raw_input
    #   port = raw_input
    host_name = '127.0.0.1'
    port = '8124'
    p = Popen(['./client', host_name, port], stdin=PIPE, close_fds=True)
    myInput = raw_input()
    p.stdin.write(myInput)
    p.communicate()

if __name__ == '__main__':
    run()

客户端.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define BUFSZ 256

int main(int argc, char *argv[])
{
    int sockfd, ret, exitCode = 0;
    char buffer[BUFSZ];
    memset(buffer, 0, BUFSZ);
    fgets(buffer, sizeof(buffer), stdin);
    char *hostname = argv[1];
    int port = atoi(argv[2]);
    // echo out the stuff just to see
    printf("hostname = %s\n", hostname);
    printf("port = %d\n", port);
    printf("msg = %s\n", buffer);
    // creates the socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) {
        fprintf(stderr, "client: socket() failed\n");
        exit(1);
    }
    struct sockaddr_in sockAddr;
    memset(&sockAddr, 0, sizeof(struct sockaddr_in));
    sockAddr.sin_family = AF_INET;
    sockAddr.sin_port = htons(port);
    sockAddr.sin_addr.s_addr = inet_addr(hostname);
    // connect to server
    ret = connect(sockfd, (const struct sockaddr *)&sockAddr, sizeof(struct sockaddr));
    if (ret == -1) {
        fprintf(stderr, "client: connect failed\n");
        exitCode = 1;
        goto done;
    }
    // send buffer
    ssize_t charsSent = send(sockfd, buffer, strlen(buffer)+1, 0);
    printf("client: chars sent: %d\n", (int)charsSent);
    /*
    // this seems to work as well
    write(sockfd, buffer, strlen(buffer)+1);
    */

done:
    close(sockfd);
    return exitCode;
}

服务器.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define PORTNUM 8124
#define BUFSZ 256

int main(int argc, char *argv[])
{
    char buf[BUFSZ];
    int ret, exitCode = 1;
    // create socket
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    int recvFd;
    if (sockfd == -1) {
        fprintf(stderr, "server: Error creating socket\n");
        exit(1);
    }
    struct sockaddr_in sockAddr;
    memset(&sockAddr, 0, sizeof(struct sockaddr_in));
    sockAddr.sin_family = AF_INET;
    sockAddr.sin_port = htons(PORTNUM);
    sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    // bind socket
    ret = bind(sockfd, (const struct sockaddr *)&sockAddr, sizeof(struct sockaddr));
    if (ret == -1) {
        fprintf(stderr, "server: bind failed\n");
        exitCode = 1;
        goto done;
    }
    // listen for connection. max of 1 connection
    ret = listen(sockfd, 1);
    if (ret == -1) {
        fprintf(stderr, "server: listen failed\n");
        exitCode = 1;
        goto done;
    }
    struct sockaddr_in dest;
    socklen_t sockLen;
    ssize_t bytesReceived;
    while (1) {
        // accept connection from client
        recvFd = accept(sockfd, (struct sockaddr *)&dest, &sockLen);
        if (recvFd == -1) {
            fprintf(stderr, "server: accept failed\n");
            exitCode = 1;
            break;
        }
        // receive message from client
        bytesReceived = recv(recvFd, buf, BUFSZ, 0);
        if (bytesReceived == -1) {
            fprintf(stderr, "server: recv failed\n");
            exitCode = 1;
            break;
        } else {
            printf("server: received %s", buf);
            fflush(stdout);
        }
    }
done:
    close(sockfd);
    return exitCode;
}
于 2013-06-16T10:29:52.257 回答