异常处理方法:The input stream for an incoming message is null.

本文介绍了解决在使用Webservices技术时遇到的“The input stream for an incoming message is null”异常的方法。异常源于.NET Development Server可能不支持HTTP1.1的ChunkedEncoding。文中提供了一个解决方案,即设置ADBClient不使用ChunkedEncoding。

 做Web services技术作业的时候,遇到了这个异常。通过查找相关资料,终于解决了,详情见[1]。

 

 异常描述:

 org.apache.axis2.AxisFault: The input stream for an incoming message is null.
 at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:92)
 at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:67)
 at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:354)
 at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:417)
 at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
 at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)

 

 ADB Client的解决方法:

 // stub 是利用Axis2 wsdl2java命令生成的*Stub.java类的实例

 stub._getServiceClient().getOptions().setProperty(HTTPConstants.CHUNKED,"false"); 

 这句话的作用是,设置ADB Client不使用Chunked Encoding。原因是.NET Development Server很可能不支持HTTP 1.1的Chunked Encoding。因此,将这个开关置为FALSE。

 

 [1] 中还提到了如何解决JAXWS Client的上述异常问题,研究中... ...

 

 [1] http://old.nabble.com/The-input-stream-for-an-incoming-message-is-null-td24067861.html

client.cpp代码如下: #include <stdio.h> #include <stdlib.h> #include <netdb.h> #include <netinet/in.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]) { (void)argc; (void)argv; int sockfd, n; uint16_t portno; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if (argc < 3) { fprintf(stderr, "usage %s hostname port\n", argv[0]); exit(0); } portno = (uint16_t)atoi(argv[2]); /* Create a socket point */ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR opening socket"); exit(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(server->h_addr, (char *)&serv_addr.sin_addr.s_addr, (size_t)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"); exit(1); } /* Now ask for a message from the user, this message * will be read by server */ printf("Please enter the message: "); bzero(buffer, 256); if (fgets(buffer, 255, stdin) == NULL) { perror("ERROR reading from stdin"); exit(1); } /* Send message to the server */ n = write(sockfd, buffer, strlen(buffer)); if (n < 0) { perror("ERROR writing to socket"); exit(1); } /* Now read server response */ bzero(buffer, 256); n = read(sockfd, buffer, 255); if (n < 0) { perror("ERROR reading from socket"); exit(1); } printf("%s\n", buffer); return 0; } server.cpp代码如下: #include <stdio.h> #include <stdlib.h> #include <netdb.h> #include <netinet/in.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]) { (void)argc; (void)argv; int sockfd, newsockfd; uint16_t portno; unsigned int clilen; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; ssize_t n; /* First call to socket() function */ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR opening socket"); exit(1); } /* Initialize socket structure */ bzero((char *)&serv_addr, sizeof(serv_addr)); portno = 5001; 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 bind() call.*/ if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { perror("ERROR on binding"); exit(1); } /* Now start listening for the clients, here process will * go in sleep mode and will wait for the incoming connection */ listen(sockfd, 5); clilen = sizeof(cli_addr); /* Accept actual connection from the client */ newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen); if (newsockfd < 0) { perror("ERROR on accept"); exit(1); } /* If connection is established then start communicating */ bzero(buffer, 256); n = read(newsockfd, buffer, 255); if (n < 0) { perror("ERROR reading from socket"); exit(1); } printf("Here is the message: %s\n", buffer); /* Write a response to the client */ n = write(newsockfd, "I got your message", 18); if (n < 0) { perror("ERROR writing to socket"); exit(1); } return 0; } 在此代码的基础上修改
最新发布
10-21
### 客户端代码修改 ```cpp #include <stdio.h> #include <stdlib.h> #include <netdb.h> #include <netinet/in.h> #include <unistd.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> #include <time.h> // 修改处开始 #define BUFFER_SIZE 1024 // 接收消息线程函数 void *receive_messages(void *arg) { int sockfd = *(int *)arg; char buffer[BUFFER_SIZE]; while (1) { memset(buffer, 0, BUFFER_SIZE); ssize_t n = read(sockfd, buffer, BUFFER_SIZE); if (n <= 0) { perror("Server disconnected"); exit(1); } time_t now = time(NULL); struct tm *tm_info = localtime(&now); char time_str[26]; strftime(time_str, 26, "%H:%M", tm_info); printf("{%s} %s\n", time_str, buffer); } return NULL; } int main(int argc, char *argv[]) { if (argc < 4) { fprintf(stderr, "usage %s server_address port nickname\n", argv[0]); exit(0); } uint16_t portno = (uint16_t)atoi(argv[2]); char *nickname = argv[3]; struct addrinfo hints, *res, *p; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; // 支持 IPv4 和 IPv6 hints.ai_socktype = SOCK_STREAM; int status = getaddrinfo(argv[1], argv[2], &hints, &res); if (status != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); return 2; } int sockfd; for (p = res; p != NULL; p = p->ai_next) { sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); if (sockfd == -1) { perror("socket"); continue; } if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("connect"); continue; } break; } if (p == NULL) { fprintf(stderr, "failed to connect\n"); return 2; } freeaddrinfo(res); // 发送昵称 uint32_t nickname_size = htonl(strlen(nickname)); write(sockfd, &nickname_size, sizeof(nickname_size)); write(sockfd, nickname, strlen(nickname)); // 创建接收消息线程 pthread_t receive_thread; pthread_create(&receive_thread, NULL, receive_messages, &sockfd); char input[BUFFER_SIZE]; while (1) { char c = getchar(); if (c == 'm') { fgets(input, BUFFER_SIZE, stdin); input[strcspn(input, "\n")] = 0; // 去除换行符 uint32_t body_size = htonl(strlen(input)); write(sockfd, &body_size, sizeof(body_size)); write(sockfd, input, strlen(input)); } } close(sockfd); return 0; } // 修改处结束 ``` ### 服务器代码修改 ```cpp #include <stdio.h> #include <stdlib.h> #include <netdb.h> #include <netinet/in.h> #include <unistd.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> // 修改处开始 #define BUFFER_SIZE 1024 // 处理客户端线程函数 void *handle_client(void *arg) { int client_sockfd = *(int *)arg; uint32_t nickname_size_net; read(client_sockfd, &nickname_size_net, sizeof(nickname_size_net)); uint32_t nickname_size = ntohl(nickname_size_net); char *nickname = (char *)malloc(nickname_size + 1); read(client_sockfd, nickname, nickname_size); nickname[nickname_size] = '\0'; char buffer[BUFFER_SIZE]; while (1) { uint32_t body_size_net; ssize_t n = read(client_sockfd, &body_size_net, sizeof(body_size_net)); if (n <= 0) { break; } uint32_t body_size = ntohl(body_size_net); memset(buffer, 0, BUFFER_SIZE); read(client_sockfd, buffer, body_size); // 构造要发送给其他客户端的消息 char message[BUFFER_SIZE + nickname_size + 10]; sprintf(message, "[%s] %s", nickname, buffer); // 广播消息给所有客户端 // 这里需要一个全局的客户端列表,为了简化,暂不实现 // 实际中可以用链表或数组存储客户端套接字 } free(nickname); close(client_sockfd); return NULL; } int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "usage %s port\n", argv[0]); exit(0); } uint16_t portno = (uint16_t)atoi(argv[1]); struct addrinfo hints, *res, *p; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; // 支持 IPv4 和 IPv6 hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; int status = getaddrinfo(NULL, argv[1], &hints, &res); if (status != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); return 2; } int sockfd; for (p = res; p != NULL; p = p->ai_next) { sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); if (sockfd == -1) { perror("socket"); continue; } int yes = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("setsockopt"); continue; } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("bind"); continue; } break; } if (p == NULL) { fprintf(stderr, "failed to bind\n"); return 2; } freeaddrinfo(res); if (listen(sockfd, 5) == -1) { perror("listen"); return 1; } while (1) { struct sockaddr_storage their_addr; socklen_t addr_size = sizeof(their_addr); int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size); if (new_fd == -1) { perror("accept"); continue; } pthread_t client_thread; pthread_create(&client_thread, NULL, handle_client, &new_fd); pthread_detach(client_thread); } close(sockfd); return 0; } // 修改处结束 ``` ### Makefile 示例(仅作参考,不修改原 Makefile) ```makefile all: server client server: server.cpp g++ -std=c++11 server.cpp -o server -pthread client: client.cpp g++ -std=c++11 client.cpp -o client -pthread ``` ###
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值