Liờn l衣c gi英a ti院n trỡnh cha và ti院n trỡnh con

Một phần của tài liệu Giáo trình Hệ thống mạng Linux (Trang 31 - 37)

Trong vớ d映 d逢噂i 8õy, m瓜t ti院n trỡnh t衣o ra m瓜t 嘘ng d磯n, t衣o ra m瓜t ti院n trỡnh con, vi院t m瓜t v<n b違n vào 嘘ng d磯n.Ti院n trỡnh con th瑛a h逢荏ng 嘘ng d磯n và cỏc ký hi羽u mụ t違 c栄a 嘘ng d磯n, th詠c hi羽n 8丑c trong 嘘ng d磯n:

#include <errno. h> #include <stdio. h>

void code_fils(int number) { int fd, nread;

fd=number;

printf(" So hieu mo ta la %d\n",fd);

switch (nread=read(fd, texte, sizeof(texte))) { case -1: perror("Loi doc."); case 0: perror("EOF"); default:

printf("Van ban nhan duoc co %d ky tu: %s\n",fd, texte); } } main() { int fd[2]; char chaine[10]; if (pipe(fd)==-1)

{ perror("Loi khoi tao pipe."); exit(1);

}

switch (fork()) { case -1:

perror(" Loi khoi tao tien trinh."); break; case 0: if (close(fd[1])==-1) perror(" Error."); code_fils(fd[0]); exit(0); } close(fd[0]); if (write(fd[1]),"hello",6)==-1) perror("Loi truyen."); } M院t qu違 ch逢挨ng trỡnh: So hieu mo ta la: 5

Van ban nhan duoc co 6 ky tu: hello

Chỳ ý r茨ng, ti院n trỡnh con 8丑c trong 嘘ng d磯n mà khụng vi院t 荏"8ú nờn nú b逸t 8亥u d茨ng cỏch 8úng ph亥n vi院t fd[1] 8吋 ti院t ki羽m cỏc tớn hi羽u mụ t違 c栄a t鰻 h嬰p. T逢挨ng t詠, vỡ ti院n trỡnh cha ch雨 s穎 d映ng ph亥n vi院t nờn nú 8úng ph亥n 8丑c l衣i (fd[0]). Sau 8ú ti院n trỡnh cha vi院t vào 嘘ng d磯n 6 ký t詠 và ti院n trỡnh con 8ó 8丑c chỳng.

Bài 9 N壱p trỡnh mng TCP/IP

9.1. Lp trỡnh client /server theo giao thc TCP/IP

Ch⇔¬ng trỡnhtcpClient.c

/ * Khai bỏo cỏc file t h v in c n t hit 8; gi hàm socket * /

#include <sys/types.h> #include <sys/socket.h>

#include <netinet/in.h> / * get host by nam e* /

#include <arpa/inet.h> #include <netdb.h> #include <stdio.h>

#include <unistd.h> / * close * /

#define SERVER_PORT 1500 #define MAX_MSG 100

int main (int argc, char *argv[]) {

/ * Khi t o cỏc bin dựng t r ong ch ng t r ỡnh * /

int sd, rc, i;

struct sockaddr_in localAddr, servAddr; struct hostent *h;

if(argc < 3) {

printf("usage: %s <IPserver> <data1> <data2> ... <dataN>\n",argv[0]); exit(1);

}

/ * Hàm get host byn am e( ) l y v・"8^a ch I P t h eo t ờn n h p vào t r ong t p t in / et c/ host s * /

h = gethostbyname(argv[1]); if(h==NULL) {

printf("%s: unknown host '%s'\n",argv[0],argv[1]); exit(1);

}

servAddr.sin_family = h->h_addrtype;

memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); servAddr.sin_port = htons(SERVER_PORT);

/ * Gỏn cỏc giỏ t r cho 8えi t ng socket .

T o socket cho m ỏy Client . L u l i s m ụ t socket * /

sd = socket(AF_INET, SOCK_STREAM, 0); if(sd<0) {

perror("cannot open socket "); exit(1);

}

/ * A t t ờn socket cho ch ng t r ỡn h Client

Gỏn 8^a ch kt ni cho socket t heo giao t hc I nt er net * /

localAddr.sin_family = AF_INET;

localAddr.sin_addr.s_addr = htonl(INADDR_ANY); localAddr.sin_port = htons(0);

/ * Hàm ht ons( ) dựng 8; chu yn 8がi t r t t by t e ca s nguy ờn t r c kh i gi 8i do h

t hng s dng c ch giao t ip TCP/ I P * / / * Ràng buc t ờn vi socket * /

rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr)); if(rc<0) {

printf("%s: cannot bind port TCP %u\n",argv[0],SERVER_PORT); perror("error ");

exit(1); }

/ * Thc hin kt ni 8,n ser v er t heo t ờn/8^a ch n h p v ào t dũng lnh * /

rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr)); if(rc<0) {

perror("cannot connect "); exit(1);

}

/ * Sau khi socket 8ó kt ni, t hc h in gi cỏc d liu 8,n ch ng t r ỡnh Ser v er * /

for(i=2;i<argc;i++) {

rc = send(sd, argv[i], strlen(argv[i]) + 1, 0);

if(rc<0) {

perror("cannot send data "); close(sd);

exit(1);

}/ * if * /

printf("%s: data%u sent (%s)\n",argv[0],i-1,argv[i]);

}/ * for * /

return 0; }/*main*/

Ch⇔¬ng trỡnh tcpServer.c

/ * Ch u on g t r in h t cpSe r v e r .c * /

/ * Khai bỏo cỏc file t h v in c n t hit 8; gi hàm socket * /

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

#include <unistd.h> / * close * /

#define SUCCESS 0 #define ERROR 1 #define END_LINE 0x0 #define SERVER_PORT 1500 #define MAX_MSG 100 /* function readline */ int read_line();

int main (int argc, char *argv[]) {

int sd, newSd, cliLen;

struct sockaddr_in cliAddr, servAddr; char line[MAX_MSG];

/ * Gỏn cỏc giỏ t r cho 8えi t ng socket .

T o socket cho m ỏy Ser v er . L u l i s m ụ t socket * /

sd = socket(AF_INET, SOCK_STREAM, 0); if(sd<0) {

perror("cannot open socket "); return ERROR;

}

/ * A t t ờn socket cho ch ng t r ỡn h Ser v er

Gỏn 8^a ch kt ni cho socket t heo giao t hc I nt er net * /

servAddr.sin_family = AF_INET;

servAddr.sin_addr.s_addr = htonl(INADDR_ANY); servAddr.sin_port = htons(SERVER_PORT);

if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) { perror("cannot bind port ");

return ERROR; } /* V o hàng 8ずi l ng n ghe kt ni ca client Cho phộp hàng 8ずi nh n ti 8a 5 kt ni */ listen(sd,5); / * L p liờn tc ch v à lx y kt ni ca client * / while(1) {

printf("%s: waiting for data on port TCP %u\n",argv[0],SERVER_PORT); cliLen = sizeof(cliAddr);

/* Ch p nh n kt ni * /

newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen); if(newSd<0) {

perror("cannot accept connection "); return ERROR; } /* init line */ memset(line,0x0,MAX_MSG); /* Aいc d liu do Client gi 8,n - x lý d liu n h n 8 ずc */ while(read_line(newSd,line)!=ERROR) {

printf("%s: received from %s:TCP%d : %s\n", argv[0], inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), line);

/* init line */

} /* while(read_line) */

} /* while (1) */

}

/ * WARNI NG * /

/ * t his funct ion is ex per im ent al. I don't k now y et if it w or ks * /

/ * cor r ect ly or not . Use St ev en's r eadlin e( ) fun ct ion t o hav e som et hing r obust .* / / * r cv _line is m y f unct ion r eadline( ) . Dat a is r ead fr om t he socket w hen * /

/ * needed, but not by t e aft er by t es. All t he r eceiv ed dat a is r ead. * /

/ * Th is m eans only one call t o r ecv ( ) , inst ead of one call for each r eceiv ed by t e. * / / * You can set END_CHAR t o w hat ev er m eans en dofline f or y ou. ( 0x 0A is \ n) * / / * r ead_lin r et ur ns t he num ber of by t es r et u r ned in lin e_t o_r et ur n * /

/* Hàm cú chc n<ng 8いc d liu t socket*/ int read_line(int newSd, char *line_to_return) {

static int rcv_ptr=0;

static char rcv_msg[MAX_MSG]; static int n;

int offset;

offset=0;

while(1) {

if(rcv_ptr==0) {

/* read data from socket */

memset(rcv_msg,0x0,MAX_MSG); /* init buffer */ n = recv(newSd, rcv_msg, MAX_MSG, 0); /* wait for data */ if (n<0) {

perror(" cannot receive data "); return ERROR;

} else if (n==0) {

printf(" connection closed by client\n"); close(newSd);

return ERROR; }

}

/* if new data read on socket OR if another line is still in buffer */ /* copy line into 'line_to_return' */

while(*(rcv_msg+rcv_ptr)!=END_LINE && rcv_ptr<n) { memcpy(line_to_return+offset,rcv_msg+rcv_ptr,1); offset++;

rcv_ptr++; }

/* end of line + end of buffer => return line */ if(rcv_ptr==n-1) {

/* set last byte to END_LINE */ *(line_to_return+offset)=END_LINE; rcv_ptr=0;

return ++offset; }

/* end of line but still some data in buffer => return line */ if(rcv_ptr <n-1) {

/* set last byte to END_LINE */ *(line_to_return+offset)=END_LINE; rcv_ptr++;

return ++offset; }

/* end of buffer but line is not ended => */ /* wait for more data to arrive on socket */ if(rcv_ptr == n) {

rcv_ptr = 0; }

} / * w hile * /

}/ * m ain* /

Một phần của tài liệu Giáo trình Hệ thống mạng Linux (Trang 31 - 37)

Tải bản đầy đủ (PDF)

(47 trang)