1:read與recv函數
recv首先查看一下幫助:
recv的功能和read的一樣,都可以從套接口當中獲取數據,但是recv只能用於套接口I/O不能用於文件I/O,以及其他的I/O,read函數可以用於任何I/O。
recv相比於read多了一個flags選項,通過這個選項可以指定接受的行爲。主要有兩個重要的選項:
MSG_OOB:可以接收帶外數據,通過緊急指針發送的數據。帶外數據被認爲具有比普通數據更高的優先級
MSG_PEEK:可以接收緩存區的數據但是並不將緩存區中的數據清除,read函數一旦讀取數據就會將數據從緩存區中清除。
ssize_t recv_peek(int sockfd, void *buf, size_t len)
{
while(1)
{
int ret = recv(sockfd, buf, len, MSG_PEEK);
//只接受數據不將數據從緩存區中清除
if(ret == -1 && errno == EINTR) //信號中斷
{
continue;
}
return ret;
}
}
2:readline實現(按行讀取)
ssize_t readline(int sockfd, void* buf, size_t maxline)
{
int ret;
int nread;
char *bufp = buf;
int nleft = maxline;
while(1)
{
ret = recv_peek(sockfd, buf, nleft);
if(ret < 0)
return ret;
else if(ret == 0) //對方關閉了套接口
return ret;
nread = ret;
int i = 0;
for(i = 0; i < nread; i++)
{
if(buf[i] == '\n')
{
ret = readn(sockfd, bufp, i+1);
if(ret != i+1)
exit(EXIT_FAILURE);
return ret;
}
}
if(nread > nleft)
exit(EXIT_FAILURE);
nleft -= nread;
ret = readn(sockfd, bufp, nread);
//將緩存區中的數據刪除
if(ret != nread)
exit(EXIT_FAILURE);
bufp += nread;
nleft -= nread;
}
return -1;
}
服務器端程序:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
struct packet
{
int len;
char buf[1024];
};
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0);
ssize_t readn(int fd, void *buf, size_t count)
{
size_t nleft = count; // 剩餘字節數
ssize_t nread;
char *bufp = (char*) buf;
while (nleft > 0)
{
nread = read(fd, bufp, nleft);
if (nread < 0)
{
if (errno == EINTR)
{
continue;
}
return -1;
} else if (nread == 0)
{
return count - nleft;
}
bufp += nread;
nleft -= nread;
}
return count;
}
ssize_t writen(int fd, const void *buf, size_t count)
{
size_t nleft = count;
ssize_t nwritten;
char* bufp = (char*)buf;
while (nleft > 0)
{
if ((nwritten = write(fd, bufp, nleft)) < 0)
{
if (errno == EINTR)
{
continue;
}
return -1;
}
else if (nwritten == 0)
{
continue;
}
bufp += nwritten;
nleft -= nwritten;
}
return count;
}
ssize_t recv_peek(int sockfd, void *buf, size_t len)
{
while (1)
{
int ret = recv(sockfd, buf, len, MSG_PEEK); // 查看傳入消息
if (ret == -1 && errno == EINTR)
{
continue;
}
return ret;
}
}
ssize_t readline(int sockfd, void *buf, size_t maxline)
{
int ret;
int nread;
char *bufp = (char*)buf; // 當前指針位置
int nleft = maxline;
while (1)
{
ret = recv_peek(sockfd, buf, nleft);
if (ret < 0)
{
return ret;
}
else if (ret == 0)
{
return ret;
}
nread = ret;
int i;
for (i = 0; i < nread; i++)
{
if (bufp[i] == '\n')
{
ret = readn(sockfd, bufp, i+1);
if (ret != i+1)
{
exit(EXIT_FAILURE);
}
return ret;
}
}
if (nread > nleft)
{
exit(EXIT_FAILURE);
}
nleft -= nread;
ret = readn(sockfd, bufp, nread);
if (ret != nread)
{
exit(EXIT_FAILURE);
}
bufp += nread;
}
return -1;
}
void echo_srv(int connfd)
{
char recvbuf[1024];
// struct packet recvbuf;
int n;
while (1)
{
memset(recvbuf, 0, sizeof recvbuf);
int ret = readline(connfd, recvbuf, 1024);
if (ret == -1)
{
ERR_EXIT("readline");
}
if (ret == 0)
{
printf("client close\n");
break;
}
fputs(recvbuf, stdout);
writen(connfd, recvbuf, strlen(recvbuf));
}
}
int main(int argc, char** argv) {
// 1. 創建套接字
int listenfd;
if ((listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
ERR_EXIT("socket");
}
// 2. 分配套接字地址
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof servaddr);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6666);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
// servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
// inet_aton("127.0.0.1", &servaddr.sin_addr);
int on = 1;
// 確保time_wait狀態下同一端口仍可使用
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on) < 0) {
ERR_EXIT("setsockopt");
}
// 3. 綁定套接字地址
if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof servaddr) < 0) {
ERR_EXIT("bind");
}
// 4. 等待連接請求狀態
if (listen(listenfd, SOMAXCONN) < 0) {
ERR_EXIT("listen");
}
// 5. 允許連接
struct sockaddr_in peeraddr;
socklen_t peerlen = sizeof peeraddr;
// 6. 數據交換
pid_t pid;
while (1) {
int connfd;
if ((connfd = accept(listenfd, (struct sockaddr *) &peeraddr, &peerlen)) < 0) {
ERR_EXIT("accept");
}
printf("id = %s, ", inet_ntoa(peeraddr.sin_addr));
printf("port = %d\n", ntohs(peeraddr.sin_port));
pid = fork();
if (pid == -1) {
ERR_EXIT("fork");
}
if (pid == 0) // 子進程
{
close(listenfd);
echo_srv(connfd);
//printf("child exit\n");
exit(EXIT_SUCCESS);
} else {
//printf("parent exit\n");
close(connfd);
}
}
// 7. 斷開連接
close(listenfd);
return 0;
}
客戶端程序:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/wait.h>
using namespace std;
struct packet
{
int len;
char buf[1024];
};
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0);
ssize_t readn(int fd, void *buf, size_t count)
{
size_t nleft = count; // 剩餘字節數
ssize_t nread;
char *bufp = (char*) buf;
while (nleft > 0)
{
nread = read(fd, bufp, nleft);
if (nread < 0)
{
if (errno == EINTR)
{
continue;
}
return -1;
} else if (nread == 0)
{
return count - nleft;
}
bufp += nread;
nleft -= nread;
}
return count;
}
ssize_t writen(int fd, const void *buf, size_t count)
{
size_t nleft = count;
ssize_t nwritten;
char* bufp = (char*)buf;
while (nleft > 0)
{
if ((nwritten = write(fd, bufp, nleft)) < 0)
{
if (errno == EINTR)
{
continue;
}
return -1;
}
else if (nwritten == 0)
{
continue;
}
bufp += nwritten;
nleft -= nwritten;
}
return count;
}
ssize_t recv_peek(int sockfd, void *buf, size_t len)
{
while (1)
{
int ret = recv(sockfd, buf, len, MSG_PEEK); // 查看傳入消息
if (ret == -1 && errno == EINTR)
{
continue;
}
return ret;
}
}
ssize_t readline(int sockfd, void *buf, size_t maxline)
{
int ret;
int nread;
char *bufp = (char*)buf; // 當前指針位置
int nleft = maxline;
while (1)
{
ret = recv_peek(sockfd, buf, nleft);
if (ret < 0)
{
return ret;
}
else if (ret == 0)
{
return ret;
}
nread = ret;
int i;
for (i = 0; i < nread; i++)
{
if (bufp[i] == '\n')
{
ret = readn(sockfd, bufp, i+1);
if (ret != i+1)
{
exit(EXIT_FAILURE);
}
return ret;
}
}
if (nread > nleft)
{
exit(EXIT_FAILURE);
}
nleft -= nread;
ret = readn(sockfd, bufp, nread);
if (ret != nread)
{
exit(EXIT_FAILURE);
}
bufp += nread;
}
return -1;
}
void ehco_cli(int sockfd)
{
char recvbuf[1024];
char sendbuf[1024];
// struct packet recvbuf;
// struct packet sendbuf;
memset(recvbuf, 0, sizeof recvbuf);
memset(sendbuf, 0, sizeof sendbuf);
int n = 0;
while (fgets(sendbuf, sizeof sendbuf, stdin) != NULL) // 鍵盤輸入獲取
{
writen(sockfd, sendbuf, strlen(sendbuf)); // 寫入服務器
int ret = readline(sockfd, recvbuf, sizeof recvbuf); // 服務器讀取
if (ret == -1)
{
ERR_EXIT("readline");
}
if (ret == 0)
{
printf("server close\n");
break;
}
fputs(recvbuf, stdout); // 服務器返回數據輸出
// 清空
memset(recvbuf, 0, sizeof recvbuf);
memset(sendbuf, 0, sizeof sendbuf);
}
}
void handle_sigchld(int sig)
{
// wait(NULL);
while (waitpid(-1, NULL, WNOHANG) > 0);
}
int main(int argc, char** argv) {
// signal(SIGCHLD, SIG_IGN);
signal(SIGCHLD, handle_sigchld);
// 1. 創建套接字
int sockfd[5];
int i;
for (i = 0; i < 5; ++i)
{
if ((sockfd[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
ERR_EXIT("socket");
}
// 2. 分配套接字地址
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof servaddr);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6666);
// servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
// inet_aton("127.0.0.1", &servaddr.sin_addr);
// 3. 請求鏈接
if (connect(sockfd[i], (struct sockaddr *) &servaddr, sizeof servaddr) < 0) {
ERR_EXIT("connect");
}
struct sockaddr_in localaddr;
socklen_t addrlen = sizeof localaddr;
if (getsockname(sockfd[i], (struct sockaddr*)&localaddr, &addrlen) < 0)
{
ERR_EXIT("getsockname");
}
printf("id = %s, ", inet_ntoa(localaddr.sin_addr));
printf("port = %d\n", ntohs(localaddr.sin_port));
}
// 4. 數據交換
ehco_cli(sockfd[0]);
// 5. 斷開連接
close(sockfd[0]);
return 0;
}
3:getsockname, getpeername,gethostname,gethostbyname,gethostbyaddr
函數原型:
getsocket函數獲取本地的地址
使用方式:
struct sockaddr_in localaddr;
socklen_t addrlen = sizeof localaddr;
if (getsockname(sockfd[i], (struct sockaddr*)&localaddr, &addrlen) < 0)
{
ERR_EXIT("getsockname");
}
printf("id = %s, ", inet_ntoa(localaddr.sin_addr));
printf("port = %d\n", ntohs(localaddr.sin_port));
getpeername獲取對方的地址,與getsockname使用方法相同。但是套接字必須是連接成功之後的纔行。
gethostname獲取主機名稱,
gethostbyname通過主機名獲取主機上所有的IP地址
gethostname函數原型:
返回值
gethostbyname函數原型:
程序例子:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while (0);
int getlocalip(char *ip)
{
char host[100] = {0};
if (gethostname(host, sizeof host) < 0)
{
ERR_EXIT("gethostname");
}
struct hostent *hp;
if ((hp = gethostbyname(host)) == NULL)
{
ERR_EXIT("gethostbyname");
}
strcpy(ip, inet_ntoa(*(struct in_addr*)hp->h_addr_list[0]));
return 0;
}
int main(void)
{
// char host[100] = {0};
// if (gethostname(host, sizeof host) < 0)
// {
// ERR_EXIT("gethostname");
// }
//
// struct hostent *hp;
// if ((hp = gethostbyname(host)) == NULL)
// {
// ERR_EXIT("gethostbyname");
// }
// int i = 0;
// while (hp->h_addr_list[i] != NULL)
// {
// printf("%s\n", inet_ntoa(*(struct in_addr*)hp->h_addr_list[i]));
// ++i;
// }
char ip[16] = {0};
getlocalip(ip); //獲取本機IP地址。
printf("localip = %s\n", ip);
return 0;
}