⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netif.cpp

📁 robocup源代码2001年清华机器人源代码
💻 CPP
字号:
/*
    Copyright (C) 2001  Tsinghuaeolus

    Authors : ChenJiang, YaoJinyi, CaiYunpeng, Lishi

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

	If you make any changes or have any comments we would appreciate a 
	message to yjy01@mails.tsinghua.edu.cn.
*/

#include "stdafx.h"#include <errno.h>#include "netif.h"
#include "utils.h"#include "global.h"int wait_message(char *buf, Socket *sock){
	for (int i = 0; i < 10; i++)  {
		if (receive_message(buf, sock) == 1){
			return 1;
		}
		//my_error("waiting for message!");
		Sleep(100);	}
	DoLog("wait fail");
	return 0;}Socket* init_connection(char *host, int port){
	struct hostent 	*host_ent ;
	struct in_addr	*addr_ptr ;
	struct sockaddr_in	cli_addr ;
	int			sockfd;
	Socket*		sock=new Socket;

	sock->socketfd = -1 ;

/*     winsock initialization       */
	WORD wVersionRequested;
	WSADATA wsaData;
	wVersionRequested = MAKEWORD( 2, 2 );

	if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
		return sock;
	}

	if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
		WSACleanup( );
		return sock; 
	}
	
	if(inet_addr(host) == -1){
		if((host_ent = (struct hostent *)gethostbyname(host)) == NULL) {
			return sock;
		}else{
			addr_ptr = (struct in_addr *) *host_ent->h_addr_list ;
			host = inet_ntoa(*addr_ptr) ;
		}
	}
	
	//  Open UDP socket.
	 
	if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
		return sock ;	// Can't open socket.
	
	 /*      Bind any local address.              */
	 
	memset((char *) &cli_addr,0,sizeof(cli_addr)) ;
	cli_addr.sin_family			= AF_INET ;
	cli_addr.sin_addr.s_addr	= htonl(INADDR_ANY) ;
	cli_addr.sin_port			= htons(0) ;


	if(bind(sockfd, (struct sockaddr *) &cli_addr,
		sizeof(cli_addr)) < 0){
		return sock ;	// Can't bind local address
	}

	
	//Fill in the structure with the address of the server.
	 
	sock->socketfd = sockfd ;

	memset((char *) &sock->serv_addr,0, sizeof(sock->serv_addr)) ;
	sock->serv_addr.sin_family		= AF_INET ;
	sock->serv_addr.sin_addr.s_addr	= inet_addr(host) ;
	sock->serv_addr.sin_port			= htons(port) ;
/*
	int timeout = 1;
	setsockopt(sockfd, IPPROTO_TCP, SO_RCVTIMEO, (char*)(&timeout), sizeof(int));*/

/*    set the socket to be nonblocking		*/
	unsigned long arp = 1;
	ioctlsocket(sockfd, FIONBIO, &arp);
	return sock ;}int send_message(char *buf, Socket *sock){
	int n ;
	if (!buf) return 0;
	n = strlen(buf) ;  
	if( sendto(sock->socketfd, buf, n, 0,
       (struct sockaddr *)&sock->serv_addr, sizeof(sock->serv_addr))!= n )
	   return (-1) ;
	else {
		return 0;	}}	   int receive_message(char *buf, Socket *sock){
	int			n,servlen ;
	struct sockaddr_in	serv_addr ;
	servlen = sizeof(serv_addr) ;
	n = recvfrom(sock->socketfd, buf, MAXMESG, 0,	       (struct sockaddr *)&serv_addr, &servlen);
	if(n < 0){
		if( n == -1&& errno == WSAEWOULDBLOCK) return 0 ;
		else {
			/*my_error("Receive error: %d on %d", errno, sock->socketfd);
			perror("");*/
			fflush(stderr);
			return (-1) ;		}
	}
	else{
		buf[n] = '\0' ;		sock->serv_addr.sin_port = serv_addr.sin_port ;//?
		if(n == 0) {
			my_error("Received null message");
			return 0 ;		}else   return 1 ;	}}void close_connection(Socket *sock){
	closesocket(sock->socketfd) ;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -