tcpclient.c

来自「这是VxWorks下的TCP/IP通信的源码」· C语言 代码 · 共 108 行

C
108
字号
/* tcpClient.c - TCP client Demo */ 

/* Autor - LiuCY */
 
/* 
DESCRIPTION 
This file contains the client-side of the VxWorks TCP Demo code. 
The Demo code demonstrates the usage of several BSD 4.4-style 
socket routine calls. 
*/ 
 
/* includes */ 
 
#include "vxWorks.h" 
#include "sockLib.h" 
#include "inetLib.h" 
#include "stdioLib.h" 
#include "strLib.h" 
#include "hostLib.h" 
#include "ioLib.h" 
#include "tcpHeader.h" 
 
/**************************************************************************** 
* 
* tcpClient - send requests to server over a TCP socket 
* 
* This routine connects over a TCP socket to a server, and sends a 
* user-provided message to the server.  Optionally, this routine 
* waits for the server's reply message. 
* 
* This routine may be invoked as follows: 
*       -> tcpClient
*       -> MESSAGE FROM SERVER: 
*       Server received your message 
* 
* RETURNS: OK, or ERROR if the message could not be sent to the server. 
*/ 
 
STATUS tcpClient (void) 
    { 
    struct request      myRequest;     /* request to send to server */ 
    struct sockaddr_in  serverAddr;    /* server's socket address */ 
    char                replyBuf[REPLY_MSG_SIZE]; /* buffer for reply */ 
    char                reply;         /* if TRUE, expect reply back */ 
    int                 sockAddrSize;  /* size of socket address structure */ 
    int                 sFd;           /* socket file descriptor */ 
    int                 mlen;          /* length of message */ 
    char *              serverName;     /* name or IP address of server */ 
    serverName = "192.168.0.194";
 
    /* create client's socket */ 
    if ((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR) 
        { 
        perror ("socket"); 
        return (ERROR); 
        } 
 
    /* bind not required - port number is dynamic */ 
    /* build server socket address */ 
    sockAddrSize = sizeof (struct sockaddr_in); 
    bzero ((char *) &serverAddr, sockAddrSize); 
    serverAddr.sin_family = AF_INET; 
    serverAddr.sin_len = (u_char) sockAddrSize; 
    serverAddr.sin_port = htons (SERVER_PORT_NUM); 
 
    if (((serverAddr.sin_addr.s_addr = inet_addr (serverName)) == ERROR) && 
        ((serverAddr.sin_addr.s_addr = hostGetByName (serverName)) == ERROR)) 
        { 
        perror ("unknown server name"); 
        close (sFd); 
        return (ERROR); 
        } 
 
    /* connect to server */ 
    if (connect (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR) 
        { 
        perror ("connect"); 
        close (sFd); 
        return (ERROR); 
        } 

    strcpy(myRequest.message,"Hello Word!\n");
    myRequest.reply = TRUE;
    /* send request to server */ 
 
    if (write (sFd, (char *) &myRequest, sizeof (myRequest)) == ERROR) 
        { 
        perror ("write"); 
        close (sFd); 
        return (ERROR); 
        } 

    if (myRequest.reply)        /* if expecting reply, read and display it */  
        { 
        if (read (sFd, replyBuf, REPLY_MSG_SIZE) < 0) 
            { 
            perror ("read"); 
            close (sFd); 
            return (ERROR); 
            }
        printf ("MESSAGE FROM SERVER:\n%s\n", replyBuf); 
        }

    close (sFd); 
    return (OK); 
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?