diff options
Diffstat (limited to 'testcode/app_example/func-test')
-rw-r--r-- | testcode/app_example/func-test/CMakeLists.txt | 18 | ||||
-rw-r--r-- | testcode/app_example/func-test/file_transfer/CMakeLists.txt | 30 | ||||
-rw-r--r-- | testcode/app_example/func-test/file_transfer/client_filetrans.c | 276 | ||||
-rw-r--r-- | testcode/app_example/func-test/file_transfer/server_filetrans.c | 336 | ||||
-rw-r--r-- | testcode/app_example/func-test/fork/CMakeLists.txt | 30 | ||||
-rw-r--r-- | testcode/app_example/func-test/fork/tcpclient.c | 170 | ||||
-rw-r--r-- | testcode/app_example/func-test/fork/tcpserver.c | 156 |
7 files changed, 1016 insertions, 0 deletions
diff --git a/testcode/app_example/func-test/CMakeLists.txt b/testcode/app_example/func-test/CMakeLists.txt new file mode 100644 index 0000000..865eba5 --- /dev/null +++ b/testcode/app_example/func-test/CMakeLists.txt @@ -0,0 +1,18 @@ +######################################################################### +# +# Copyright (c) 2018 Huawei Technologies Co.,Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +######################################################################### + +ADD_SUBDIRECTORY(fork) +ADD_SUBDIRECTORY(file_transfer) diff --git a/testcode/app_example/func-test/file_transfer/CMakeLists.txt b/testcode/app_example/func-test/file_transfer/CMakeLists.txt new file mode 100644 index 0000000..e4f653a --- /dev/null +++ b/testcode/app_example/func-test/file_transfer/CMakeLists.txt @@ -0,0 +1,30 @@ +######################################################################### +# +# Copyright (c) 2018 Huawei Technologies Co.,Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +######################################################################### +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -g -fPIC -m64 -mssse3 -std=gnu89") + +LINK_DIRECTORIES(${LIB_PATH_SHARED} ${LIB_PATH_STATIC}) + +ADD_EXECUTABLE(vc_serv_file server_filetrans.c) +TARGET_LINK_LIBRARIES(vc_serv_file libnStackAPI.so -lpthread -lrt) + +ADD_EXECUTABLE(vc_cli_file client_filetrans.c) +TARGET_LINK_LIBRARIES(vc_cli_file libnStackAPI.so -lpthread -lrt) + +ADD_EXECUTABLE(kc_serv_file server_filetrans.c) +TARGET_LINK_LIBRARIES(kc_serv_file pthread) + +ADD_EXECUTABLE(kc_cli_file client_filetrans.c) +TARGET_LINK_LIBRARIES(kc_cli_file pthread) diff --git a/testcode/app_example/func-test/file_transfer/client_filetrans.c b/testcode/app_example/func-test/file_transfer/client_filetrans.c new file mode 100644 index 0000000..f0e2a80 --- /dev/null +++ b/testcode/app_example/func-test/file_transfer/client_filetrans.c @@ -0,0 +1,276 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/types.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <fcntl.h> +#include <netdb.h> +#include <ctype.h> +char *END_FLAG = "=================END"; +#define HEXCONVERT_COLS 8 +#define HEX_CONVERT 1 +//#define DEBUG 1 +#define out(fmt, arg...) (void)printf(fmt, ##arg) + +#ifdef DEBUG +#define DBG(fmt, arg...) do { \ + out("[Debug] " fmt, ##arg); \ + } while (0) +#else +#define DBG(fmt, arg...) ((void)0) +#endif + +void error(const char *msg) +{ + perror(msg); + out("./client_tcp [server_ip_address] [port number] [filename] [client_ip_address]\n"); + exit(1); +} + +#if defined(HEX_CONVERT) && defined(DEBUG) +void hexconvert(void *mem, unsigned int len) +{ + unsigned int i; + + for (i = 0; + i < + len + + ((len % HEXCONVERT_COLS) ? (HEXCONVERT_COLS - + len % HEXCONVERT_COLS) : 0); i++) + { + /* print offset */ + if (i % HEXCONVERT_COLS == 0) + { + DBG("\n0x%06x: ", i); + } + + /*print hex data */ + if (i < len) + { + DBG("%02x ", 0xFF & ((char *) mem)[i]); + } + else /* end of block, just aligning for ASCII dump */ + { + DBG("\n"); + } + } +} +#endif + +void tcp(char **pArgv) +{ + + int sockfd, portno; + char buff[1024]; + + struct sockaddr_in serv_addr, cli_addr; + struct hostent *server; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) + { + error("error in socket creation\n"); + } + out("socket create successful\n"); + + portno = atoi(pArgv[2]); + server = gethostbyname(pArgv[1]); + + if (server == NULL) + { + fprintf(stderr, "error no such host\n"); + } + + bzero((char *) &serv_addr, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + bcopy((char *) server->h_addr, (char *) &serv_addr.sin_addr, + server->h_length); + serv_addr.sin_port = htons(portno); + bzero((char *) &cli_addr, sizeof(serv_addr)); + + cli_addr.sin_family = AF_INET; + cli_addr.sin_addr.s_addr = inet_addr(pArgv[4]); + cli_addr.sin_port = htons(portno); + if (bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) + { + error("bind fail"); + } + out("Bind successful\n"); + + if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < + 0) + { + error("connection fail"); + } + out("connection done\n"); + + FILE *file; + int filebyte = 0; + int lsize, totalsize = 0; + + file = fopen(pArgv[3], "r"); + fseek(file, 0, SEEK_END); + lsize = ftell(file); + rewind(file); + out("Name of file: %s, Size of file : %d\n", pArgv[3], lsize); + if (write(sockfd, &lsize, sizeof(int)) == -1) + { + out("error executing write\n"); + } + if (write(sockfd, pArgv[3], 255) == -1) + { + out("error executing write\n"); + } + while (lsize > totalsize) + { + bzero(buff, 1024); + fseek(file, totalsize, SEEK_SET); + filebyte = fread(buff, 1, sizeof(buff), file); + if (filebyte == 0) + { + printf("file End of file\n"); + break; + } + + if (filebyte < 0) + error("error in reading file. \n"); +#if defined(HEX_CONVERT) && defined(DEBUG) + DBG("=========================================\n"); + hexconvert(buff, filebyte); + DBG("=========================================\n"); +#endif + + void *p = buff; + totalsize += filebyte; + + while (filebyte > 0) + { +#ifdef DEBUG + DBG("=========================================\n"); + puts((const char *) p); + DBG("=========================================\n"); +#endif + int bytes_written = write(sockfd, p, filebyte); + if (bytes_written <= 0) + { + error("error in Socket write.\n"); + } + + filebyte -= bytes_written; + p += bytes_written; +//#if DEBUG + DBG("Total size of file = %d, Total Bytes sent to socket = %d, bytes_written in each step = %d\n", lsize, totalsize, bytes_written); +//#endif + } + } + out("file has been sent successfully\n"); + out("Final Total size of file = %d, Total Bytes sent to socket = %d\n", + lsize, totalsize); + + fclose(file); + sleep(60); + close(sockfd); + return; +} + +void udp(char **pArgv) +{ + int sockfd, n, fd, sz, portno, MAXLINE; + FILE *fp; + struct sockaddr_in servaddr, cliaddr; + char *buf; + char *target, *path; + portno = atoi(pArgv[2]); + bzero(&servaddr, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(portno); + servaddr.sin_addr.s_addr = inet_addr(pArgv[1]); + bzero(&cliaddr, sizeof(servaddr)); + cliaddr.sin_family = AF_INET; + cliaddr.sin_port = htons(portno); + cliaddr.sin_addr.s_addr = inet_addr(pArgv[3]); + sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) + { + out("error in socket creation\n"); + } + out("socket create successful\n"); + + if (bind(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr)) < 0) + { + out("bind fail"); + } + out("Bind successful\n"); + + path = pArgv[4]; + target = pArgv[5]; + MAXLINE = atoi(pArgv[6]); + buf = malloc(MAXLINE * sizeof(int)); + fp = fopen(path, "r"); + fseek(fp, 0L, SEEK_END); + sz = ftell(fp); + out("The size of the path file is %d", sz); + sendto(sockfd, target, strlen(target), 0, + (struct sockaddr *) &servaddr, sizeof(servaddr)); + n = recvfrom(sockfd, buf, MAXLINE, 0, NULL, NULL); + if (!strncmp(buf, "ok", 2)) + { + out("Filename sent.\n"); + } + + fd = open(path, O_RDONLY); + while ((n = read(fd, buf, MAXLINE)) > 0) + { + sendto(sockfd, buf, n, 0, (struct sockaddr *) &servaddr, + sizeof(servaddr)); + } + sendto(sockfd, END_FLAG, strlen(END_FLAG), 0, + (struct sockaddr *) &servaddr, sizeof(servaddr)); + fclose(fp); + sleep(60); + close(sockfd); + return; +} + +int main(int argc, char *argv[]) +{ + int i; + char **pArgv, str[10]; + pArgv = (char **) malloc(sizeof(char *) * 10); + for (i = 0; i < 10; i++) + { + pArgv[i] = (char *) malloc(sizeof(char) * 20); + } + printf("%s", argv[1]); + + if (strcmp("tcp", argv[1]) == 0) + { + strcpy(pArgv[0], "tcp"); + printf("pArgv[0]=%s", pArgv[0]); + /* The arguments of tcp are [server_ip_address] [port number] [filename] [client_ip_address] */ + for (i = 1; i < 5; i++) + { + strcpy(pArgv[i], argv[i + 1]); + } + tcp(pArgv); + } + + else + { + strcpy(str, argv[1]); + if (strcmp("udp", str) == 0) + { + strcpy(pArgv[0], "udp"); + printf("pArgv[0]=%s", pArgv[0]); + /* The arguments of udp are [server_ip_address] [port number] [client_ip_address] [filename] [target_filename] [MAX_BUFFER_LENGTH] */ + for (i = 1; i < 7; i++) + { + strcpy(pArgv[i], argv[i + 1]); + } + udp(pArgv); + } + } + return 0; +} diff --git a/testcode/app_example/func-test/file_transfer/server_filetrans.c b/testcode/app_example/func-test/file_transfer/server_filetrans.c new file mode 100644 index 0000000..8b7e25f --- /dev/null +++ b/testcode/app_example/func-test/file_transfer/server_filetrans.c @@ -0,0 +1,336 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/types.h> +#include <netinet/in.h> +#include <netdb.h> +#include <ctype.h> +#include <arpa/inet.h> +#include <fcntl.h> +char *END_FLAG = "=================END"; +#define HEXCONVERT_COLS 8 +#define HEX_CONVERT 1 +//#define DEBUG 1 +#define out(fmt, arg...) (void)printf(fmt, ##arg) + +#ifdef DEBUG +#define DBG(fmt, arg...) do { \ + out("[Debug] " fmt, ##arg); \ + } while (0) +#else +#define DBG(fmt, arg...) ((void)0) +#endif + +void error(const char *msg) +{ + perror(msg); + out("./server_tcp [server_ip_address] [port number]\n"); + exit(1); +} + +#if defined(HEX_CONVERT) && defined(DEBUG) +void hexconvert(void *mem, unsigned int len) +{ + unsigned int i; + + for (i = 0; + i < + len + + ((len % HEXCONVERT_COLS) ? (HEXCONVERT_COLS - + len % HEXCONVERT_COLS) : 0); i++) + { + /* print offset */ + if (i % HEXCONVERT_COLS == 0) + { + DBG("\n0x%06x: ", i); + } + + /*print hex data */ + if (i < len) + { + DBG("%02x ", 0xFF & ((char *) mem)[i]); + } + else /* end of block, just aligning for ASCII dump */ + { + DBG("\n"); + } + } +} +#endif +int compareFiles(FILE * fp1, FILE * fp2) +{ + + char ch1 = getc(fp1); + char ch2 = getc(fp2); + int error = 0, pos = 0, line = 1; + + while (ch1 != EOF && ch2 != EOF) + { + pos++; + if (ch1 == '\n' && ch2 == '\n') + { + line++; + pos = 0; + } + + if (ch1 != ch2) + { + error++; + DBG("Line Number : %d \tError" " Position :%d \n", line, pos); + } + + ch1 = getc(fp1); + ch2 = getc(fp2); + } + + //printf("Total Errors : %d\t", error); + return error; +} + +void tcp(char **pArgv) +{ + + int sockfd, newsockfd, portno; + char buff[1024], filename[255]; + + struct sockaddr_in serv_addr, cli_addr; + socklen_t clilen; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) + { + error("error in socket creation"); + } + out("socket create successful\n"); + + bzero((char *) &serv_addr, sizeof(serv_addr)); + portno = atoi(pArgv[2]); + + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = inet_addr(pArgv[1]); + serv_addr.sin_port = htons(portno); + + if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) + { + error("bind fail"); + } + out("Bind successful\n"); + listen(sockfd, 5); + clilen = sizeof(cli_addr); + + newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); + if (newsockfd < 0) + { + error("error in accept"); + } + out("socket accept succesful\n"); + bzero(buff, 1024); + + FILE *fp; + int lSize = 0, totallSize = 0; + + bzero(filename, 255); + + fclose(fopen("receive_file.txt", "w")); + if (system("chmod +x *") == -1) + { + out(" incorrect use of system\n"); + } + fp = fopen("receive_file.txt", "a"); + + if (read(newsockfd, &lSize, sizeof(int)) == -1) + { + out("error executing read\n"); + } + if (read(newsockfd, filename, sizeof(filename)) == -1) + { + out("error executing read\n"); + } + + while (lSize > totallSize) + { + int bytes_read = 0; + bzero(buff, 1024); + + bytes_read = read(newsockfd, buff, 1024); + + if (bytes_read == 0) + { + break; + } + + if (bytes_read < 0) + { + error("error in Socket read.\n"); + } + +#if defined(HEX_CONVERT) && defined(DEBUG) + DBG("=========================================\n"); + hexconvert(buff, bytes_read); + DBG("=========================================\n"); +#endif +#ifdef DEBUG + DBG("=========================================\n"); + puts((const char *) buff); + DBG("=========================================\n"); +#endif + totallSize += bytes_read; + + if (fwrite(buff, 1, bytes_read, fp) == -1) + { + error("error in file write\n"); + } +//#if DEBUG + DBG("Total size of file = %d, Total Bytes sent to socket = %d, bytes_read in each step = %d\n", lSize, totallSize, bytes_read); +//#endif + } + out("file name = %s\n", filename); + out("Final total size of file = %d, total read from socket = %d\n", lSize, + totallSize); + out("copy complete\n"); + fclose(fp); + + FILE *fp1 = fopen("receive_file.txt", "r"); + FILE *fp2 = fopen(filename, "r"); + + fseek(fp2, 0L, SEEK_END); + int lfile_size = ftell(fp2); + rewind(fp2); + if (lfile_size != lSize) + { + out("Size unmatch...\n"); + } + else + { + out("Size match...\n"); + } + + if (compareFiles(fp1, fp2) > 0) + { + out("file unmatch...\n"); + } + else + { + out("file match...\n"); + } + + close(newsockfd); + close(sockfd); + return; +} + +void +run(int sockfd, struct sockaddr *cliaddr, socklen_t clilen, char *res_buf, + int MAXLINE) +{ + int n, fd; + socklen_t len; + char *buf, *buf2; + FILE *fp1, *fp2; + buf = malloc(MAXLINE + 1); + len = clilen; + n = recvfrom(sockfd, buf, MAXLINE, 0, cliaddr, &len); + buf[n] = 0; + out("Received from client:[%s] \n", buf); + buf2 = malloc(MAXLINE); + strcpy(buf2, buf); + sendto(sockfd, "ok", strlen("ok"), 0, cliaddr, len); + fd = open(buf, O_RDWR | O_CREAT, 0666); + while ((n = recvfrom(sockfd, buf, MAXLINE, 0, cliaddr, &len))) + { + buf[n] = 0; + //out("%s", buf); + if (!(strcmp(buf, END_FLAG))) + { + break; + } + if (write(fd, buf, n) == -1) + { + out("error in executing write\n"); + } + } + fp1 = fopen(buf2, "r"); + fp2 = fopen(res_buf, "r"); + + if (compareFiles(fp1, fp2) == 0) + { + out("\nPass:The contents of the files are same"); + } + else + { + out("\nFail:The contents of the files are different"); + } + close(fd); +} + +void udp(char **pArgv) +{ + int sockfd, portno, MAXLINE; + struct sockaddr_in servaddr, cliaddr; + char *res_buf; + res_buf = pArgv[3]; + + portno = atoi(pArgv[2]); + MAXLINE = atoi(pArgv[4]); + sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) + { + out("error in socket creation\n"); + } + out("socket create successful\n"); + + bzero(&servaddr, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = inet_addr(pArgv[1]); + servaddr.sin_port = htons(portno); + + if (bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) + { + out("bind fail"); + } + out("Binded successfully\n"); + fd_set read_fds; + FD_ZERO(&read_fds); + FD_SET(sockfd, &read_fds); + int fdmax = sockfd; + if (FD_ISSET(sockfd, &read_fds)) + { + run(fdmax, (struct sockaddr *) &cliaddr, sizeof(cliaddr), res_buf, + MAXLINE); + } + return; +} + +int main(int argc, char *argv[]) +{ + int i, j; + char **pArgv; + pArgv = (char **) malloc(sizeof(char *) * 10); + for (i = 0; i < 10; i++) + { + pArgv[i] = (char *) malloc(sizeof(char) * 20); + } + if (strcmp("tcp", argv[1]) == 0) + { + strcpy(pArgv[0], "tcp"); + /* The arguments of tcp are [server_ip_address] [port number] */ + for (i = 1; i < 3; i++) + { + strcpy(pArgv[i], argv[i + 1]); + } + tcp(pArgv); + } + else if (strcmp("udp", argv[1]) == 0) + { + strcpy(pArgv[0], "udp"); + /* The arguments of udp are [server_ip_address] [port number] [filename] [MAX_BUFFER_LENGTH] */ + for (i = 1; i < 5; i++) + { + strcpy(pArgv[i], argv[i + 1]); + } + udp(pArgv); + } + + return 0; +} diff --git a/testcode/app_example/func-test/fork/CMakeLists.txt b/testcode/app_example/func-test/fork/CMakeLists.txt new file mode 100644 index 0000000..a78f617 --- /dev/null +++ b/testcode/app_example/func-test/fork/CMakeLists.txt @@ -0,0 +1,30 @@ +######################################################################### +# +# Copyright (c) 2018 Huawei Technologies Co.,Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +######################################################################### +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -g -fPIC -m64 -mssse3 -std=gnu89") + +LINK_DIRECTORIES(${LIB_PATH_SHARED} ${LIB_PATH_STATIC}) + +ADD_EXECUTABLE(vtcp_fork_server tcpserver.c) +TARGET_LINK_LIBRARIES(vtcp_fork_server libnStackAPI.so -lpthread -lrt) + +ADD_EXECUTABLE(vtcp_client tcpclient.c) +TARGET_LINK_LIBRARIES(vtcp_client libnStackAPI.so -lpthread -lrt) + +ADD_EXECUTABLE(ktcp_fork_server tcpserver.c) +TARGET_LINK_LIBRARIES(ktcp_fork_server pthread) + +ADD_EXECUTABLE(ktcp_client tcpclient.c) +TARGET_LINK_LIBRARIES(ktcp_client pthread) diff --git a/testcode/app_example/func-test/fork/tcpclient.c b/testcode/app_example/func-test/fork/tcpclient.c new file mode 100644 index 0000000..8023b34 --- /dev/null +++ b/testcode/app_example/func-test/fork/tcpclient.c @@ -0,0 +1,170 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <errno.h> + +#define DEBUG + +#ifdef DEBUG +#define DBG(fmt, arg...) do { \ + printf(fmt, ##arg); \ +} while(0) +#else +#define DBG(fmt, arg...) ((void)0) +#endif + +static struct sockaddr_in g_dest; +static struct sockaddr_in g_src; +int srcPort = 0; +int destPort = 0; +int times = 0; + +void random_str(char *str, const int len) +{ + static const char alphaNum[] = + "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789"; + int i = 0; + + for (i = 0; i < len; i++) + { + str[i] = alphaNum[rand() % (sizeof(alphaNum) - 1)]; + } + + str[len] = 0; +} + +static void setArgsDefault() +{ + + memset(&g_dest, 0, sizeof(g_dest)); + g_dest.sin_family = AF_INET; + g_dest.sin_addr.s_addr = inet_addr("127.0.0.1"); + g_dest.sin_port = htons(12345); + bzero(&(g_dest.sin_zero), 8); + + memset(&g_src, 0, sizeof(g_src)); + g_src.sin_family = AF_INET; + g_src.sin_addr.s_addr = inet_addr("0.0.0.0"); + g_src.sin_port = htons(7895); + bzero(&(g_src.sin_zero), 8); + + times = 1; +} + +static int process_arg(int argc, char *argv[]) +{ + int opt = 0; + int error = 0; + const char *optstring = "p:d:s:a:t:"; + + if (argc < 5) + { + DBG("Param info :-p dest_portid; -d dest_serverIP; -a src_portid; -s src_clientIP; -t times; \n"); + return -1; + } + setArgsDefault(); + while ((opt = getopt(argc, argv, optstring)) != -1) + { + switch (opt) + { + case 'p': + g_dest.sin_port = htons(atoi(optarg)); + break; + case 'd': + g_dest.sin_addr.s_addr = inet_addr(optarg); + break; + case 's': + g_src.sin_addr.s_addr = inet_addr(optarg); + break; + case 'a': + g_src.sin_port = htons(atoi(optarg)); + break; + case 't': + times = atoi(optarg); + break; + } + } + return 0; +} + +int main(int argc, char *argv[]) +{ + + int clientSocket, ret, i; + char sndbuffer[1024]; + char rcvbuffer[1024]; + int result = 0; + + /* + * check command line arguments + */ + if (0 != process_arg(argc, argv)) + { + DBG("Error in argument.%d\n", argc); + exit(1); + } + + clientSocket = socket(AF_INET, SOCK_STREAM, 0); + if (clientSocket < 0) + { + DBG("Error in connection.\n"); + exit(1); + } + DBG("[INFO]Client Socket is created.\n"); + + ret = bind(clientSocket, (struct sockaddr *) &g_src, sizeof(g_src)); + if (ret < 0) + { + DBG("Error in binding.errno=%d\n", errno); + exit(1); + } + + DBG("[INFO]Bind to client aaddress port 0\n"); + + ret = connect(clientSocket, (struct sockaddr *) &g_dest, sizeof(g_dest)); + if (ret < 0) + { + DBG("Error in connection.\n"); + exit(1); + } + DBG("[INFO]Connected to Server.\n"); + + memset(sndbuffer, '\0', 1024 * sizeof(char)); + memset(rcvbuffer, '\0', 1024 * sizeof(char)); + + for (i = 1; i <= times; i++) + { + //DBG ("Client: \t"); + random_str(sndbuffer, 50); + send(clientSocket, sndbuffer, strlen(sndbuffer), 0); + + if (recv(clientSocket, rcvbuffer, 1024, 0) < 0) + { + DBG("Error in receiving data.\n"); + } + else + { + //DBG ("Server: \t%s\n", rcvbuffer); + } + if (0 != strcmp(sndbuffer, rcvbuffer)) + { + result = -1; + break; + } + } + + /* Send exit message to server */ + strcpy(sndbuffer, "#exit"); + send(clientSocket, sndbuffer, strlen(sndbuffer), 0); + + DBG("Result = %s\n", (result == 0) ? "Success" : "Fail"); + close(clientSocket); + DBG("Disconnecting from server.\n"); + return 0; +} diff --git a/testcode/app_example/func-test/fork/tcpserver.c b/testcode/app_example/func-test/fork/tcpserver.c new file mode 100644 index 0000000..af11832 --- /dev/null +++ b/testcode/app_example/func-test/fork/tcpserver.c @@ -0,0 +1,156 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <sys/wait.h> + +static struct sockaddr_in g_src; +int srcPort = 0; +int destPort = 0; +int times = 0; + +#ifdef DEBUG +#define DBG(fmt, arg...) do { \ + DBG(fmt, ##arg); \ +} while(0) +#else +#define DBG(fmt, arg...) ((void)0) +#endif + +static void setArgsDefault() +{ + memset(&g_src, 0, sizeof(g_src)); + g_src.sin_family = AF_INET; + g_src.sin_addr.s_addr = inet_addr("0.0.0.0"); + g_src.sin_port = htons(7895); + bzero(&(g_src.sin_zero), 8); + + times = 1; +} + +static int process_arg(int argc, char *argv[]) +{ + int opt = 0; + int error = 0; + const char *optstring = "s:a:t:"; + + if (argc < 5) + { + DBG("Param info :-p dest_portid; -d dest_serverIP; -a src_portid; -s src_clientIP; -t times; \n"); + return -1; + } + setArgsDefault(); + while ((opt = getopt(argc, argv, optstring)) != -1) + { + switch (opt) + { + case 's': + g_src.sin_addr.s_addr = inet_addr(optarg); + break; + case 'a': + g_src.sin_port = htons(atoi(optarg)); + break; + case 't': + times = atoi(optarg); + break; + } + } + return 0; +} + +int main(int argc, char *argv[]) +{ + + int sockfd, ret; + int newSocket; + socklen_t addr_size; + static struct sockaddr_in accept_addr; + char buffer[1024]; + pid_t childpid; + + /* + * check command line arguments + */ + if (0 != process_arg(argc, argv)) + { + DBG("Error in argument.%d\n", argc); + exit(1); + } + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) + { + DBG("Error in connection.\n"); + exit(1); + } + DBG("Server Socket is created. %d\n", sockfd); + + ret = bind(sockfd, (struct sockaddr *) &g_src, sizeof(g_src)); + if (ret < 0) + { + DBG("Error in binding.errno=%d\n", errno); + exit(1); + } + + DBG("Bind sucess port %d\n", g_src.sin_port); + + if (listen(sockfd, 10) == 0) + { + DBG("Listening on %s....\n", inet_ntoa(g_src.sin_addr)); + } + else + { + DBG("Error in binding.\n"); + } + + while (1) + { + newSocket = + accept(sockfd, (struct sockaddr *) &accept_addr, &addr_size); + if (newSocket < 0) + { + DBG("Error: Exiting here pid %d", getpid()); + exit(1); + } + + DBG("Connection accepted from %s:%d fd %d\n", + inet_ntoa(accept_addr.sin_addr), + ntohs(accept_addr.sin_port), newSocket); + if ((childpid = fork()) == 0) + { + DBG("[ PID %d] Child process Created. Pid %d \r\n", getpid(), + getpid()); + DBG("[ PID %d] Closing fd %d\n", getpid(), sockfd); + close(sockfd); + + while (1) + { + memset(buffer, '\0', 1024 * sizeof(char)); + recv(newSocket, buffer, 1024, 0); + if (strcmp(buffer, "#exit") == 0) + { + DBG("Disconnected from %s:%d\n", + inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port)); + break; + } + else + { + DBG("[PID %d]Client: %s\n", getpid(), buffer); + send(newSocket, buffer, strlen(buffer), 0); + bzero(buffer, sizeof(buffer)); + } + } + + DBG("[PID %d]Closing socket %d\r\n", getpid(), newSocket); + close(newSocket); + } + + } + + DBG("[PID %d]Process exiting... %d\r\n", getpid(), getpid()); + return 0; +} |