/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/fork/socket-unfinished.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 <stdio.h>
 
2
#include <sys/types.h>
 
3
#include <sys/stat.h>
 
4
#include <fcntl.h>
 
5
#include <unistd.h>
 
6
#include <sys/socket.h>
 
7
 
 
8
extern int errno;
 
9
int f[2];
 
10
 
 
11
void sp_handler(int a) {
 
12
        puts("SIG");
 
13
}
 
14
 
 
15
void runparent(pid_t child,int s) {
 
16
        char buf[255];
 
17
        int readed=1;
 
18
        int writed;
 
19
 
 
20
        while (readed>0) {
 
21
                puts("Pre");
 
22
                readed=read(s,buf,254);
 
23
                printf("Here %i %i\n",readed,errno);
 
24
                if (readed>0) writed=write(0,buf,readed);
 
25
        }
 
26
 
 
27
        // Waiting for child to be finished!
 
28
        waitpid(child,NULL,0);
 
29
 
 
30
}
 
31
 
 
32
void runchild() {
 
33
        char fl[24];
 
34
        int err;
 
35
        char buf[100]="Helo!\n\0";
 
36
 
 
37
        write(f[1],buf,6);
 
38
}
 
39
 
 
40
main() {
 
41
        struct sockaddr mysock;
 
42
        pid_t child;
 
43
        int s;
 
44
        
 
45
        s=socket(PF_UNIX,SOCK_STREAM,0);
 
46
        if (!s) {
 
47
                printf("Error creating socket, error: %i!\n",errno);
 
48
                return;
 
49
        }
 
50
 
 
51
        mysock.sa_family=AF_UNIX;
 
52
        strcpy(mysock.sa_data,"/tmp/ds");
 
53
        if (bind(s,&mysock,3+sizeof("/tmp/ds"))==-1) {
 
54
                printf("Error binding socket to file, error %i!\n",errno);
 
55
                return;
 
56
        }
 
57
        
 
58
        child=fork();
 
59
        if (child>0) runparent(child,s);
 
60
        else if (child==0) runchild();
 
61
        else printf("Error forking child, error: %i!\n",errno);
 
62
}