send.cpp

extern "C" {
   #include "unpthread.h"
}
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<math.h>
#include<sys/uio.h>
#include<time.h>
#include<string.h>
#include<sys/timeb.h>
#include <netinet/tcp.h>
#include <semaphore.h>
#include <sys/un.h>

#define SERVER_PATH     "/home/informix/test/unixdomain/unixdomain"
int iConn = 0 ;

int main(int argc, char **argv)
{
    if( argc != 2 ){
        printf("run like : ./send.exe 1111111111 \n") ;
        exit( 0 ) ;
    }
    int sd = -1 ;
    int rc,length;
    const int   on = 1;
    char   buffer[2048];
    struct sockaddr_un serveraddr;

    sd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (sd < 0)
    {
       printf("socket() failed");
       exit(0) ;
    }
    memset(&serveraddr, 0x00 , sizeof(serveraddr));
    serveraddr.sun_family = AF_UNIX;
    strcpy(serveraddr.sun_path, SERVER_PATH);

    rc=connect(sd,(struct sockaddr *)&serveraddr,sizeof(struct sockaddr_un));
    if (rc < 0)
    {
        printf("connect() failed");
        exit(0) ;
    }
    int onex = 1 ;
    setsockopt(sd,IPPROTO_TCP,TCP_NODELAY,&onex,sizeof(onex));
    iConn = sd ;
    char msg[128]={0}  ;
    strcpy(msg,argv[1]) ;
    while( 1 ){
       if (send(iConn,msg,strlen(msg),MSG_NOSIGNAL|MSG_DONTWAIT|MSG_DONTROUTE)<0){
            printf("send to port 4600 error...\n");
            exit(0) ;
        }
        usleep( 1 ) ;
    } //while
} //main

recv.cpp

extern "C" {
   #include "unpthread.h"
}
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<math.h>
#include<sys/uio.h>
#include<time.h>
#include<string.h>
#include<sys/timeb.h>
#include <netinet/tcp.h>
#include <semaphore.h>
#include <sys/un.h>
#include <assert.h>

#define SERVER_PATH     "/home/informix/test/unixdomain/unixdomain"
int connfd = 0 ;

int main(int argc, char **argv)
{
    int sd = -1 ;
    int    rc, length;
    const int   on = 1;
    struct sockaddr_un serveraddr;

    sd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (sd < 0)
    {
       printf("socket() failed");
       exit(0) ;
    }
    setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
    memset(&serveraddr, 0x00 , sizeof(serveraddr));
    serveraddr.sun_family = AF_UNIX;
    strcpy(serveraddr.sun_path, SERVER_PATH);

    unlink(SERVER_PATH);
    rc = bind(sd, (struct sockaddr *)&serveraddr, sizeof(struct sockaddr_un));
    if (rc < 0)
    {
       printf("bind() failed");
       exit(0) ;
    }
    rc = listen(sd, 10);
    if (rc< 0)
    {
        printf("listen() failed");
        exit(0) ;
    }
    printf("Ready for client connect().\n");
    connfd = -1 ;

    pthread_t tid;
    void   *doit(void *);
    for ( ; ; ) {
        connfd = accept(sd,NULL,NULL);
        if(connfd < 0)
        {
            printf("accept failed..\n");
            exit(0) ;
        }
        Pthread_create(&tid, NULL, &doit, (void *)(long) connfd);
   }
   unlink(SERVER_PATH);
}

void * doit(void *arg)
{
    void web_child(long);

    Pthread_detach(pthread_self());
    web_child((long) arg);
    Close((long) arg);
    printf("thread [%05d] dead...\n",(long) arg);
    return(NULL);
}

void web_child(long sockfd)
{
    ssize_t nread;
    for ( ; ; ) {
        char line[2048]={0} ;
        if ( (nread = recv(sockfd,line,2047,MSG_NOSIGNAL)) <= 0)
        {
            break;
        }
        printf("(%d)(%s)\n",nread,line) ;

        char *p = line ;
        while( *p != 0x00 ){
            assert( *p == line[0] ) ;
            p++ ;
        } //while
    } //for
}

g++ --std=c++11 -O2 -Werror recv.cpp -lrt -pthread /home/informix/tools/unpv13e/libunp.a -o recv.exe
g++ --std=c++11 -O2 -Werror send.cpp -lrt -pthread /home/informix/tools/unpv13e/libunp.a -o send.exe

arrow
arrow
    全站熱搜

    hedgezzz 發表在 痞客邦 留言(0) 人氣()