Internet - IP , Internet. , -, -, , , , TCP.
s = socket(domain, type, protocol);
. Internet, :
stream TCP, , socket :
s = socket(AF_INET, SOCK_STREAM, 0);
Internet , , , , connect send , .
bind:
bind(s, name, namelen);
(name) , . .
error = connect(s, serveraddr, serveraddrlen);
, s serveraddr, sockaddr_in, . , connect bind.
Connect 0, . -1 , . , read write, close.
. , , , . listen:
error = listen(s, qlength);
s , qlength , , ; .
, , ' '. Internet . accept:
newsock = accept(s, clientaddr, clientaddrlen);
, , , accept, .
1.
write(s, buf, sizeof(buf)); read(s, buf, sizeof(buf));
send recv read write, , .
send(s, buf, sizeof(buf), flags); recv(s, buf, sizeof(buf), flags);
, , :
, close, :
close(s);
, ( stream), . , , , . , shutdown . shutdown "" . :
shutdown(s, how);
how :
/* ----------------------------------------------------------- MakeConnection Function allocates a socket and estabishes a connection with remote host. Default port number 80. Input : WWW server name (with port number, if it is not 80) Output : file descriptor on success -1 on error ----------------------------------------------------------- */ int MakeConnection(unsigned char* ServerName){ int s; struct sockaddr_in ssin; struct hostent* hp; int PortNum; unsigned char strHlp[STRNGLEN], *pch; /* use default port number - 80 or specific number from the server name */ strcpy(strHlp,ServerName); pch = strchr(strHlp,':'); if(pch==NULL){ PortNum = 80; }else{ pch[0] = '\0'; pch++; PortNum = atoi(pch); if(PortNum==0){ PortNum = 80; } } /* get host by name - resolve host name into IP address */ if( (hp=gethostbyname(strHlp)) == NULL ) { return -1; } bzero(&ssin, sizeof(ssin)); bcopy(hp->h_addr, &ssin.sin_addr, hp->h_length); ssin.sin_family = hp->h_addrtype; ssin.sin_port = htons(PortNum); /* allocate a socket */ if((s=socket(AF_INET, SOCK_STREAM, 0))==-1) { return -1; } /* make a connection */ if(connect(s, &ssin, sizeof(ssin), 0)==-1){ return -1; } return s; /* socket descriptor */ }
Last-modified: Thu, 28 May 1998 14:26:10 GMT