Exemplo n.º 1
0
int main(int argc, char *argv[]) {
  int listenfd, connectfd;
  struct sockaddr_in host_addr, pin_addr;
  int pin_addr_size;
  pthread_t multiCS;
  ADDR_ARG *arg = NULL;

  createSocket(&listenfd, 0);
  initHostAddr(&host_addr, myport);

  printf(" init ipaddress: %s\n", inet_ntoa(host_addr.sin_addr));
  bindPort(&listenfd, &host_addr);
  listenPort(&listenfd);

  pin_addr_size = sizeof(pin_addr);
  while (1) {
    getAccept(&listenfd, &connectfd, &pin_addr, &pin_addr_size);

    arg = (ADDR_ARG *)malloc(sizeof(ADDR_ARG));
    arg->connectfd = connectfd;
    memcpy((void *)&arg->cli_addr, &pin_addr, sizeof(pin_addr));

    createPthread(&multiCS, start_talk, arg);
  }

  close(listenfd);
  return 0;
}
Exemplo n.º 2
0
InternalNode*
RSHNetwork::CreateInternalNode( Network* inetwork, 
                                std::string imy_hostname,
                                Rank imy_rank,
                                std::string iphostname,
                                Port ipport, 
                                Rank iprank,
                                int idataSocket,
                                Port idataPort )
{
    XPlat_Socket listeningSocket = idataSocket;
    Port listeningPort = idataPort;

    // we build and bind our listening socket now
    // because other platforms need to have this information
    // in their initialization code, and it doesn't hurt us
    // to bind it early
    int bRet = bindPort( &listeningSocket, &listeningPort, true /*non-blocking*/ );
    if( bRet == -1 ) {
        mrn_dbg( 1, mrn_printf(FLF, stderr, 
                               "Failure: unable to instantiate network\n") );
        return NULL;
    }
    assert( listeningPort != UnknownPort );

    return new RSHInternalNode( inetwork,
                                imy_hostname,
                                imy_rank,
                                iphostname,
                                ipport,
                                iprank,
                                listeningSocket,
                                listeningPort );
}
Exemplo n.º 3
0
bool TSocket::listen( unsigned port , int maxWaitClient )
{
	if ( !createTCP( true ) )
		return false;

	if ( !bindPort( port ) )
		return false;

	int result = ::listen( getSocketObject(), maxWaitClient );
	if( result == SOCKET_ERROR)
	{
		return false;
	}

	mState = SKS_LISTING;
	return true;
}
Exemplo n.º 4
0
Beaglebone::Beaglebone():Subsystem ("Beaglebone"){
    ultrasonic = new AnalogChannel(BALL_ULTRASONIC_MODULE, BALL_ULTRASONIC);
    ultrasonic->SetAverageBits(32);
    ultrasonic->SetOversampleBits(32);
    
    ballX = 99.0;
    ballY = 99.0;
    ballDiam = 99.0;
    goalX = 99.0;
    goalY = 99.0;
    goalAngle = 99.0;
    isConnected = false;
    isBound = false;
    isTrackingBall = true;
    
    // Initialise the connection parameters
    // This section opens a UDP connection to the beagle bone
    memset(&address_bbb, 0, sizeof(address_bbb));
    address_bbb.sin_family = AF_INET;
    address_bbb.sin_port = htons(BEAGLEBONE_PORT);
    inet_aton(const_cast<char *>(BEAGLEBONE_IP), &address_bbb.sin_addr);
    sd_bbb = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // To send to BBB

    // This section creates a listening UDP connection on the cRIO
    memset(&address_crio, 0, sizeof(address_crio));
    address_crio.sin_family = AF_INET;
    address_crio.sin_port = htons(BEAGLEBONE_PORT);
    address_crio.sin_addr.s_addr = htonl(INADDR_ANY);
    sd_crio = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // To send receive on cRIO
    struct timeval tv;
	tv.tv_sec = 0;
	tv.tv_usec = 10000; // Timeout for connection
	setsockopt(sd_crio, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
    
    // Try to bind the port
    bindPort();
}
Exemplo n.º 5
0
int main(int argc, char *argv[])
{
    int sockfd, clientfd, sin_size, recvbytes;  //定义监听套接字、客户套接字
    pid_t pid, ppid;            //定义父子线程标记变量
    char *buf, *r_addr, *w_addr, *temp, *time_str;      //="\0"; //定义临时存储区
    struct sockaddr_in their_addr;      //定义地址结构
    key_t shmid;
    shmid = shm_create();       //创建共享存储区
    temp = (char *) malloc(255);
    time_str = (char *) malloc(20);
    sockfd = bindPort(MYPORT);  //绑定端口
    while (1)
    {
        if (listen(sockfd, BACKLOG) == -1)      //在指定端口上监听
        {
            perror("listen");
            exit(1);
        }
        printf("listening......\n");
        if ((clientfd = accept(sockfd, (struct sockaddr *) &their_addr, &sin_size)) == -1)      //接收客户端连接
        {
            perror("accept");
            exit(1);
        }
        printf("accept from:%d\n", inet_ntoa(their_addr.sin_addr));
        send(clientfd, WELCOME, strlen(WELCOME), 0);    //发送问候信息
        buf = (char *) malloc(255);
        ppid = fork();          //创建子进程
        if (ppid == 0)
        {
            //printf("ppid=0\n");
            pid = fork();       //创建子进程  
            while (1)
            {
                if (pid > 0)
                {
                    //父进程用于接收信息
                    memset(buf, 0, 255);
                    //printf("recv\n");
                    //sleep(1);
                    if ((recvbytes = recv(clientfd, buf, 255, 0)) <= 0)
                    {
                        perror("recv1");
                        close(clientfd);
                        raise(SIGKILL);
                        exit(1);
                    }
                    //write buf's data to share memory
                    w_addr = shmat(shmid, 0, 0);
                    memset(w_addr, '\0', 1024);
                    strncpy(w_addr, buf, 1024);
                    get_cur_time(time_str);
                    strcat(buf, time_str);
                    printf(" %s\n", buf);
                }
                else if (pid == 0)
                {
                    //子进程用于发送信息
                    //scanf("%s",buf);
                    sleep(1);
                    r_addr = shmat(shmid, 0, 0);
                    //printf("---%s\n",r_addr);
                    //printf("cmp:%d\n",strcmp(temp,r_addr));
                    if (strcmp(temp, r_addr) != 0)
                    {
                        strcpy(temp, r_addr);
                        get_cur_time(time_str);
                        strcat(r_addr, time_str);
                        //printf("discriptor:%d\n",clientfd);
                        //if(send(clientfd,buf,strlen(buf),0) == -1)
                        if (send(clientfd, r_addr, strlen(r_addr), 0) ==
                            -1)
                        {
                            perror("send");
                        }
                        memset(r_addr, '\0', 1024);
                        strcpy(r_addr, temp);
                    }
                }
                else
                    perror("fork");
            }
        }
    }
    printf("------------------------------\n");
    free(buf);
    close(sockfd);
    close(clientfd);
    return 0;
}
Exemplo n.º 6
0
void Beaglebone::readData() {
    if (!isBound) {
        // Try to connect again
        bindPort();
        return;
    }
    isTrackingBall = ultrasonic->GetAverageVoltage()<BALL_ULTRASONIC_THRESHOLD;
    // Send a UDP blast to the BBB to make sure it is in the correct mode
    char targetCode = 'g';
    if (isTrackingBall) {
        DriverStation::Alliance colour;
	    colour = DriverStation::GetInstance()->GetAlliance();
	    if (colour == DriverStation::Alliance::kBlue){
		    targetCode = 'b';
		}
		else{
			targetCode = 'r';
		}
    }
    sendto(sd_bbb, &targetCode, 1, 0, (struct sockaddr*)&address_bbb, sizeof(address_bbb));
    
    // Create a buffer to receive info into
    char line[256];
    char* lastChar = NULL;
    
    
    // Receive a UDP packet
    int numRead = recvfrom(sd_crio, line, sizeof(line), 0, NULL, NULL);
    
    // Process the packet
    if (numRead > 0) {
        char target = line[0]; // First character tells us if it's goal or ball
        lastChar = &line[1]; // Process numbers from the second character
		double a = strtod(lastChar, &lastChar);
		double b = strtod(lastChar, &lastChar);
		double c = strtod(lastChar, NULL);
        // Store these in the appropriate member variables
        if (target == 'g') {
            goalX = a;
            goalY = b;
            goalAngle = c;
            // Reset the other variables
            ballX = 99.0;
            ballY = 99.0;
            ballDiam = 99.0;
        } else {
            ballX = a;
            ballY = b;
            ballDiam = c;
            goalX = 99.0;
            goalY = 99.0;
            goalAngle = 99.0;
            isConnected = true;
            missedPackets=0;
        }
    } else {
		// We must have timed out
		missedPackets++;
	}
	if (missedPackets > 100) {
		isConnected = false;
	}
}
Exemplo n.º 7
0
int main(int argc, char *argv[])
{
    int clientfd;
    char *buf;

    //绑定端口
    int sockfd = bindPort(MYPORT);
    
    //创建共享存储区
    key_t shmid, onlineShmid;
    char *r_addr, *w_addr;
    shmid = shm_create();
    w_addr = shmat(shmid, 0, 0);
    r_addr = shmat(shmid, 0, 0);
    
    onlineShmid = initOnlineShmat();
    
    // //定义临时存储区
    char *temp, *time_str;
    temp = (char *)malloc(255);
    time_str=(char *)malloc(20);
    
    
    while(1)
    {
        //在指定端口上监听
        if(listen(sockfd,BACKLOG) == -1)
        {
            perror("listen error");
            exit(1);
        }
        printf("listening......\n");
        
        
        //接收客户端连接
        int sin_size = 0;
        struct sockaddr_in their_addr;
        if((clientfd = accept(sockfd,(struct sockaddr*)&their_addr,&sin_size)) == -1)
        {
            perror("accept error");
            exit(1);
        }
        printf("discriptor:%d\n",clientfd);
        printf("accept from:%d\n",inet_ntoa(their_addr.sin_addr));
        
        //发送问候信息
        send(clientfd, WELCOME, strlen(WELCOME), 0);
        
        //build buff
        buf = (char *)malloc(255);
        
        //创建子进程
        pid_t ppid = fork();
        if(ppid == 0)
        {
            printf("fork ppid=0\n");
            
            //再次创建子进程
            pid_t pid = fork();
            bool bBeRequested = false;
            while(1)
            {
                //父进程用于接收信息
                if(pid > 0)
                {
                    memset(buf,0,255);
                    printf("begin recv\n");
                    //sleep(1);
                    
                    int recvbytes = 0;
                    if((recvbytes = recv(clientfd,buf,255,0)) <= 0)
                    {
                        perror("recv1 error");
                        close(clientfd);
                        raise(SIGKILL);
                        exit(1);
                    }
                    
                    
                    //write buf's data to share memory
                    memset(w_addr, '\0', 1024);
                    strncpy(w_addr, buf, 1024);
                    bBeRequested = true;
                    printf("w_addr->%s\n",w_addr);
                    
                    
                    //strcat time info
                    get_cur_time(time_str);
                    strcat(buf,time_str);
                    printf("buf:%s\n",buf);
                }
                
                //子进程用于发送信息
                else if(pid == 0)
                {
                    sleep(1);
                    printf("r_addr:|%s| temp:|%s|\n",r_addr, temp);
                    
                    //swap shmat buffer
                    if(strcmp(temp,r_addr) != 0)
                    {
                        printf("swap buffer!\n");
                        get_cur_time(time_str);
                        //strcat(r_addr,time_str);
                        strcpy(temp,r_addr);
                        memset(r_addr, '\0', 255);
                        printf("temp:%s\n",temp);

                        char *delims = { "@" };
                        char *cmd = strtok( temp, delims);
                        if( NULL == cmd )
                        {
                            printf("temp:%s parse error,no cmd!\n", temp);
                            memset(temp, '\0', 1024);
                            continue;
                        }
                        
                        
                        char* rbody = strtok( NULL, delims);
                        if( NULL == rbody )
                        {
                            printf("temp:%s parse error,no rbody!\n", temp);
                            memset(temp, '\0', 1024);
                            continue;
                        }
                        printf("cmd:%s rbody:%s\n", cmd, rbody);
                        
                        
                        if( 0 == strcmp(cmd, "reg") )
                        {
                            regOnlineShmat(onlineShmid, rbody);
                        }
                        

                        //send temp buffer
                        //if(send(clientfd,temp,strlen(temp),0) == -1)
                        //{
                        //    perror("send error");
                        //}
                        memset(temp, '\0', 1024);
                    }
                }
                else
                {
                    perror("fork error");
                }
            }
        }
    }
    printf("------------------------------\n");
    free(buf);
    close(sockfd);
    close(clientfd);
    return 0;
}