zoukankan      html  css  js  c++  java
  • socket客户端小例

    #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
    ", inet_ntoa(addr.sin_addr));
        printf("%u
    ", htons(5000));
        
        return 0;
    }

    以上代码连结本机的5000端口,并且发送hello world!的字符给服务端

  • 相关阅读:
    CSS display使用
    WPF触发器
    WPF动画2
    WPF动画2
    WPF 动画1
    CSS 媒体查询
    [Leetcode] Rotate List
    [Leetcode] Add Two Numbers
    [Leetcode] Sort List
    [Leetcode] Reverse Linked List II
  • 原文地址:https://www.cnblogs.com/zitonglove/p/4979007.html
Copyright © 2011-2022 走看看