linux下c语言socket client
/* ============================================================================ Name : socket.c Author : Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <netdb.h>#include <sys/socket.h>#include <netinet/in.h>#include<arpa/inet.h>const int MAXLINE = 5;int main(void) {int sockfd;struct sockaddr_in server_addr;struct hostent *host;host = gethostbyname("127.0.0.1");sockfd = socket(AF_INET, SOCK_STREAM, 0);if (sockfd == -1) {perror("出错了");exit(1);}bzero(&server_addr, sizeof(server_addr));server_addr.sin_family = AF_INET;server_addr.sin_port = htons(1024);server_addr.sin_addr = *((struct in_addr*) host->h_addr);int cn = connect(sockfd, (struct sockaddr *) &server_addr,sizeof(server_addr));if (cn == -1) {perror("出错了");exit(1);}char *buf = "你好啊...";write(sockfd, buf, strlen(buf));close(sockfd);return EXIT_SUCCESS;}