如何使用Telnet库从服务器发送查询和从客户端接收回显

How to send Queries from Server and receive echo from client using Telnet libraries

本文关键字:端接 客户 客户端 Telnet 何使用 服务器 查询      更新时间:2023-10-16

我编写了一个套接字程序,其中客户端向服务器发送存储在字符缓冲区中的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);