[C.C++] 写一段用网络将英文翻译成中文的程序

1778 4
狼毛 2023-8-25 20:24:18 | 显示全部楼层 |阅读模式
  1. // 写一段用网络将英文翻译成中文的程序
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <netdb.h>
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <sys/stat.h>

  14. int main(int argc, char *argv[])
  15. {
  16.     int sockfd;
  17.     struct sockaddr_in servaddr;
  18.     char buf[1024];
  19.     int n;
  20.     if (argc != 2)
  21.     {
  22.         printf("usage: tcpclient <IPaddress>\n");
  23.         exit(1);
  24.     }
  25.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  26.     bzero(&servaddr, sizeof(servaddr));
  27.     servaddr.sin_family = AF_INET;
  28.     servaddr.sin_port = htons(12345);
  29.     inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
  30.     connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  31.     while (1)
  32.     {
  33.         printf("Please enter the message:");
  34.         fgets(buf, 1024, stdin);
  35.         write(sockfd, buf, strlen(buf));
  36.         n = read(sockfd, buf, 1024);
  37.         if (n == 0)
  38.         {
  39.             printf("The other side has been closed.\n");
  40.             break;
  41.         }
  42.         else
  43.         {
  44.             printf("The message from server is:\n");
  45.             write(STDOUT_FILENO, buf, n);
  46.         }
  47.     }
  48.     close(sockfd);
  49.     return 0;
  50. }
复制代码
qq616310998 2023-8-26 11:06:31 | 显示全部楼层
如何运行此代码

点评

VS,不是VSC,还有的话看你是外行,建议用DEV  详情 回复 发表于 2023-10-21 23:03
狼毛 2023-8-26 19:00:04 | 显示全部楼层
在vscode编译即可.
开朗的盟员 2023-10-21 23:03:09 | 显示全部楼层

VS,不是VSC,还有的话看你是外行,建议用DEV
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

狼毛

精英红客

关注
  • 206
    主题
  • 2
    粉丝
  • 1
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行