📄 t_sockopt_lingr.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"#define BIG_SIZE (16*1024)voidtest_task(int stacd, void *exinf){ int i, s, s2, ret; int len, flags; struct sockaddr_in name, addr; int addrlen, namelen; char buf[BIG_SIZE]; int num; struct linger lingr; int testno, testno2, result[4]; 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 errno=%d\n",errno); goto testend; } /* Test 1 */ printf("setsockopt Test %d: \n",num++); /* opt=SO_LINGER: linger on closed if data present */ lingr.l_onoff = 0; /* on!=0, off=0 */ lingr.l_linger = 0; /* seconds */testno = 1; if ((ret = unix_setsockopt(s, SOL_SOCKET, SO_LINGER, (char *)&lingr, sizeof(lingr))) < 0) { result[testno] = 1; printf("setsockopt fail (linger = off)\n"); unix_close(s); goto testend; } else { result[testno] = 0; printf("setsockopt pass (linger = off)\n"); } memset((char *)&name, 0, sizeof(name)); name.sin_family = AF_INET; name.sin_addr.s_addr = inet_addr(SUN_ADDR); name.sin_port = htons(SUN_PORT); namelen = sizeof(name); errno = 0; if ((ret = unix_connect(s, (struct sockaddr *)&name, namelen)) < 0) { printf("connect(s,&name,namelen) fail\n"); unix_close(s); goto testend; } ret = unix_send(s, buf, BIG_SIZE, 0); if (ret < 0) { printf("Fail! :send data, errno=%d\n",errno); unix_close(s); goto testend; } else { printf("OK! :send data, size=%d\n",ret); } errno = 0; ret = unix_shutdown(s, SHUT_RDWR); if (ret == 0) { printf("OK! :shutdown(s, 0),ret=%d\n",ret); } else { printf("Fail! :shutdown(s, 0),ret=%d\n",ret); unix_close(s); goto testend; } errno = 0; ret = unix_close(s); if (ret == 0) { printf("OK! :close(s),ret=%d\n",ret); } else { printf("Fail! :close(s),ret=%d\n",ret); }#if defined(T_KERNEL) tk_slp_tsk(2000);#endif errno = 0; if ((s = unix_socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("socket(AF_INET,SOCK_STREAM,0) fail errno=%d\n",errno); goto testend; } /* Test 2 */ printf("setsockopt Test %d: \n",num++); /* opt=SO_LINGER: linger on closed if data present */ lingr.l_onoff = 1; /* on!=0, off=0 */ lingr.l_linger = 2; /* seconds */testno = 2; if ((ret = unix_setsockopt(s, SOL_SOCKET, SO_LINGER, (char *)&lingr, sizeof(lingr))) < 0) { result[testno] = 1; printf("setsockopt fail (linger = off)\n"); unix_close(s); goto testend; } else { result[testno] = 0; printf("setsockopt pass (linger = on)\n"); } memset((char *)&name, 0, sizeof(name)); name.sin_family = AF_INET; name.sin_addr.s_addr = inet_addr(SUN_ADDR); name.sin_port = htons(SUN_PORT +1); namelen = sizeof(name); errno = 0; if ((ret = unix_connect(s, (struct sockaddr *)&name, namelen)) < 0) { printf("connect(s,&name,namelen) fail\n"); unix_close(s); goto testend; } ret = unix_send(s, buf, BIG_SIZE, 0); if (ret < 0) { printf("Fail! :send data, errno=%d\n",errno); unix_close(s); goto testend; } else { printf("OK! :send data, size=%d\n",ret); } errno = 0; ret = unix_shutdown(s, SHUT_RDWR); if (ret == 0) { printf("OK! :shutdown(s, 0),ret=%d\n",ret); } else { printf("Fail! :shutdown(s, 0),ret=%d\n",ret); unix_close(s); goto testend; } errno = 0; ret = unix_close(s); if (ret == 0) { printf("OK! :close(s),ret=%d\n",ret); } else { printf("Fail! :close(s),ret=%d\n",ret); }#if defined(T_KERNEL) tk_slp_tsk(2000);#endiftestend: /* check the results */ testno2 = 2; 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 + -