【socket编程问题】向一个未connect()的sockfd写入数据,程序退出了,为什么?
描述如下:
我用socket()创建了一个sockfd,但是故意连接失败(或者不连接),这时再向sockfd写入一些数据write(),照理说应该返回我错误,但是程序毫无征兆的退出了,这是为什么呢?太奇怪了。
源代码如下:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <pthread.h>#include <string.h>#include <time.h>int main(int argc, char *argv[]){ /*取得main函数参数*/ struct sockaddr_in address;//socket地址 address.sin_family = AF_INET; address.sin_addr.s_addr = inet_addr("127.0.0.1"); address.sin_port = htons(6666); int len = sizeof(address); char send_buf[30] = "大家好,我是发送内容";//发送内容 /*如果有参数,则以参数里的内容作为IP连接,如果无参数,则使用默认值*/ if (argc != 4) { printf("*******************************************\r\n"); printf("你输入的参数不全,正确的格式应该为:\r\n"); printf("./sc_eth_com \"ip地址\" \"端口\" \"发送内容\"\r\n"); printf("未输入的参数将使用默认值连接\r\n"); printf("*******************************************\r\n"); } if (argc > 1) { address.sin_addr.s_addr = inet_addr(argv[1]); if (address.sin_addr.s_addr == INADDR_NONE) { printf("IP地址格式错误,参数被强制转换为127.0.0.1\r\n"); address.sin_addr.s_addr = inet_addr("127.0.0.1"); } } if (argc > 2) { address.sin_port = htons((unsigned short)atoi(argv[2])); } if (argc > 3) { strncpy(send_buf, argv[3], 29); send_buf[29] = '\0'; } /*展示所有参数*/ char port_str[6] = {0}; sprintf(port_str, "%d", ntohs(address.sin_port)); printf("参数值 >>> IP地址:%s\r\n", inet_ntoa(address.sin_addr)); printf("参数值 >>> 端口号:%s\r\n", port_str); printf("参数值 >>> 发送内容:[%s]\r\n", send_buf); #if 1 if (write(5, send_buf, strlen(send_buf)+1) < 0) { perror("随便一个write");//这里不会退出 } #endif /*打开一个套接字*/ int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("创建socket套接字失败"); } printf("创建的套接字sockfd的值为[%d]\r\n", sockfd); #if 1 int retval; retval = write(sockfd, send_buf, 20);//程序在这里退出了 if (retval < 1) { printf("发送内容失败\r\n"); } else { printf("发送内容成功\r\n"); } #endif /*开始连接*/ retval = connect(sockfd, (struct sockaddr *)&address, len); if (retval == -1) { perror("连接服务器失败"); printf("sockfd 为 [%d]\r\n", sockfd); //exit(-1); } printf("开始发送\r\n"); while (1) { retval = write(sockfd, send_buf, 20); if (retval < 1) { printf("发送内容失败\r\n"); } else { printf("发送内容成功\r\n"); } sleep(1); } char recv_buf[20] = {0}; while (1) { retval = recv(sockfd, recv_buf, 20, MSG_DONTWAIT); printf("接收内容 <<< [%s]\r\n长度为[%d]字节\r\n", recv_buf, retval); sleep(1); } close(sockfd); exit(0);}
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <pthread.h>#include <string.h>#include <time.h>#include <signal.h>int main(int argc, char *argv[]){ /*取得main函数参数*/ struct sockaddr_in address;//socket地址 address.sin_family = AF_INET; address.sin_addr.s_addr = inet_addr("127.0.0.1"); address.sin_port = htons(6666); int len = sizeof(address); char send_buf[30] = "大家好,我是发送内容";//发送内容 /*如果有参数,则以参数里的内容作为IP连接,如果无参数,则使用默认值*/ if (argc != 4) { printf("*******************************************\r\n"); printf("你输入的参数不全,正确的格式应该为:\r\n"); printf("./sc_eth_com \"ip地址\" \"端口\" \"发送内容\"\r\n"); printf("未输入的参数将使用默认值连接\r\n"); printf("*******************************************\r\n"); } if (argc > 1) { address.sin_addr.s_addr = inet_addr(argv[1]); if (address.sin_addr.s_addr == INADDR_NONE) { printf("IP地址格式错误,参数被强制转换为127.0.0.1\r\n"); address.sin_addr.s_addr = inet_addr("127.0.0.1"); } } if (argc > 2) { address.sin_port = htons((unsigned short)atoi(argv[2])); } if (argc > 3) { strncpy(send_buf, argv[3], 29); send_buf[29] = '\0'; } /*展示所有参数*/ char port_str[6] = {0}; sprintf(port_str, "%d"