如何使用Telnet库从服务器发送查询和从客户端接收回显
How to send Queries from Server and receive echo from client using Telnet libraries
我编写了一个套接字程序,其中客户端向服务器发送存储在字符缓冲区中的Telnet协商。服务器接收它,并使用每个缓冲区的消息长度进行响应。这是代码:
服务器.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/telnet.h>
#include <unistd.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2)
{
fprintf(stderr,"ERROR, no port providedn");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{
perror("ERROR on binding");
exit(1);
}
if(listen(sockfd,5)<0)
{
perror("Error on listen");
}
while(1)
{
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
perror("ERROR on accept");
exit(1);
}
//fork to hande this client
if (fork() == 0)
{ // client code no longer need this;
close(sockfd);
// loop until a closed or error state happens
ssize_t n =0;
while ((n = read(newsockfd,buffer,sizeof(buffer)-1))>0)
{
printf("Recieved : %*sn",static_cast<int>(n),buffer);
//send response
static const char resp[] = "I got yout message n";
n = write(newsockfd, resp , sizeof(resp)-1);
if(n<0)
{
perror("Error writing to socket ");
exit(1);
}
}
close(newsockfd);
exit(0);
}
close(newsockfd); }
return 0;
}
客户端.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/telnet.h>
char buf1[] = {0xff, 0xfb, 0x18, 0xff, 0xfb, 0x1f};
void read (int sock)
{
char buffer[256];
/* Now read server response */
memset(buffer, 0, sizeof(buffer));
int n = recv( sock, buffer, 255, 0 );
if (n < 0)
{
perror("ERROR reading from socket");
return;
}
printf("n%d bytes received buffer is: %s", n, buffer);
for (int i = 0; i < n; i++)
printf("%2x ", buffer[i]);//printing ascii characters
printf("n");
}
void mwrite (int sock, char * buf, int size)
{
int n = send( sock, buf, size, 0 );
if (n < 0)
{
perror("ERROR writing to socket");
return;
}
printf("Bytes Sent: %dn", n);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname portn", argv[0]);
return(0);
}
portno = atoi(argv[2]);
/* Create a socket point */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
return(1);
}
server = gethostbyname(argv[1]);
if (server == NULL)
{fprintf(stderr,"ERROR no such host n");
exit(0);}
bzero((char *) &serv_addr , sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char*)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons( portno );
/* Now connect to the server */
if (connect( sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr) ) < 0)
{
perror("ERROR connecting");
return(1);
}
n= write(sockfd,buffer,strlen(buffer));
if(n<0)
printf("ERROR writing in socket %d len %d", n, strlen(buffer));
n = read(sockfd, buffer, 255);
if(n<0)
perror("ERROR reading from socket");
printf("%sn",buffer);
bzero(buffer,256);
buffer[0] = 0x0d;
buffer[1] = 0x0a;
printf("nSend buff....");
mwrite ( sockfd, buffer,2);
read(sockfd);
mwrite( sockfd, buf1, sizeof(buf1));
printf("nSend buff1....");
read(sockfd);
printf("nRecieved all negotiation buffers");
close(sockfd); // close socket
return 0;
输出为:客户端输出:
debian:~/echoserver$ ./client 127.0.0.1 8088
I got your message
Send buff....Bytes Sent: 2
20 bytes received buffer is: I got yout message
49 20 67 6f 74 20 79 6f 75 74 20 6d 65 73 73 61 67 65 20 a
Send buf1....Bytes Sent: 6
20 bytes received buffer is: I got yout message
49 20 67 6f 74 20 79 6f 75 74 20 6d 65 73 73 61 67 65 20 a
Recieved all negotiation buffers
服务器输出:
@debian:~/echoserver$ ./server 8088
Recieved : .N=��
Recieved :
=��
我还想知道我们是否想反转这个过程,也就是说,从服务器向客户端发送一个3字节的查询序列,其中字节1解释为命令(0xff),字节2命令代码,字节3选项代码。Telnet是流式传输的,因此接收到的消息可能包含一个或多个查询。例如,服务器在接受连接时发送一条3字节的消息(ff,fd,18)。客户端应该只回显Won't(ff,fc,18)
例如:
Server : Sending Query 1 : 0xff,0xfd,0x18
Client echo : 0xff , 0xfc , 0x18
提前谢谢。
您的buf
数组不是字符串(没有终止的' '
字符),而是对它们调用strlen()
。这会给你带来不明确的行为。由于它们是二进制数组,请改用sizeof
。
这在您自己的跟踪printf()
s中可以清楚地看到,第一行写着"Bytes Sent: 18
",但很明显buf1
中只有6个字节。
在client.cpp中,buffer
在首次使用之前不会初始化。
int main(int argc, char *argv[]) {
char buffer[256];
...
n= write(sockfd,buffer,strlen(buffer));
当接收到来自套接字的"字符串"时,是附加一个试用' '
还是确保发送方已发送该字符串。
n = write(sockfd,buffer,strlen(buffer)+1);
...
n = read(sockfd, buffer, sizeof buf - 1);
if (n > 0 && buffer[n-1] != ' ') {
buffer[n-1] = ' ';
n++;
}
函数签名在client.cpp
中不对齐,所以即使这被标记为C
,这也是C++
帖子。
void read (int sock) { ... }
int main(int argc, char *argv[]) {
...
n = read(sockfd, buffer, 255);
相关文章:
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 我可以与 python 服务器而不是 c++ 客户端建立 tcp/ip 套接字吗?
- C++套接字客户端到 Python 服务器未创建连接
- 当客户端在 write() 期间终止连接时,由对等套接字错误重置连接
- 在 1 个服务器 n 客户端套接字 C++ MFC 应用程序中更新数据的客户端
- TCP 服务器的异步读取使用 boost::asio 打印客户端套接字发送的数据
- ZMQ - 客户端服务器:客户端意外关闭,服务器如何检测到?
- 螺纹安全管道端接
- 使用 Broadcast 发出的从节点服务器发送的数据不能被 C++ 套接字 IO 客户端读取
- TCP套接字(客户端-服务器)recv()返回-1值
- Asio端接插座功能:取消、关闭、关闭、释放
- 节点.js HTTP 无法通过套接字接收来自C++客户端的请求
- 仅通过建立一次TCP连接将Recv从客户端发送到服务器套接字
- 为瘦客户端设计客户端(厚)/服务器+额外的REST API
- 套接字编程中的客户端到客户端通信与许多客户端
- 套接字编程中的客户端到客户端消息传递
- 如何使用Telnet库从服务器发送查询和从客户端接收回显
- 使用C++和Boost::Asio的N方客户端到客户端通信
- 客户端到客户端套接字通信
- 图像发送通过socket c++服务器端和c#客户端