#include<stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SERVPORT 3333
#define MAXDATASIZE 1000 /*每次最大数据传输量 */
typedef unsigned char UINT8;
typedef signed char INT8;
typedef unsigned short int UINT16;
typedef signed short int INT16;
typedef unsigned int UINT32;
typedef signed int INT32;
typedef unsigned char BOOLEAN;
typedef unsigned short int TC_OPCODE_T;
typedef struct
{
UINT8 reserved1 : 7; // Reserved
UINT8 cmd_rsp_flag : 1; // Command/Response Flag 0 indicate command 1 response
UINT8 seq_tag; // Sequence Tag // ensure the same command between request and response
TC_OPCODE_T opcode; // Opcode
UINT8 client_tag; // Reserved
UINT8 no_rsp_reqd_flag : 1; // No Response Required Flag 0 expected response 1 no expected
UINT8 reserved3 : 7; // Reserved
UINT16 reserved4; // Reserved
UINT32 length; // Data Length of Request
} TC_CMD_REQ_HDR_T;
typedef struct tc_tcmd
{
TC_CMD_REQ_HDR_T cmd_header; // TCMD command header
//if will change the communication mode between AP<-->CP or AP<-->CP???
int cmd_sender;
void * cmd_pdata; // TCMD cmd data
} TC_TCMD_T;
int main(int argc, char *argv[]){
/*
TC_CMD_REQ_HDR_T header;
TC_CMD_REQ_HDR_T header1;
TC_TCMD_T tcmd;
TC_TCMD_T tcmd_one;
TC_TCMD_T tcmd_two;
TC_TCMD_T *ptcmd=&tcmd_two;
int counter;
char buffer[]="hello";
char buffer_test[20];
header.opcode=0x0039;
header.length=0;
tcmd.cmd_header=header;
tcmd.cmd_pdata=NULL;
header1.opcode=0x0039;
header1.length=strlen(buffer);
tcmd_one.cmd_header=header1;
tcmd_one.cmd_pdata=buffer;
printf( " tcmd_header length is %d\n ",sizeof(TC_CMD_REQ_HDR_T) );
printf( " tcmd length is %d\n ",sizeof(tcmd) );
printf( " tcmd_one length is %d\n ",sizeof(tcmd_one) );
memcpy( buffer_test, &tcmd_one , sizeof(TC_CMD_REQ_HDR_T));
memcpy( buffer_test+sizeof( TC_CMD_REQ_HDR_T),"hello" , 6 );
memcpy( &(tcmd_two.cmd_header), buffer_test, sizeof(TC_CMD_REQ_HDR_T));
//memcpy( &(tcmd_two.cmd_header), buffer_test, sizeof(TC_CMD_REQ_HDR_T));
tcmd_two.cmd_pdata= buffer_test+ sizeof(TC_CMD_REQ_HDR_T);
printf( "data: %s\n" ,ptcmd->cmd_pdata);
*/
//socket
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
char send_buf[200];
snprintf(send_buf, 200 - 1,"GET /%s HTTP/1.1\r\nHOST:%s\r\nCONNECTION:CLOSE\r\n\r\n",argv[3], argv[1]);
printf("send_buf:%s" , send_buf );
if (argc < 3) {
fprintf(stderr,"Please enter the server's hostname and port!\n");
exit(1);
}
if((host=gethostbyname(argv[1]))==NULL) {
perror("gethostbyname出错!");
exit(1);
}
/*
if(inet_aton(argv[1],&serv_addr.sin_addr)!=0)
{
host=gethostbyaddr((char *)&serv_addr.sin_addr,4,AF_INET);
printf("Address information of Ip %s\n",argv[1]);
}
*/
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("socket创建出错!");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(atoi(argv[2]));
printf( "port:%d\n", atoi(argv[2]));
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
//inet_aton(argv[1],&serv_addr.sin_addr);
//serv_addr.sin_addr.s_addr = INADDR_ANY;
printf("argv[1]%s\n" , argv[1]);
printf( "inet_aton success!\n" );
bzero( &(serv_addr.sin_zero),8);
printf( "receiving....!\n" );
if (connect(sockfd, (struct sockaddr *) &serv_addr, \
sizeof(struct sockaddr)) == -1) {
perror("connect出错!");
exit(1);
}
printf( "receiving..hostname..%s!\n",host->h_name );
printf( " serv_addrlength is %d ",sizeof(serv_addr) );
if (send(sockfd, (void *)send_buf, 200 , 0) == -1){
perror( "send出错!");
close(sockfd);
exit(0);
}
printf( "received...\n" );
while(1)
{
if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) ==-1) {
perror("recv出错!");
exit(1);
}
if( recvbytes==0 ){break;};
buf[recvbytes] = '\0';
printf( "Received: %s",buf);
break;
}
buf[recvbytes] = '\0';
printf( "Received: %s",buf);
close(sockfd);
}
|