• socket_client


    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    #include <sys/types.h>

    #include <sys/socket.h>

    #include <sys/un.h>

    #include <netinet/in.h>

    #include <unistd.h>

     

    //#define UNIX_DOMAIN "/tmp/UNIX.domain"

    #define DATELEN 1024

     

    int main(int argc, char *argv[])

    {

    int GuiConnect_fd = -1;

    int iRet = -1;

    int iRecvLen = 0;

    int iSendLen = 0;

    char GuiSendBuf[DATELEN] = {0};

    char GuiRecvBuf[DATELEN] = {0};

     

    //static struct sockaddr_un ServAddr;

    struct sockaddr_in ServAddr;

     

    //creat unix socket

    //GuiConnect_fd = socket(PF_UNIX, SOCK_STREAM, 0);

    GuiConnect_fd = socket(AF_INET, SOCK_STREAM, 0);

    printf("== GuiConnect_fd = %d ", GuiConnect_fd);

     

    if (GuiConnect_fd < 0)

    {

    perror("cannot create communication socket");

    return 1;

    }

     

    //ServAddr.sun_family = AF_UNIX;

    //strncpy(ServAddr.sun_path, UNIX_DOMAIN, sizeof(ServAddr.sun_path) - 1);

     

    memset(&ServAddr, 0, sizeof(ServAddr));

    ServAddr.sin_family = AF_INET;

    ServAddr.sin_port = htons(5050);

    ServAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

     

    //connect server

    iRet = connect(GuiConnect_fd, (struct sockaddr*)&ServAddr, sizeof(ServAddr));

    if(-1 == iRet)

    {

    perror("cannot connect to the server");

    close(GuiConnect_fd);

    return 1;

    }

     

    //receive and send message

    memset(GuiRecvBuf, 0, DATELEN);

    printf("GUI Receie Msg from TDC ");

    //iRecvLen = read(GuiConnect_fd, GuiRecvBuf, sizeof(GuiRecvBuf));

    iRecvLen = recv(GuiConnect_fd, GuiRecvBuf, DATELEN, 0);

    printf("receive message from server (%d) :%s ", iRecvLen, GuiRecvBuf);

    printf("GUI Send msg to TDC server: ");

    memset(GuiSendBuf, 0, DATELEN);

    strcpy(GuiSendBuf, "receive message from GUI client ");

    GuiSendBuf[strlen(GuiSendBuf)] = '';

    //iSendLen = write(GuiConnect_fd, GuiSendBuf, sizeof(GuiSendBuf));

    iSendLen = send(GuiConnect_fd, GuiSendBuf, strlen(GuiSendBuf), 0);

    printf("wrint Date Len to server (%d) : %s ", iSendLen, GuiSendBuf);

     

    close(GuiConnect_fd);

     

    return 0;

    }

  • 相关阅读:
    CSS3 渐变 透明 圆角
    使用JSON作为函数的参数(转载)
    如何让输入的单词首字母大写
    mysql 修改表/字段 增加/删除表索引
    Jquery Mobile 客户端验证
    如何写出漂亮的js代码(转载)
    GoogleMap添加一个Marker
    Log4j的使用【转载】
    Google Map 自定义infowindow
    MYSQL重装出现could not start the service mysql error:0处理(已验证可以使用)
  • 原文地址:https://www.cnblogs.com/yinguojin/p/14811403.html
Copyright © 2020-2023  润新知