日期:2014-05-16 浏览次数:20853 次
#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 <s