69 lines
1.7 KiB
C
69 lines
1.7 KiB
C
/** function comment
|
|
* @Author: 陈逸凡 1343619937@qq.com
|
|
* @Date: 2024-04-17 15:29:56
|
|
* @LastEditors: 陈逸凡 1343619937@qq.com
|
|
* @LastEditTime: 2024-04-18 16:25:48
|
|
* @FilePath: \telnet\telnet_client.c
|
|
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <arpa/inet.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
|
|
#define BUFFER_SIZE 1024
|
|
#define SERVER_IP "127.0.0.1"
|
|
#define SERVER_PORT 23
|
|
|
|
int main()
|
|
{
|
|
int sockfd;
|
|
struct sockaddr_in server_addr;
|
|
char buffer[BUFFER_SIZE];
|
|
ssize_t bytes_read;
|
|
|
|
// 创建socket
|
|
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
|
{
|
|
perror("socket creation failed");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// 设置服务器地址结构
|
|
memset(&server_addr, 0, sizeof(server_addr));
|
|
server_addr.sin_family = AF_INET;
|
|
server_addr.sin_port = htons(SERVER_PORT);
|
|
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
|
|
|
|
// 连接到服务器
|
|
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
|
|
{
|
|
perror("connection failed");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
printf(" connect success\n");
|
|
while (1)
|
|
{
|
|
// 接收数据
|
|
bytes_read = recv(sockfd, buffer, BUFFER_SIZE - 1, 0);
|
|
if (bytes_read == -1)
|
|
{
|
|
perror("recv failed");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
for (int i = 0; i <bytes_read ; i++)
|
|
{
|
|
printf("%d ", buffer[i]);
|
|
}
|
|
|
|
}
|
|
|
|
// 关闭socket
|
|
close(sockfd);
|
|
|
|
return 0;
|
|
} |