2
0

sockchild.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdlib.h>
  17. #include "testsock.h"
  18. #include "fspr_network_io.h"
  19. #include "fspr_pools.h"
  20. int main(int argc, char *argv[])
  21. {
  22. fspr_pool_t *p;
  23. fspr_socket_t *sock;
  24. fspr_status_t rv;
  25. fspr_sockaddr_t *remote_sa;
  26. fspr_initialize();
  27. atexit(fspr_terminate);
  28. fspr_pool_create(&p, NULL);
  29. if (argc < 2) {
  30. exit(-1);
  31. }
  32. rv = fspr_sockaddr_info_get(&remote_sa, "127.0.0.1", APR_UNSPEC, 8021, 0, p);
  33. if (rv != APR_SUCCESS) {
  34. exit(-1);
  35. }
  36. if (fspr_socket_create(&sock, remote_sa->family, SOCK_STREAM, 0,
  37. p) != APR_SUCCESS) {
  38. exit(-1);
  39. }
  40. rv = fspr_socket_timeout_set(sock, fspr_time_from_sec(3));
  41. if (rv) {
  42. exit(-1);
  43. }
  44. fspr_socket_connect(sock, remote_sa);
  45. if (!strcmp("read", argv[1])) {
  46. char datarecv[STRLEN];
  47. fspr_size_t length = STRLEN;
  48. fspr_status_t rv;
  49. memset(datarecv, 0, STRLEN);
  50. rv = fspr_socket_recv(sock, datarecv, &length);
  51. fspr_socket_close(sock);
  52. if (APR_STATUS_IS_TIMEUP(rv)) {
  53. exit(SOCKET_TIMEOUT);
  54. }
  55. if (strcmp(datarecv, DATASTR)) {
  56. exit(-1);
  57. }
  58. exit(length);
  59. }
  60. else if (!strcmp("write", argv[1])) {
  61. fspr_size_t length = strlen(DATASTR);
  62. fspr_socket_send(sock, DATASTR, &length);
  63. fspr_socket_close(sock);
  64. exit(length);
  65. }
  66. exit(-1);
  67. }