![](/img/trans.png)
[英]UNIX Socket Programming: Issues Getting a C Client to Connect to a Python Server
[英]Client and Server Socket Programming in C Unix
我在同一台计算机上(使用套接字)有一个客户端和一个服务器。 应该从客户端向服务器发送命令,例如ls -l /
。
服务器应接收该命令,并在外壳中运行该命令,并将结果发送回客户端。 用户应该在自己的屏幕上(例如在客户端的屏幕上)看到结果(在本例中为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;
}
popen()
。 例如: fp = popen(cmd, "r");
fp
的内容传输到客户端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.