我不确定您是如何创建客户端和服务器的,但这是我的实现,它与模拟用户的 python 程序的实现几乎相同。
Github 要点:https ://gist.github.com/yanhan/5791613
根据您为客户提供的代码片段,我怀疑可能出了什么问题:
- server_addr 变量未正确设置
- 服务器代码中的错误
就像 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;
}