📄 network.c
字号:
#ifndef lintstatic const char rcsid[] = "$Id: network.c,v 1.1.1.1 2001/03/08 00:01:47 efalk Exp $" ;#endif/* * Copyright (c) 1995 by Edward A. Falk *//********** * * * @ @ @@@@@ @@@@@ @ @ @@@ @@@@ @ @ * @@ @ @ @ @ @ @ @ @ @ @ @ * @ @ @ @@@ @ @ @ @ @ @ @@@@ @@@ * @ @@ @ @ @ @ @ @ @ @ @ @ @ * @ @ @@@@@ @ @ @ @@@ @ @ @ @ * * NETWORK - Make network connection * * Routines provided here: * * * void * IpConnect() * Make IP connection according to parameters in gcomm.h * * void * NetDisConnect() * Close IP connection. * * * * Edward A. Falk * * January, 1995 * * * **********/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <malloc.h>#include <string.h>#include <ctype.h>#include <fcntl.h>#include <errno.h>#include <sys/param.h>#include <sys/types.h>#include <sys/time.h>#include <sys/stat.h>#include <sys/stat.h>#include <netdb.h> /* stuff used by tcp/ip */#include <sys/socket.h>#include <netinet/in.h>#include <sys/wait.h>#include <sys/signal.h>#include "gcomm.h"#include "windows.h"#include "winutils.h"extern int errno ;voidNetConnect(){ int sockfd ; int i,j ; struct protoent *protoent ; struct hostent *hostent ; struct sockaddr_in sockaddr ; int port ; int proto ; char *hostId = getHostId() ; WindowStatus("Connecting...") ; if( (protoent = getprotobyname("ip")) == NULL ) proto = 0 ; else proto = protoent->p_proto ; port = PortLookup(getPortNo()) ; if( port == -1 ) return ; bzero(&sockaddr, sizeof(sockaddr)) ; sockaddr.sin_family = AF_INET ; sockaddr.sin_port = htons(port) ; hostent = gethostbyname(hostId) ; if( hostent != NULL ) { sockaddr.sin_addr = *(struct in_addr *) hostent->h_addr ; } else if( isdigit(hostId[0]) ) { int b1,b2,b3,b4 ; i = sscanf(hostId, "%d.%d.%d.%d", &b1,&b2,&b3,&b4) ; if( i != 4 ) { WindowStatus("Host address must be in dd.dd.dd.dd format") ; return ; } sockaddr.sin_addr.s_addr = (b1<<24)|(b2<<16)|(b3<<8)|b4 ; } else { WindowStatus("Hostname not found") ; return ; } sockfd = socket(AF_INET, SOCK_STREAM, proto) ; if( sockfd < 0 ) { WindowStatus("Cannot open socket: %s", strerror(errno)) ; return ; } i = connect(sockfd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) ; if( i < 0 ) { WindowStatus("Cannot connect to host: %s", strerror(errno)) ; close(sockfd) ; return ; } ifd = ofd = sockfd ; i = fcntl(ifd, F_GETFL, 0) ; j = fcntl(ifd, F_SETFL, i|O_NDELAY) ; connectionActive = True ; connectTime0 = time(NULL) ; WindowStatus("Connected") ;}voidNetDisConnect(){ if( ifd != -1 ) { close(ifd) ; ifd = ofd = -1 ; } ConnectionDone() ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -