20080307 windows c socket soap http://www.shengfang.org arm-elf-gcc -nostartfiles -Wl, -elf2flt -o sockaspx socketaspx.c -I/home/sf/sfsf/uClinux-IST/uClibc/include -L/home/sf/sfsf/uClinux-IST/uClibc/lib -fpermissive /home/sf/sfsf/uClinux-IST/lib/uClibc/lib/crt0.o -lc -lpthread -lm #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 PORT 5566 /* the port client will be connecting to */ #define MAXDATASIZE 1500 /* max number of bytes we can get at once */ int main(int argc, char *argv[]) { int sockfd, numbytes; char buf[MAXDATASIZE] ; char* req="POST /test/websrvtest/s.asmx HTTP/1.1\r\n" "Host: www.shengfang.org:5566\r\n" "User-Agent: gSOAP/2.7\r\n" "Content-Type: text/xml; charset=utf-8\r\n" "Content-Length: 481\r\n" "Connection: close\r\n" "SOAPAction: \"http://tempuri.org/HelloWorld\"\r\n\r\n" "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:ns2=\"http://tempuri.org/Service1Soap\" xmlns:ns1=\"http://tempuri.org/\" xmlns:ns3=\"http://tempuri.org/Service1Soap12\"><SOAP-ENV:Body><ns1:HelloWorld></ns1:HelloWorld></SOAP-ENV:Body></SOAP-ENV:Envelope>"; struct hostent *he; struct sockaddr_in their_addr; /* connector's address information */ if(argc != 2) { fprintf(stderr,"usage: client hostname\n"); exit(1); } printf("1\r\n"); if((he=gethostbyname(argv[1])) == NULL) { /* get the host info */ herror("get host by name" ) ; exit(1); } printf("2\r\n"); if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1) ; } printf("3\r\n"); their_addr.sin_family = AF_INET; /* host byte order */ their_addr.sin_port = htons(PORT); /* short, network byte order */ their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero), 8); /* zero the rest of the struct */ printf("4\r\n"); if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { perror("connect") ; exit( 1 ) ; } printf("5\r\n"); if (send(sockfd,req, strlen(req), 0) == -1) { perror("send error"); close(sockfd); exit(0); } printf("6\r\n"); numbytes=1; while(numbytes>0) { numbytes=recv(sockfd,buf,MAXDATASIZE,0); if(numbytes>0) buf[numbytes]=0; else buf[0]=0; printf("\nReceived bytes:%d\n",numbytes); printf("Result:\n%s",buf); } printf("7\r\n"); buf[numbytes] = '\0'; printf("Received: %s",buf); close(sockfd); printf("8\r\n"); return 0; }
字体:大 中 小 |