socket

simple server and client 

may be useful for copy in the future


server:

#include <errno.h>
#include "sys/types.h"
#include "sys/socket.h"
#include "sys/stat.h"
#include "unistd.h"
#include <netinet/in.h>
//#include <arpa/inet.h>
#include <fcntl.h>

#define SERVER_PORT (56738)
#define SERVER_IP "127.0.0.1"
#define LOG printf

static void handle_crashing_process_new(int fd)
{
    int n;
    unsigned int tid;
    int length = getpid();
    int retry = 30;
    printf("handle_crashing_process_new in\n");
    while((n = read(fd, &tid, sizeof(unsigned))) != sizeof(unsigned)) {
        if(errno == EINTR) continue;
        if(errno == EWOULDBLOCK) {
            if(retry-- > 0) {
                usleep(100 * 1000);
                continue;
            }
            LOG("timed out reading tid\n");
            goto done;
        }
        //LOG("read failure %s\n", strerror(errno));
        //goto done;
    }
    printf("getpid =%d\n",tid);
    n = send(fd, &length,sizeof(length),0);
    if(n < 0){ 
        printf("send failure %s\n", strerror(errno));
        goto done;
    }
    done:
        return;
}
int main(int argc, char** argv)
{
    int s;
    struct sockaddr_in addr;  
    //bzero(&addr,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(SERVER_PORT);
    addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    
    s = socket(AF_INET,
            SOCK_STREAM, 0);
    if(s < 0){ 
        printf("socket failure %s\n", strerror(errno));
        goto done;
    }
    //fcntl(s, F_SETFD, FD_CLOEXEC);

    //LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
    //printf("pid=%d,tid=%d\n",getpid(),gettid());
    
    if(bind(s, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0) 
    {  
        printf("bind error %s\n",strerror(errno));
        goto done;    
    }
    
    if (listen(s,10))
    {
        printf("listen error %s\n",strerror(errno));
        goto done;    
    }
    
    for(;;) {
        struct sockaddr addr;
        socklen_t alen;
        int fd;

        alen = sizeof(addr);
        printf("before accept\n");
        fd = accept(s, &addr, &alen);
        printf("after accept\n");
        if(fd < 0) {
            printf("accept failed: %s\n", strerror(errno));
            continue;
        }
        //fcntl(fd, F_SETFD, FD_CLOEXEC);
        handle_crashing_process_new(fd);
        close(fd);
        fd = -1;
    }
    done:
    close(s);
    
    return 0;
}



client


#include <stdio.h>
#include <errno.h>
#include "sys/types.h"
#include "sys/socket.h"
#include "sys/stat.h"
#include "unistd.h"
//#include <sys/syscall.h>  
#include <netinet/in.h>
//#include <arpa/inet.h>
#include <fcntl.h>

  
#define gettid() ((pid_t) syscall(SYS_gettid)) 
#define SERVER_PORT 56738
#define SERVER_IP "127.0.0.1"
#define LOG printf

 void sendPidtoServer()
{
    int s;
    int pid, tid;
    int datalen=0,retry=30;
    printf("333 pid\n");
    pid=getpid();
    tid=pid;//gettid();
    int n = 0;
    printf("pid=%d\n",pid);
    struct sockaddr_in addr;  
    //bzero(&addr,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(SERVER_PORT);
    addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    
    s = socket(AF_INET, SOCK_STREAM, 0);
    if (s<0)
    {
        printf(" socket error %s", strerror(errno));
        goto done;
    }
    n= connect(s,(struct sockaddr*)(&addr),sizeof(addr));
    if (n!=0)
    {
        printf(" connect error %s", strerror(errno));
        goto done;
    }
    n = send(s,&tid,sizeof(tid),0);
    if (n<0)
    {
        printf(" connect error %s\n", strerror(errno));
        goto done;
    }
    while((n = read(s, &datalen, sizeof(datalen))) != sizeof(datalen)) {
        if(errno == EINTR) continue;
        if(errno == EWOULDBLOCK) {
            if(retry-- > 0) {
                usleep(100 * 1000);
                continue;
            }
            LOG("timed out reading tid\n");
            goto done;
        }
    }    
    printf("get length=%d\n",datalen);
    sleep(1);
    done:
    close(s);
}

int main(int argc, char** argv)
{
    printf("socket client\n");
    sendPidtoServer();
    return 0;
}




你可能感兴趣的:(Date,struct,Stream,server,socket)