#include #include #include #include #include #include #include #include #include #include #ifndef AF_SMC #define AF_SMC 43 #endif #define NET_PROTOCAL AF_INET #define SERV_IP "11.213.5.33" #define SERV_PORT 10012 #define BUF_SIZE (5 * 128 * 1024) int stream_send(int fd, char *buf, int msgsize) { int n = msgsize; while (n) { int i = write(fd, buf, n); if (i < 0) return i; buf += i; n -= i; if (i == 0) break; printf("Successfully send %d B message\n", i); } return msgsize-n; } int net_clnt(char *ip, int port, int msgsize, int msgcnt) { int count = 0, send; char c; if (!ip) ip = SERV_IP; if (!port) port = SERV_PORT; int sock = socket(NET_PROTOCAL, SOCK_STREAM, 0); struct sockaddr_in s_addr; memset(&s_addr, 0, sizeof(s_addr)); s_addr.sin_family = NET_PROTOCAL; s_addr.sin_addr.s_addr = inet_addr(ip); s_addr.sin_port = htons(port); if (connect(sock, (struct sockaddr*)&s_addr, sizeof(s_addr))){ printf("connect fail\n"); return 0; } char *buf = (char *)malloc(sizeof(char) * BUF_SIZE); while (--msgcnt) { if (msgsize > BUF_SIZE) break; printf("Send msgsize: %d\n", msgsize); send = stream_send(sock, buf, msgsize); if (send <= 0) { printf("Error send %d\n", send); break; } } close(sock); } int main(int argc, char **argv){ int msgsize = BUF_SIZE, msgcnt = 10; char *ip = NULL; bool wrong_param = false; int port = 0; int c; while(!wrong_param && (-1 != (c = getopt(argc, argv, "i:p:m:c:")))) { switch (c) { case 'i': ip = optarg; break; case 'p': port = atoi(optarg); break; case 'm': msgsize = atoi(optarg); break; case 'c': msgcnt = atoi(optarg); break; case '?': printf("usage: ./client -i -p -m -c \n"); wrong_param = true; break; } } if (!wrong_param) net_clnt(ip, port, msgsize, msgcnt); return 0; }