Socket编程实践(3)

socket函数

#include <sys/types.h>#include <sys/socket.h>int socket(int domain, int type, int protocol);

创建一个套接字用于通信

参数:

domain:指定通信协议族(protocolfamily),常用取值AF_INET(IPv4)

type:指定socket类型,流式套接字SOCK_STREAM,数据报套接字SOCK_DGRAM,原始套接字SOCK_RAW

protocol:协议类型,常用取值0,使用默认协议

返回值:

成功:返回非负整数,套接字;

失败:返回-1

bind函数

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

绑定一个本地地址到套接字

参数:

sockfd:socket函数返回的套接字

addr:要绑定的地址

//sockaddr_in结构, bind时需要强制转换成为struct sockaddr*类型struct sockaddr_in{sa_family_t sin_family; /* address family: AF_INET */in_port_tsin_port; /* port in network byte order */struct in_addr sin_addr; /* internet address */};/* Internet address. */struct in_addr{uint32_ts_addr;/* address in network byte order */};/**示例:INADDR_ANY的使用, 绑定本机任意地址**/int main(){int listenfd = socket(AF_INET, SOCK_STREAM, 0);if (listenfd == -1)err_exit("socket error");struct sockaddr_in addr;addr.sin_family = AF_INET;addr.sin_port = htons(8001);//绑定本机的任意一个IP地址, 作用同下面两行语句addr.sin_addr.s_addr = htonl(INADDR_ANY);//inet_aton("127.0.0.1", &addr.sin_addr);//addr.sin_addr.s_addr = inet_addr("127.0.0.1");if (bind(listenfd, (const struct sockaddr *)&addr, sizeof(addr)) == -1)err_exit("bind error");elsecout << "bind success" << endl;}

,,并且用在调用

thefirstconnectionrequestonthequeueofpendingconnectionsforthelistening

connect函数

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

建立一个连接至addr所指定的套接字

参数:

sockfd:未连接套接字

addr:要连接的套接字地址

addrlen:第二个参数addr长度

示例:echoserver/client实现

//server端代码int main(){int listenfd = socket(AF_INET, SOCK_STREAM, 0);if (listenfd == -1)err_exit("socket error");struct sockaddr_in addr;addr.sin_family = AF_INET;addr.sin_port = htons(8001);addr.sin_addr.s_addr = htonl(INADDR_ANY);if (bind(listenfd, (const struct sockaddr *)&addr, sizeof(addr)) == -1)err_exit("bind error");if (listen(listenfd, SOMAXCONN) == -1)err_exit("listen error");char buf[512];int readBytes;struct sockaddr_in clientAddr;//谨记: 此处一定要初始化socklen_t addrLen = sizeof(clientAddr);while (true){int clientfd = accept(listenfd, (struct sockaddr *)&clientAddr, &addrLen);if (clientfd == -1)err_exit("accept error");//打印客户IP地址与端口号cout << "Client information: " << inet_ntoa(clientAddr.sin_addr)<< ", " << ntohs(clientAddr.sin_port) << endl;memset(buf, 0, sizeof(buf));while ((readBytes = read(clientfd, buf, sizeof(buf))) > 0){cout << buf;if (write(clientfd, buf, readBytes) == -1)err_exit("write socket error");memset(buf, 0, sizeof(buf));}if (readBytes == 0){cerr << "client connect closed…" << endl;close(clientfd);}else if (readBytes == -1)err_exit("read socket error");}close(listenfd);}//client端代码int main(){int sockfd = socket(AF_INET, SOCK_STREAM, 0);if (sockfd == -1)err_exit("socket error");//填写服务器端口号与IP地址struct sockaddr_in serverAddr;serverAddr.sin_family = AF_INET;serverAddr.sin_port = htons(8001);serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");if (connect(sockfd, (const struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1)err_exit("connect error");char buf[512];while (fgets(buf, sizeof(buf), stdin) != NULL){if (write(sockfd, buf, strlen(buf)) == -1)err_exit("write socket error");memset(buf, 0, sizeof(buf));int readBytes = read(sockfd, buf, sizeof(buf));if (readBytes == 0){cerr << "server connect closed… \nexiting…" << endl;break;}else if (readBytes == -1)err_exit("read socket error");cout << buf;memset(buf, 0, sizeof(buf));}close(sockfd);}

附-Makefile

.PHONY: clean all CC = g++ CPPFLAGS = -Wall -g -pthread -std=c++11BIN = server clientSOURCES = $(BIN.=.cpp)all: $(BIN)$(BIN): $(SOURCES) clean:-rm -rf $(BIN) bin/ obj/ core

只有坚韧不拔向着目标奋进,成功后将在不远处等待着你的到来。

Socket编程实践(3)

相关文章:

你感兴趣的文章:

标签云: