使用TCP Socket发送Buff1

时间:2014-11-14 15:19:36

标签: sockets tcp

以下是字符串缓冲区

string buff1[]= {"0xff , 0xfd, 0x18,0xff,0xfd,0x23","0xff , 0xfd, 0x1e","0xff , 0xfd, 0x1d","0xff ,0xfd,0x17"};

现在我编写了以下使用二维字符缓冲区的服务器代码:

 char buff1[][3]= {{0xff , 0xfd, 0x18},{0xff , 0xfd, 0x1e},{0xff , 0xfd, 0x1d},{0xff , 0xfd , 0x17}};

服务器代码如下:

#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>

char buff1[][3]= {{0xff , 0xfd, 0x18},{0xff , 0xfd, 0x1e},{0xff , 0xfd, 0x1d},{0xff , 0xfd , 0x17}};

char recbuf[1024];

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;

  sockfd = socket(AF_INET , SOCK_STREAM , 0); // create a socket
  if (sockfd < 0 )
  {
   perror("Error opening socket ");
   exit(1);
  }

  /* Initialize socket structure */
  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);

 /* Now bind the host address using client */
  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");
       exit(1);
     }
        int k = 0;
        int count = 0;
        int ctr;
        clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);

    if (newsockfd < 0)
    {
     perror("ERROR on accept");
     exit(1);
    }
          ctr = sizeof(buff1)/sizeof(buff1[0]) ;

char integer[4];


     printf("\n=====================================================\nServer Side\n=====================================================\n");

       uint32_t len = ctr;
       len = htonl(len);
       send(newsockfd,&len,sizeof(len),0);
       // while ((n = send(newsockfd,&buff1[count][0],3,0))> 0 && ctr>0 )
        for (int i = 0 ; i < ctr ; ++i)

         {        send(newsockfd,buff1[i],3,0);
                  k = recv(newsockfd, recbuf , sizeof(recbuf),0);
                  if (k >= 0)
                  {
           printf("Server Sent query %d to Client:%hhX %hhX %hhX\n", (count)+1 , buff1[count][0], buff1[count][1], buff1[count][2]);
           //printf("n = %d\n",k);
                   printf("Server received response from Client: %hhX %hhX %hhX\n\n", recbuf[0], recbuf[1], recbuf[2]);
                   count++; 
          }
                  else if( k < 0)
               {
                      close(newsockfd);
                      close(sockfd);
                      exit(-1);
                   }


         }
      close(sockfd);
      return(0);
}

我想知道应该在服务器代码中做些什么更改,以便将字符串buff1 [],3个元素及时发送给接收者,即:

Server          Client
ff,fd,18    --> Received 1
ff,fd,23    --> Received 2
ff,fd,1e    --> Received 3
ff,fd,1d    --> Received 4
ff,fd,17    --> Received 5 

0 个答案:

没有答案