Help me find where error? After "strcat", data all become 0x00!!!!
Hi:
I create a http client to send data to server. The code : *********************************************************************/ int main(int argc, char *argv[]) { int sockfd; char buffer[1024]; struct sockaddr_in server_addr; //struct hostent *host; int portnumber,nbytes; char host_addr[256]; char host_file[1024]; char local_file[256]; FILE * fp; char *request; char post[300],host[100],content_len[100]; int send, totalsend; int i; char * pt; char data[19] = {0x41,0x54,0x4d,0x53,0x30,0x31,0x31,0x01,0x01,0x0,0x0,0x0,0x03,0x01,0x0a,0x0a,0x12,0x0f,0x38}; memset(post,0,sizeof(post)); memset(host,0,sizeof(host)); memset(content_len,0,sizeof(content_len)); if(argc!=2) { fprintf(stderr,"Usage:%s web-address\a\n",argv[0]); exit(1); } #if debug printf("parameter.1 is: %s\n", argv[1]); #endif GetHost(argv[1], host_addr, host_file, &portnumber); #if debug printf("webhost:%s\n", host_addr); printf("hostfile:%s\n", host_file); printf("portnumber:%d\n\n", portnumber); printf("Before socket\n"); #endif if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"Socket Error:%s\a\n",strerror(errno)); exit(1); } bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family=AF_INET; server_addr.sin_port=htons(portnumber); //server_addr.sin_addr=*((struct in_addr *) host->h_addr); server_addr.sin_addr.s_addr=inet_addr(host_addr); if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) { fprintf(stderr,"Connect Error:%s\a\n",strerror(errno)); exit(1); } /* sprintf(request,"POST /%s HTTP/1.1\r\n\ Host:%s:%d\r\n\ Content-Length: 7\r\n\ Cache-Control: no-cache\r\n\r\nHello!",host_file,host_addr,portnumber); */ sprintf(post,"POST /%s HTTP/1.1\r\n",host_file); sprintf(host,"Host:%s:%d\r\n",host_addr,portnumber); sprintf(content_len,"Content-Length:%d\r\n\r\n",19); nbytes = strlen(post)+strlen(host)+strlen(content_len)+19+1; request = (char *)malloc(nbytes); if(request==NULL) { return -1; } memcpy(request,post,strlen(post)); strcat(request,host); strcat(request,content_len); strcat(request,data); printf("%s", request); for(i=0;i<nbytes;i++) printf("%02x ",request[i]); printf("\n"); ………… result is : 50 4f 53 54 20 2f 6c 69 66 74 2f 66 61 75 6c 74 54 72 61 6e 73 66 2f 20 48 54 54 50 2f 31 2e 31 0d 0a 48 6f 73 74 3a 31 39 32 2e 31 36 38 2e 31 2e 31 37 32 3a 38 30 38 30 0d 0a 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 31 39 0d 0a 0d 0a 41 54 4d 53 30 31 31 01 01 00 00 00 00 00 00 00 00 00 00 00 but the data is 0x41,0x54,0x4d,0x53,0x30,0x31,0x31,0x01,0x01,0x0,0x0,0x0,0x03,0x01,0x0a,0x0a,0x12,0x0f,0x38 Help me ! thanks! |
Doesn't a null character (i.e 0x00) terminate a string? If so strcat() is probably the wrong function.
|
The problem is in request = (char *)malloc(nbytes);
Allocate more memory and check |
Hi,Thanks all the same!
I have fixed the question: All "strcat" are replaced with “memcpy”. |
All times are GMT -5. The time now is 06:14 AM. |