⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.c

📁 周立功Magic2410实验箱光盘源码 第5章Linux应用程序编写 5.1 HelloWorld程序实验 5.2 Linux定时器实验 5.3多进程实验 5.4多线程实验 5.5文件和目
💻 C
字号:
/*****************************************Copyright (c)****************************************************                               Guangzhou Zhiyuan Electronic Co.,LTD.**                                     graduate school**                                 http://www.zyinside.com****------------------------------------- File Info ------------------------------------------------------** File name:           client.c** Last modified Date:  2005-12-30** Last Version:        1.0** Descriptions:        client of TCP.**------------------------------------------------------------------------------------------------------** Created by:          Ganda** Created date:        2005-12-27** Version:             1.0** Descriptions:        Preliminary version.****------------------------------------------------------------------------------------------------------** Modified by:		Chenxibing** Modified date:	2005-12-30	** Version:		1.0.1** Descriptions:	modified for MagicARM2410.***********************************************************************************************************/#include <stdio.h> #include <stdlib.h>#include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #define PORT 5000                      // The port which is communicate with server#define LENGTH 256                     // Buffer lengthint main(int argc, char *argv[]){    int sockfd;                        // Socket file descriptor    int num;                           // Counter of received bytes      char revbuf[LENGTH];               // Receive buffer    struct sockaddr_in remote_addr;    // Host address information    /* Check parameters number */    if (argc != 2)                         {            printf ("Usage: client HOST IP (ex: ./client 192.168.0.94).\n");        return (0);    }    /* Get the Socket file descriptor */    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)    {        printf("ERROR: Failed to obtain Socket Descriptor!\n");        return (0);    }        /* Fill the socket address struct */    remote_addr.sin_family = AF_INET;                   // Protocol Family    remote_addr.sin_port = htons(PORT);                 // Port number    inet_pton(AF_INET, argv[1], &remote_addr.sin_addr); // Net Address    bzero(&(remote_addr.sin_zero), 8);                  // Flush the rest of struct    /* Try to connect the remote */    if (connect(sockfd, (struct sockaddr *)&remote_addr,  sizeof(struct sockaddr)) == -1)     {        printf ("ERROR: Failed to connect to the host!\n");        return (0);    }      else    {        printf ("OK: Have connected to the %s\n",argv[1]);    }    /* Try to connect the server */    while (strcmp(revbuf,"exit") != 0)     // Check remoter command    {              bzero(revbuf,LENGTH);        num = recv(sockfd, revbuf, LENGTH, 0);        switch(num)        {            case -1:        	printf("ERROR: Receive string error!\n");                close(sockfd);                return (0);                            case  0:                close(sockfd);                return(0);                          default:                printf ("OK: Receviced numbytes = %d\n", num);                break;        }                revbuf[num] = '\0';        printf ("OK: Receviced string is: %s\n", revbuf);    }    close (sockfd);    return (0);}

⌨️ 快捷键说明

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