📄 t_read.c
字号:
/* * Copyright (C) 1999-2003 MITSUBISHI ELECTRIC CORPORATION and * RENESAS SOLUTIONS CORPORATION and * RENESAS TECHNOLOGY CORPORATION * All rights reserved. * * TCP/IP Test Application program [API]. */#include <tk/tkernel.h>#include <renesas_tcpip.h>#include <renesas_tcpip_common.h>#include "config.h"voidtest_task(int stacd, void *exinf){ int i, s, s2, ret; int len, flags; struct sockaddr_in name, addr; int addrlen, namelen; char buf[2048]; int num, nonblock; int testno, testno2, result[8]; num = 1; testno = 0; memset((char *)result, 0, sizeof(result)); /* for setup: get normal socket */ errno = 0; if ((s = unix_socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("socket(AF_INET,SOCK_STREAM,0) fail\n"); goto testend; } memset((char *)&name, 0, sizeof(name)); name.sin_family = AF_INET; name.sin_addr.s_addr = inet_addr(TGT_ADDR); name.sin_port = htons(TGT_PORT); namelen = sizeof(name); if ((ret = unix_bind(s, (struct sockaddr *)&name, namelen)) < 0) { printf("bind(s,&name,namelen) fail\n"); unix_close(s); goto testend; } if ((ret = unix_listen(s, 5)) < 0) { printf("listen(s,5) fail\n"); unix_close(s); goto testend; } /* opt=SO_REUSEADDR: enable local address reuse */ flags = 1; if ((ret = unix_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags))) < 0) { printf("setsockopt fail\n"); unix_close(s); goto testend; } /* Test 1 */ errno = 0;testno = 1; ret = unix_read(s, buf, sizeof(buf)); if (ret < 0) { printf("OK! :read(s(no connection), buf, len), ENOTCONN,errno = %d\n",errno); result[testno] = 0; } else { printf("Fail! :read(s(no connection), buf, len), ENOTCONN,errno = %d\n",errno); result[testno] = 1; } errno = 0; memset((char *)&addr, 0, sizeof(addr)); addrlen = sizeof(addr); if ((s2 = unix_accept(s, (struct sockaddr *)&addr, &addrlen)) < 0) { printf("accept(s,addr,addrlen) fail\n"); unix_close(s); goto testend; } /* Test 2 */ printf("read Test %d: \n",num++); errno = 0; nonblock = 1; ret = unix_ioctl(s2, FIONBIO, (VP)&nonblock); if (ret < 0) { unix_close(s2); unix_close(s); goto testend; } errno = 0;testno = 2; ret = unix_read(s2, buf, sizeof(buf)); if (ret < 0) { printf("OK! :read(s(no delay), buf, len), EWOULDBLOCK, errno = %d \n",errno); result[testno] = 0; } else { printf("Fail! :read(s(no delay), buf, len), EWOULDBLOCK, errno = %d \n",errno); result[testno] = 1; } nonblock = 0; ret = unix_ioctl(s2, FIONBIO, (VP)&nonblock); if (ret < 0) { unix_close(s2); unix_close(s); goto testend; } /* Test 3 */ printf("read Test %d: \n",num++); errno = 0;testno = 3; ret = unix_read(-1, buf, sizeof(buf)); if (ret < 0) { printf("OK! :read(-1, buf, len), EBADF, errno = %d\n",errno); result[testno] = 0; } else { printf("Fail! :read(-1, buf, len), EBADF, errno = %d\n",errno); result[testno] = 1; } /* Test 4 */ printf("read Test %d: \n",num++); errno = 0;testno = 4; ret = unix_read(10, buf, sizeof(buf)); if (ret < 0) { printf("OK! :read(10, buf, len), EBADF, errno = %d\n",errno); result[testno] = 0; } else { printf("Fail! :read(10, buf, len), EBADF, errno = %d\n",errno); result[testno] = 1; } /* Test 5 */ printf("read Test %d: \n",num++); errno = 0;testno = 5; ret = unix_read(s2, (char *)NULL, sizeof(buf)); if (ret < 0) { printf("OK! :read(s, NULL, len), EFAULT, errno = %d\n",errno); result[testno] = 0; } else { printf("Fail! :read(s, NULL, len), EFAULT, errno = %d\n",errno); result[testno] = 1; } /* Test 6 */ printf("read Test %d: \n",num++); errno = 0;testno = 6; ret = unix_read(s2, (char *)-1, sizeof(buf)); if (ret < 0) { printf("OK! :read(s, -1, len), EFAULT, errno = %d\n",errno); result[testno] = 0; } else { printf("Fail! :read(s, -1, len), EFAULT, errno = %d\n",errno); result[testno] = 1; } /* Test 7 */ printf("read Test %d: \n",num++); errno = 0;testno = 7; while ((ret = unix_read(s2, buf, sizeof(buf))) > 0) { printf("\t get data %d\n",ret); } if (ret < 0) { result[testno] = 1; } else { result[testno] = 0; } unix_shutdown(s2, SHUT_RDWR); unix_shutdown(s, SHUT_RDWR); unix_close(s2); unix_close(s);#if defined(T_KERNEL) tk_slp_tsk(2000);#endiftestend: /* check the results */ testno2 = 7; for (i=1; i <= testno; i++) { if (result[i] == 1) break; } if (i > testno2) printf("Pass.\n"); else printf("Error.\n");#if defined(T_KERNEL) tk_slp_tsk(TMO_FEVR);#endif}/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -