#include <math.h> #include <sys/time.h> #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <sys/types.h> int main() { struct sockaddr_in addr; addr.sin_addr.s_addr = inet_addr("127.0.0.1"); addr.sin_port = htons(5000); addr.sin_family = AF_INET; bzero(&(addr.sin_zero),8); int socketHandl = socket(AF_INET, SOCK_STREAM, 0); int ret = connect(socketHandl, (struct sockaddr *)&addr, sizeof(addr)); if(ret == -1){ printf("connect error!!"); }else{ const char *msg = "hello world!"; send(socketHandl, msg, strlen(msg), 0); } close(socketHandl); printf("%s\n", inet_ntoa(addr.sin_addr)); printf("%u\n", htons(5000)); return 0; }
以上代码连结本机的5000端口,并且发送hello world!的字符给服务端
时间: 2024-10-13 00:28:41