【发布时间】:2016-02-24 05:59:55
【问题描述】:
我正在用 C 实现一个套接字程序。
节目说明:
服务器和客户端连接后,客户端向服务器发送命令。
服务器接收客户端的请求并执行。
服务器将执行结果发送给客户端。
客户端将显示该服务器执行并显示。
我的问题:
服务器:
我将 execvp() 函数的结果放入管道 pipefd[1] 并使用 @ 将此数据发送到客户端987654323@.
客户:
我收到recv() 数据到服务器并显示。
但它不起作用。
我想要的结果:
客户:ls
服务器:
a.txt b.pdf c.jpg
客户:
a.txt b.pdf c.jpg
这是我的代码:
服务器:
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#define MAXPENDING 5
#define BUFFSIZE 32
#define MAX 128
void Die(char *mess)
{
perror(mess);
exit(1);
}
void setup(char inputBuffer[], char *args[], int *background) //Ham xu li (cat) chuoi lenh
{
const char s[4] = " \t\n";
char *token;
token = strtok(inputBuffer, s);
int i = 0;
while( token != NULL)
{
args[i] = token;
i++;
//printf("%s\n", token);
token = strtok(NULL,s);
}
args[i] = NULL;
}
void HandeClient(int sock){
char buffer[BUFFSIZE];
int received = -1;
char data[MAX];
data[0] = '\0';
/*recv() from client;*/
if((received = recv(sock, buffer,BUFFSIZE,0))<0){
Die("Failed");
}
buffer[received] = '\0';
strcat (data, buffer);
while(received>0)
{
/* send() from server; //acknowledge to client */
if(send(sock,buffer, received,0)!= received){
Die("Failed");
}
/* recv() from client; */
if((received=recv(sock,buffer,BUFFSIZE,0))<0){
Die("Failed");
}
buffer[received] = '\0';
strcat (data, buffer);
}
puts (data);
{
char *args[100];
setup(data,args,0);
// pipe
int pipefd[2],lenght;
pipe(pipefd);
pid_t pid = fork();
char path[MAX];
if(pid>0)
{
// parent process // write to pipe
execvp(args[0],args);
dup2(1,pipefd[1]);
close(pipefd[0]);
write(STDOUT_FILENO,pipefd[1],strlen(pipefd[1]));
close(pipefd[1]);
while (wait(NULL) != pid);
}
else
if(pid==0)
{
//child process // read from pipe
close(pipefd[1]);
while(lenght=read(pipefd[0],path,1) > 0){
// send the result of execvp for client.
if(send(sock,path,strlen(path),0) != strlen(path) ){
Die("Failed");
}
}
close(pipefd[0]);
exit(1);//
}
else
{
printf("Error !\n");
exit(0);//
}
}
if (strcmp (data, "exits")==0)
{
exit (1);
}
close(sock);
}
int main(int argc, char const *argv[])
{
int serversock,clientsock;
struct sockaddr_in echoserver, echoclient;
/*if(argc !=2)
{
fprintf(stderr, "USAGE: echoserver <port>\n");
exit(0);
}*/
if((serversock = socket(PF_INET, SOCK_STREAM,IPPROTO_TCP))<0){
Die("Failed");
}
memset(&echoserver,0,sizeof(echoserver));
echoserver.sin_family = AF_INET;
echoserver.sin_addr.s_addr= htonl(INADDR_ANY);
echoserver.sin_port = htons(atoi(argv[1]));
if(bind(serversock, (struct sockaddr *) & echoserver,sizeof(echoserver))<0){
Die("Failed");
}
if(listen(serversock,MAXPENDING)<0){
Die("Failed");
}
while(1)
{
unsigned int clientlen = sizeof(echoclient);
if((clientsock =
accept(serversock,(struct sockaddr *) &echoclient,
&clientlen))<0){
Die("Failed");
}
fprintf(stdout, "Client connected: %s\n",
inet_ntoa(echoclient.sin_addr));
fprintf(stdout,"Message from client:");
HandeClient(clientsock);
}
return 0;
}
客户:
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#define BUFFSIZE 32
/// Client chuyen mot chuoi ki tu, sau khi an enter n gui cho server roi ket thuc
void Die(char *mess)
{
perror(mess);
exit(1);
}
int main(int argc, char *argv[])
{
while(1){
int sock;
struct sockaddr_in echoserver;
char buffer[BUFFSIZE];
unsigned int echolen;
int received = 0;
if (argc != 3)
{
fprintf(stderr, "USAGE: TCPecho <server_ip> <word> <port>\n");
exit(1);
}
/* Create the TCP socket */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
Die("Failed to create socket");
}
/* Construct the server sockaddr_in structure */
memset(&echoserver, 0, sizeof(echoserver));
echoserver.sin_family = AF_INET;
echoserver.sin_addr.s_addr = inet_addr(argv[1]);
echoserver.sin_port = htons(atoi(argv[2]));
/* Establish connection */
if (connect(sock,(struct sockaddr *) &echoserver,sizeof(echoserver)) < 0)
{
Die("Failed to connect with server");
}
/* Send the word to the server */
//tao mot chuoi s, chuoi s nhan ki tu tu ban phim, roi chuyen cho phia server
char s[100];
fgets(s,100,stdin);
echolen = strlen(s);
/* send() from client; */
if (send(sock, s, echolen, 0) != echolen)
{
Die("Mismatch in number of sent bytes");
}
/* Receive the word back from the server */
fprintf(stdout, "Message from server: ");
while (received < echolen)
{
int bytes = 0;
/* recv() from server; */
if ((bytes = recv(sock, buffer, BUFFSIZE-1, 0)) < 1)
{
Die("Failed to receive bytes from server");
}
received += bytes;
buffer[bytes] = '\0';
/* Assure null terminated string */
fprintf(stdout, buffer);
}
fprintf(stdout, "\n");
close(sock);
if(strcmp(s,"exit") == 0)
exit(0);
}
}
【问题讨论】:
-
任何超过
execvp()的语句将永远不会被执行。只要父进程调用execvp(),新进程就会替换父进程。所有exec*()家庭永远不会返回给调用者。这必须是进程中的最后一次调用,然后该进程被视为已消失(由新进程替换)。将调用移至while循环之前的execvp()并删除while循环。