int socket(int domain, int type, int protocol); // 返回值:成功返回套接字描述符,失败返回-1
int shutdown(int sockfd, int how); // 返回值:成功返回0,失败返回-1
/* 基于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; }
【Linux编程】socket编程,布布扣,bubuko.com
原文:http://blog.csdn.net/nestler/article/details/37314303