- // 写一段用网络将英文翻译成中文的程序
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- int main(int argc, char *argv[])
- {
- int sockfd;
- struct sockaddr_in servaddr;
- char buf[1024];
- int n;
- if (argc != 2)
- {
- printf("usage: tcpclient <IPaddress>\n");
- exit(1);
- }
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- bzero(&servaddr, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_port = htons(12345);
- inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
- connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
- while (1)
- {
- printf("Please enter the message:");
- fgets(buf, 1024, stdin);
- write(sockfd, buf, strlen(buf));
- n = read(sockfd, buf, 1024);
- if (n == 0)
- {
- printf("The other side has been closed.\n");
- break;
- }
- else
- {
- printf("The message from server is:\n");
- write(STDOUT_FILENO, buf, n);
- }
- }
- close(sockfd);
- return 0;
- }
复制代码 |
点评
使用道具 举报
使用道具 举报
VS,不是VSC,还有的话看你是外行,建议用DEV
使用道具 举报