#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 char *ip; int net_clnt(int buf_size, int port) { int sndbuf_size, rcvbuf_size; struct sockaddr_in s_addr; char msg[128] = { 0 }; int optlen = 4; int sock; int rc; if (!port) port = SERV_PORT; sock = socket(NET_PROTOCAL, SOCK_STREAM, 0); if (buf_size) { sndbuf_size = rcvbuf_size = buf_size; /* set sndbuf and rcvbuf */ if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, sizeof(int))) { printf("set sndbuf failed\n"); return 0; } if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(int))) { printf("set rcvbuf failed\n"); return 0; } } memset(&s_addr, 0, sizeof(s_addr)); s_addr.sin_family = NET_PROTOCAL; if (ip) s_addr.sin_addr.s_addr = inet_addr(ip); else s_addr.sin_addr.s_addr = inet_addr(SERV_IP); s_addr.sin_port = htons(port); if (connect(sock, (struct sockaddr*)&s_addr, sizeof(s_addr))){ printf("connect fail\n"); return 0; } sndbuf_size = 0; rcvbuf_size = 0; getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, &optlen); getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, &optlen); printf("client: sndbuf_size %d, rcvbuf_size %d\n", sndbuf_size, rcvbuf_size); recv(sock, msg, sizeof(msg), 0); printf("get msg: %s\n", msg); send(sock, "Response", sizeof("Response"), MSG_NOSIGNAL); close(sock); } int main(int argc, char **argv){ bool wrong_param = false; int buf_size = 0, port = 0; int c; while(!wrong_param && (-1 != (c = getopt(argc, argv, "p:s:i:")))) { switch (c) { case 's': buf_size = atoi(optarg); break; case 'i': ip = strdup(optarg); break; case 'p': port = atoi(optarg); break; case '?': printf("usage: ./client -s -i -p \n"); wrong_param = true; break; } } if (!wrong_param) net_clnt(buf_size, port); return 0; }