第一版
| #include <stdio.h> |
| #include <sys/types.h> |
| #include <sys/socket.h> |
| #include <netinet/in.h> |
| #include <arpa/inet.h> |
| #include <unistd.h> |
| #include <stdlib.h> |
| |
| #define PORT 8085 |
| |
| int main(int argc, char *argv[]) |
| { |
| int sock_fd; |
| int len; |
| struct sockaddr_in addr; |
| int newsock_fd; |
| char * buf = "come on"; |
| int len2; |
| char rebuf[40]; |
| |
| sock_fd = socket(AF_INET, SOCK_STREAM, 0); |
| addr.sin_family = AF_INET; |
| |
| |
| |
| addr.sin_addr.s_addr = inet_addr("10.3.119.141"); |
| addr.sin_port = htons(PORT); |
| len = sizeof(addr); |
| |
| |
| newsock_fd = connect(sock_fd, (struct sockaddr *)&addr, len); |
| if (newsock_fd == -1) |
| { |
| perror("connect error.\n"); |
| exit(1); |
| } |
| else |
| { |
| printf("connect OK.\n"); |
| } |
| |
| len2 = sizeof(buf); |
| |
| send(sock_fd, buf, len2, 0); |
| printf("111"); |
| sleep(5); |
| printf("222"); |
| |
| recv(sock_fd, rebuf, 256, 0); |
| rebuf[sizeof(rebuf) + 1] = '\0'; |
| printf("receive message:\n%s\n.", rebuf); |
| |
| close(sock_fd); |
| return 0; |
| } |
| |
第二版
| #include <stdio.h> |
| #include <sys/socket.h> |
| #include <sys/types.h> |
| #include <netinet/in.h> |
| #include <stdlib.h> |
| #include <arpa/inet.h> |
| #include <string.h> |
| #include <unistd.h> |
| |
| int main(int argc, char * argv[]) |
| { |
| struct sockaddr_in client_socket; |
| int client_fd, client_connect; |
| |
| |
| client_fd = socket(AF_INET, SOCK_STREAM, 0); |
| if (client_fd == -1) |
| { |
| perror("socket error.\n"); |
| exit(1); |
| } |
| else |
| { |
| printf("socket successfully.\n"); |
| } |
| |
| client_socket.sin_family = AF_INET; |
| client_socket.sin_addr.s_addr = inet_addr("10.3.119.141"); |
| client_socket.sin_port = htons(8080); |
| bzero(&(client_socket.sin_zero), 8); |
| |
| |
| client_connect = connect(client_fd, (struct sockaddr *)&client_socket, sizeof(client_socket)); |
| if (client_connect == -1) |
| { |
| perror("socket error.\n"); |
| exit(1); |
| } |
| else |
| { |
| printf("connect successfully.\n"); |
| } |
| |
| |
| char buf[20]; |
| memset(buf, '0', sizeof(buf)); |
| recv(client_fd, buf, sizeof(buf), 0); |
| printf("MSG:%s", buf); |
| |
| |
| for (int i = 0; i < 10; i++) |
| { |
| char str[] = "hello.\n"; |
| send(client_fd, str, sizeof(str), 0); |
| sleep(1); |
| } |
| |
| |
| close(client_fd); |
| return 0; |
| } |
第三版
| |
| |
| |
| |
| #include <stdio.h> |
| #include <sys/socket.h> |
| #include <sys/types.h> |
| #include <netinet/in.h> |
| #include <stdlib.h> |
| #include <arpa/inet.h> |
| #include <string.h> |
| #include <unistd.h> |
| |
| struct sockaddr_in initParam(void); |
| int initClient(struct sockaddr_in client_socket); |
| void dealData(int client_fd); |
| |
| |
| int main(int argc, char * argv[]) |
| { |
| |
| struct sockaddr_in client_socket = initParam(); |
| |
| int client_fd = initClient(client_socket); |
| |
| dealData(client_fd); |
| |
| |
| close(client_fd); |
| exit(EXIT_SUCCESS); |
| } |
| |
| |
| void dealData(int client_fd) |
| { |
| char buf[20] ={0}; |
| |
| |
| for (int i = 0; i < 3; i++) |
| { |
| char str[] = "hello.\n"; |
| send(client_fd, str, sizeof(str), 0); |
| sleep(1); |
| } |
| |
| |
| |
| recv(client_fd, buf, sizeof(buf), 0); |
| printf("MSG:%s", buf); |
| } |
| |
| |
| struct sockaddr_in initParam(void) |
| { |
| struct sockaddr_in client_socket; |
| |
| client_socket.sin_family = AF_INET; |
| client_socket.sin_addr.s_addr = inet_addr("172.171.232.205"); |
| client_socket.sin_port = htons(8001); |
| bzero(&(client_socket.sin_zero), 8); |
| |
| return client_socket; |
| } |
| |
| |
| int initClient(struct sockaddr_in client_socket) |
| { |
| int client_fd; |
| |
| client_fd = socket(AF_INET, SOCK_STREAM, 0); |
| if (client_fd == -1) |
| { |
| perror("socket error.\n"); |
| exit(1); |
| } |
| else |
| { |
| printf("socket successfully.\n"); |
| } |
| |
| |
| int client_connect; |
| client_connect = connect(client_fd, (struct sockaddr *)&client_socket, sizeof(client_socket)); |
| if (client_connect == -1) |
| { |
| perror("socket error.\n"); |
| exit(1); |
| } |
| else |
| { |
| printf("connect successfully.\n"); |
| } |
| |
| return client_fd; |
| } |
第四版
| |
| |
| |
| |
| |
| |
| #include <stdio.h> |
| #include <sys/socket.h> |
| #include <sys/types.h> |
| #include <netinet/in.h> |
| #include <stdlib.h> |
| #include <arpa/inet.h> |
| #include <string.h> |
| #include <unistd.h> |
| |
| #define MAXSLEEP 8 |
| |
| struct sockaddr_in initParam(void); |
| int initClient(struct sockaddr_in client_socket); |
| void dealData(int client_fd); |
| int connect_retry_2(int client_fd, struct sockaddr_in client_socket); |
| |
| |
| int main(int argc, char * argv[]) |
| { |
| |
| struct sockaddr_in client_socket = initParam(); |
| |
| int client_fd = initClient(client_socket); |
| |
| dealData(client_fd); |
| |
| |
| close(client_fd); |
| exit(EXIT_SUCCESS); |
| } |
| |
| |
| void dealData(int client_fd) |
| { |
| char buf[20] ={0}; |
| |
| |
| for (int i = 0; i < 3; i++) |
| { |
| char str[] = "hello.\n"; |
| send(client_fd, str, sizeof(str), 0); |
| sleep(1); |
| } |
| |
| |
| |
| recv(client_fd, buf, sizeof(buf), 0); |
| printf("MSG:%s", buf); |
| } |
| |
| |
| struct sockaddr_in initParam(void) |
| { |
| struct sockaddr_in client_socket; |
| |
| client_socket.sin_family = AF_INET; |
| client_socket.sin_addr.s_addr = inet_addr("172.171.232.205"); |
| client_socket.sin_port = htons(8001); |
| bzero(&(client_socket.sin_zero), 8); |
| |
| return client_socket; |
| } |
| |
| |
| int initClient(struct sockaddr_in client_socket) |
| { |
| int client_fd; |
| |
| client_fd = socket(AF_INET, SOCK_STREAM, 0); |
| if (client_fd == -1) |
| { |
| perror("socket error:"); |
| exit(1); |
| } |
| else |
| { |
| printf("socket successfully.\n"); |
| } |
| |
| |
| int client_connect; |
| client_connect = connect(client_fd, (struct sockaddr *)&client_socket, sizeof(client_socket)); |
| if (client_connect == -1) |
| { |
| perror("connect error:"); |
| |
| } |
| else |
| { |
| printf("connect successfully.\n"); |
| } |
| client_fd = connect_retry_2(client_fd, client_socket); |
| return client_fd; |
| } |
| |
| |
| int connect_retry_2(int client_fd, struct sockaddr_in client_socket) |
| { |
| int numsec, fd; |
| |
| for (numsec = 1; numsec <= MAXSLEEP; numsec <<= 1) |
| { |
| if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
| { |
| return -1; |
| } |
| if (connect(client_fd, (struct sockaddr *)&client_socket, sizeof(client_socket)) >= 0) |
| { |
| return fd; |
| } |
| close(fd); |
| if (numsec <= MAXSLEEP / 2) |
| { |
| printf("Connect retry\n"); |
| sleep(numsec); |
| } |
| } |
| return -1; |
| } |
| |
| |
| int connect_retry() |
| { |
| int numsec; |
| |
| for (numsec = 1; numsec <= MAXSLEEP; numsec <<= 1) |
| { |
| if (connect(sockfd, addr, alen) == 0) |
| { |
| return (0); |
| } |
| |
| if (numsec <= MAXSLEEP / 2) |
| { |
| sleep(numsec); |
| } |
| } |
| return -1; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本