Linux 网络编程基本问题求助
问题描述:正常编译,执行时错误提示如下:coat@linux:~/codetest/sockserver$ ./a.out 127.0.0.1 985
connect: Socket operation on non-socket
代码:
程序代码:
/*************************************************** * * UNIX socket编程简单示例:发送文字的服务端 * By——thlgood(www.) * 2011年光棍节的前几个小时 **************************************************/ #include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/types.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { int sock, port; struct sockaddr_in server_addr; char str[] = "Hello,World!"; int str_length = strlen(str); /*********************************************************** *检验程序参数,argv[1]为目标主机,argv[2]为目标主机的端口 ***********************************************************/ if (argc != 3) { printf("Error! Usage:【主机名】 【端口】"); exit(1); } if (sock = socket(AF_INET, SOCK_STREAM, 0) < 0) { perror("socket"); exit(2); } port = atoi(argv[2]); memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); if ((inet_pton(AF_INET, argv[1], &server_addr.sin_addr.s_addr)) < 0) { perror("inet_pton"); exit(3); } if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { perror("connect"); exit(4); } if (send(sock, str, str_length, 0) != str_length); { perror("send"); exit(5); } printf("发送成功\n"); close (sock); return 0; }