/docs/MyDocs

To get this branch, use:
bzr branch http://darksoft.org/webbzr/docs/MyDocs

« back to all changes in this revision

Viewing changes to Development/languages/C/Samples/sockets/domain/Bolvanka.C

  • Committer: Suren A. Chilingaryan
  • Date: 2009-04-09 03:21:08 UTC
  • Revision ID: csa@dside.dyndns.org-20090409032108-w4edamdh4adrgdu3
import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <iostream>
 
2
#include <fstream>
 
3
#include <vector>
 
4
#include <cmath>
 
5
#include <string>
 
6
#include <algorithm>
 
7
#include <stdio.h>
 
8
#include <sys/types.h>
 
9
#include <sys/wait.h>
 
10
#include <sys/stat.h>
 
11
#include <unistd.h>
 
12
#include <sys/socket.h>
 
13
//#include "internet.h"
 
14
 
 
15
#define saddr "/tmp/ds-4olga"
 
16
#define maxanswersize 1024
 
17
 
 
18
//sends query to the perl-script that sends it to altavista. 
 
19
 
 
20
using namespace std;
 
21
 
 
22
extern int errno;
 
23
 
 
24
void runparent(pid_t child) {
 
25
//        waitpid(child,NULL,0);
 
26
}
 
27
 
 
28
void runchild(char *command) {
 
29
        execl("/bin/bash","bash","-c",command,NULL);
 
30
        cerr<<"Error in runchild: "<<errno<<"\n";
 
31
        exit(0);
 
32
}
 
33
 
 
34
void request(int sd,char *req, char *answer) {
 
35
  int readed=strlen(req);
 
36
  
 
37
  req[readed]='\n';
 
38
  req[readed+1]=0;
 
39
  if (send(sd,req,strlen(req)+1,0)<=0) {
 
40
    printf("Error sending request, error: %i!\n",errno);
 
41
    unlink(saddr);
 
42
    exit(0);
 
43
  }
 
44
  req[readed]=0;
 
45
  readed=recv(sd,answer,maxanswersize,0);
 
46
  if (readed<=0) {
 
47
    printf("Error getting answer, error: %i!\n",errno);
 
48
    unlink(saddr);
 
49
    exit(0);
 
50
  }
 
51
  /* For unknown reason answer[0]==0??? 
 
52
  printf("%i\n>",readed);
 
53
  for(int i=0;i<readed;i++) printf("%i - %c;",i,answer[i]);
 
54
  printf("< %s\n",answer+1); */
 
55
 
 
56
  answer[readed]=0;
 
57
}
 
58
 
 
59
int main() {
 
60
  struct sockaddr mysock,perlsock;
 
61
  socklen_t i=sizeof(struct sockaddr);
 
62
  int s, sd;
 
63
  pid_t child;
 
64
  char buf[255],answer[maxanswersize];
 
65
  int j;
 
66
  
 
67
  s=socket(PF_UNIX,SOCK_STREAM,0);
 
68
  if (!s) {
 
69
    printf("Error creating socket, error: %i!\n",errno);
 
70
    exit(0);
 
71
  }
 
72
 
 
73
  mysock.sa_family=AF_UNIX;
 
74
  strcpy(mysock.sa_data,saddr);
 
75
  unlink(saddr);
 
76
  if (bind(s,&mysock,3+sizeof(saddr))==-1) {
 
77
    printf("Error binding socket to file, error %i!\n",errno);
 
78
    exit(0);
 
79
  }
 
80
 
 
81
  if (listen(s,1)<0) {
 
82
    printf("Error listening, error %i!\n",errno);
 
83
    unlink(saddr);
 
84
    exit(0);
 
85
  }
 
86
 
 
87
  child=fork();
 
88
  if (child>0) runparent(child);
 
89
  else if (child==0) runchild("./Bolvanka.pl");
 
90
  else {
 
91
    cerr<<"Error forking child, error: "<<errno<<"\n";
 
92
    unlink(saddr);
 
93
    exit(0);
 
94
  }
 
95
 
 
96
  sd=accept(s,&perlsock,&i);
 
97
  if (sd<0) {
 
98
    printf("Error accepting connection, error: %i!\n",errno);
 
99
    unlink(saddr);
 
100
    exit(0);
 
101
  }
 
102
  
 
103
  for (i=0;i<20;i++) {
 
104
    sprintf(buf,"Hello %i",i);
 
105
    request(sd,buf,answer);
 
106
    printf("Request: %s, Answer: %s\n",buf,answer[0] ? answer : answer+1); 
 
107
        // +1 for unknown reason??? In faq is full silence...
 
108
  }
 
109
  
 
110
  unlink(saddr);
 
111
}
 
112
 
 
 
b'\\ No newline at end of file'