【Linux编程】socket编程
int socket(int domain, int type, int protocol); // 返回值:成功返回套接字描述符,失败返回-1
- 通信域为AF_INET,套接字类型为SOCK_STREAM,则默认协议为TCP。
- 通信域为AF_INET,套接字类型为SOCK_DGRAM,则默认协议为UDP。
int shutdown(int sockfd, int how); // 返回值:成功返回0,失败返回-1
- htonl:本地32位整型转网络字节序
- htons:本地16位整型转网络字节序
- ntohl:网络字节序转32位整型本地字节序
- ntohs:网络字节序转16位整型本地字节序
- socket:建立一个传输端点,返回一个套接字描述符。
- bind:将套接字绑定到某个IP和端口,此IP和端口就作为监听对象。注意,由于系统会分配给客户端默认地址,所以客户端使用该函数意义不大。
- listen:将套接字转换成监听套接字,使得客户端的连接请求可被内核接受,上述三步为设置监听描述符的正常步骤。
- accept:休眠,等待客户连接被内核接受,三次握手完毕后返回一个新的套接字描述符,该描述符连接到调用connect的客户端,被称为已连接描述符。
- send:发数据。
- recv:收数据。
- close:关闭连接,引发四次握手。
- socket:创建套接字。
- connect:建立连接,指明目的,和服务端的accept函数对应。
- send:发数据。
- recv:收数据。
- socket:建立套接字。
- bind:绑定监听的IP和端口。
- recvfrom:阻塞等待数据的到来。
/* 基于TCP协议的socket编程客户端代码 */ #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> /* 客户端: * socket-->connect-->send/recv */ #define SERVER_PORT 8888 int main(int argc, char **argv) { int iRet; int iSocketClient; struct sockaddr_in tSocketServerAddr; int iSendLen; unsigned char ucSendBuf[1000]; if (argc != 2) { printf("Usage:\n"); printf("%s <server_ip>\n", argv[0]); return -1; } /* 1. 建立socket */ iSocketClient = socket(AF_INET, SOCK_STREAM, 0); /* 2. 与服务器建立TCP连接 */ tSocketServerAddr.sin_family = AF_INET; /* recommend */ tSocketServerAddr.sin_port = htons(SERVER_PORT); /* host to net short */ if (inet_aton(argv[1], &tSocketServerAddr.sin_addr) == 0) /* 字符串转服务器IP */ { printf("Invalid server IP\n"); return -1; } bzero(tSocketServerAddr.sin_zero, 8); /* 如果iSocketClient没有绑定地址,则connect会给调用者绑定一个默认地址 */ iRet = connect(iSocketClient, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr)); if (iRet == -1) { /* 连接失败 */ printf("connect error\n"); return -1; } while (1) { if (fgets(ucSendBuf, 999, stdin)) { /* send函数发送数据 */ iSendLen = send(iSocketClient, ucSendBuf, strlen(ucSendBuf), 0); if (iSendLen == -1) { /* 函数出错 */ close(iSocketClient); return -1; } } } return 0; }
/* 基于TCP协议的socket编程服务器端代码 */ #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> /* 服务器: * socket-->bind-->listen-->accept-->send/recv */ #define SERVER_PORT 8888 #define BACKLOG 10 int main(int argc, char **argv) { int iRet; int iAddrLen; int iSocketSever; int iNewSocketSever; struct sockaddr_in tSocketServerAddr; struct sockaddr_in tSocketClientAddr; int iRecvLen; unsigned char ucRecvBuf[1000]; /* 子进程死后,会发送SIGCHLD信号给父进程, * 这里设置为忽略SIGCHLD信号 */ signal(SIGCHLD,SIG_IGN); /* 1. 建立socket * AF_INET: IPv4 Internet protocols * SOCK_STREAM: TCP协议 * 0: 通常赋值 */ iSocketSever = socket(AF_INET, SOCK_STREAM, 0); if (iSocketSever == -1) { printf("socket error\n"); return -1; } /* 2. 配置socket * 设置监听本机的哪个IP,端口 */ tSocketServerAddr.sin_family = AF_INET; /* recommend */ tSocketServerAddr.sin_port = htons(SERVER_PORT); /* host to net short */ tSocketServerAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* 绑定到本机所有网络接口 */ bzero(tSocketServerAddr.sin_zero, 8); iRet = bind(iSocketSever, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr)); if (iRet == -1) { printf("bind error\n"); return -1; } /* 3. 开启socket监听模式 * 建立服务请求队列,BACKLOG为最大连接数 */ iRet = listen(iSocketSever, BACKLOG); if (iRet == -1) { printf("listen error\n"); return -1; } while (1) { /* 4. 休眠,等待建立连接 * 客户端地址存放在第二个参数中 * 以后用新的socket描述符进行读写操作 */ iAddrLen = sizeof(struct sockaddr); iNewSocketSever = accept(iSocketSever, (struct sockaddr *)&tSocketClientAddr, &iAddrLen); if (iNewSocketSever != -1) { /* 成功建立连接 */ printf("Get connect from IP:%s, port:%d\n", inet_ntoa(tSocketClientAddr.sin_addr), ntohs(tSocketClientAddr.sin_port)); /* net to ascII */ /* fork系统调用创建子进程 */ if (fork() == 0) { /* 子进程源码 */ while (1) { /* recv函数接收客户端数据并显示,如果flags为0,则和read、write一样的操作 */ iRecvLen = recv(iNewSocketSever, ucRecvBuf, 999, 0); /* flags一般情况下置为0 */ if (iRecvLen <= 0) { /* recv出错(-1)或连接关闭(0) */ close(iNewSocketSever); printf("Disconnected!\n"); return -1; } else { ucRecvBuf[iRecvLen] = '\0'; printf("The recv msg is : %s\n", ucRecvBuf); } } } else { /* 父进程代码 */ close(iNewSocketSever); /* 父进程关闭已连接描述符尤为重要 */ } } } close(iSocketSever); return 0; }
/* 基于UDP协议的socket编程客户端代码 */ #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> /* 客户端: * socket-->connect-->send/recv */ #define SERVER_PORT 8888 int main(int argc, char **argv) { int iRet; int iSocketClient; struct sockaddr_in tSocketServerAddr; int iSendLen; unsigned char ucSendBuf[1000]; if (argc != 2) { printf("Usage:\n"); printf("%s <server_ip>\n", argv[0]); return -1; } /* 1. 建立socket */ iSocketClient = socket(AF_INET, SOCK_DGRAM, 0); /* 2. 不会建立真正的连接,只是让socket描述符中带有服务器地址, * 如果不使用connect函数,则应该使用sento函数发送数据 */ tSocketServerAddr.sin_family = AF_INET; /* recommend */ tSocketServerAddr.sin_port = htons(SERVER_PORT); /* host to net short */ if (inet_aton(argv[1], &tSocketServerAddr.sin_addr) == 0) /* 字符串转服务器IP */ { printf("Invalid server IP\n"); return -1; } bzero(tSocketServerAddr.sin_zero, 8); /* UDP中不需要建立连接,但这里使用connect是可以将地址绑定到iSocketClient上,send函数中就不需要地址信息了 */ iRet = connect(iSocketClient, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr)); if (iRet == -1) { printf("connect error\n"); return -1; } while (1) { if (fgets(ucSendBuf, 999, stdin)) { /* send函数发送数据 */ iSendLen = send(iSocketClient, ucSendBuf, strlen(ucSendBuf), 0); if (iSendLen == -1) { /* 函数出错 */ close(iSocketClient); return -1; } } } return 0; }
/* 基于UDP协议的socket编程服务器端代码 */ #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> /* 服务器: * socket-->bind-->sendto/recvfrom */ #define SERVER_PORT 8888 int main(int argc, char **argv) { int iRet; int iAddrLen; int iSocketSever; struct sockaddr_in tSocketServerAddr; struct sockaddr_in tSocketClientAddr; int iRecvLen; unsigned char ucRecvBuf[1000]; /* 1. 建立socket * AF_INET: IPv4 Internet protocols * SOCK_DGRAM: UDP协议 * 0: 通常赋值 */ iSocketSever = socket(AF_INET, SOCK_DGRAM, 0); if (iSocketSever == -1) { printf("socket error\n"); return -1; } /* 2. 配置socket * 设置监听本机的哪个IP,端口 */ tSocketServerAddr.sin_family = AF_INET; /* recommend */ tSocketServerAddr.sin_port = htons(SERVER_PORT); /* host to net short */ tSocketServerAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* 填入本机所有IP */ bzero(tSocketServerAddr.sin_zero, 8); iRet = bind(iSocketSever, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr)); if (iRet == -1) { printf("bind error\n"); return -1; } while (1) { /* 接收客户端数据,地址信息放在tSocketClientAddr结构体中 */ iAddrLen = sizeof(struct sockaddr); iRecvLen = recvfrom(iSocketSever, ucRecvBuf, 999, 0, (struct sockaddr *)&tSocketClientAddr, &iAddrLen); if (iRecvLen > 0) { ucRecvBuf[iRecvLen] = '\0'; printf("Get msg from %s : %s\n", inet_ntoa(tSocketClientAddr.sin_addr), ucRecvBuf); } } close(iSocketSever); return 0; }
/* 基于UDP协议的socket编程客户端代码 */ #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> /* 客户端: * socket-->sendto/recvfrom */ #define SERVER_PORT 8888 int main(int argc, char **argv) { int iRet; int iSocketClient; struct sockaddr_in tSocketServerAddr; int iSendLen; unsigned char ucSendBuf[1000]; if (argc != 2) { printf("Usage:\n"); printf("%s <server_ip>\n", argv[0]); return -1; } /* 1. 建立socket */ iSocketClient = socket(AF_INET, SOCK_DGRAM, 0); /* 2. 设置服务器地址 */ tSocketServerAddr.sin_family = htonl(AF_INET); /* recommend */ tSocketServerAddr.sin_port = htons(SERVER_PORT); /* host to net short */ if (inet_aton(argv[1], &tSocketServerAddr.sin_addr) == 0) /* 字符串转服务器IP */ { printf("Invalid server IP\n"); return -1; } bzero(tSocketServerAddr.sin_zero, 8); while (1) { if (fgets(ucSendBuf, 999, stdin)) { /* sendto函数发送数据 */ iSendLen = sendto(iSocketClient, ucSendBuf, strlen(ucSendBuf), 0, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr)); if (iSendLen == -1) { /* 函数出错 */ close(iSocketClient); return -1; } } } return 0; }
/* 基于UDP协议的socket编程服务器端代码 */ #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> /* 服务器: * socket-->bind-->sendto/recvfrom */ #define SERVER_PORT 8888 int main(int argc, char **argv) { int iRet; int iAddrLen; int iSocketSever; struct sockaddr_in tSocketServerAddr; struct sockaddr_in tSocketClientAddr; int iRecvLen; unsigned char ucRecvBuf[1000]; /* 1. 建立socket * AF_INET: IPv4 Internet protocols * SOCK_DGRAM: UDP协议 * 0: 通常赋值 */ iSocketSever = socket(AF_INET, SOCK_DGRAM, 0); if (iSocketSever == -1) { printf("socket error\n"); return -1; } /* 2. 配置socket * 设置监听本机的哪个IP,端口 */ tSocketServerAddr.sin_family = AF_INET; /* recommend */ tSocketServerAddr.sin_port = htons(SERVER_PORT); /* host to net short */ tSocketServerAddr.sin_addr.s_addr = INADDR_ANY; /* 填入本机所有IP */ bzero(tSocketServerAddr.sin_zero, 8); iRet = bind(iSocketSever, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr)); if (iRet == -1) { printf("bind error\n"); return -1; } while (1) { /* 接收客户端数据,地址信息放在tSocketClientAddr结构体中 */ iAddrLen = sizeof(struct sockaddr); iRecvLen = recvfrom(iSocketSever, ucRecvBuf, 999, 0, (struct sockaddr *)&tSocketClientAddr, &iAddrLen); if (iRecvLen > 0) { ucRecvBuf[iRecvLen] = '\0'; printf("Get msg from %s : %s\n", inet_ntoa(tSocketClientAddr.sin_addr), ucRecvBuf); } } close(iSocketSever); return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。