Last active
April 2, 2019 17:13
-
-
Save nishidy/a1a8c829ff24439bbfce4bbd90de123f to your computer and use it in GitHub Desktop.
TCP_DEFER_ACCEPT test program for client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> //printf(), fprintf(), perror() | |
#include <sys/socket.h> //socket(), connect(), recv() | |
#include <arpa/inet.h> // struct sockaddr_in, struct sockaddr, inet_ntoa(), inet_aton() | |
#include <stdlib.h> //atoi(), exit(), EXIT_FAILURE, EXIT_SUCCESS | |
#include <string.h> //memset() | |
#include <unistd.h> //close(), sleep() | |
#include <linux/tcp.h> //TCP_DEFER_ACCEPT | |
#define MSGSIZE 32 | |
#define MAX_MSGSIZE 1024 | |
#define BUFSIZE (MSGSIZE + 1) | |
int main(int argc, char *argv[]){ | |
int clientSock; | |
struct sockaddr_in servSockAddr; | |
char sendBuffer[BUFSIZE]; | |
char recvBuffer[BUFSIZE]; | |
int byteRcvd; | |
int defer = 1; | |
if((clientSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){ | |
perror("socket() failed"); | |
exit(EXIT_FAILURE); | |
} | |
memset(&servSockAddr, 0, sizeof(servSockAddr)); | |
servSockAddr.sin_family = AF_INET; | |
servSockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
/* | |
* host = gethostbyname("DNS"); | |
* if(host != NULL){ | |
* servSockAddr.sin_addr.s_addr = *(unsigned int *)host->h_addr_list[0]; | |
* } | |
*/ | |
servSockAddr.sin_port = htons(8080); | |
setsockopt(clientSock, IPPROTO_TCP, TCP_DEFER_ACCEPT, &defer, sizeof(defer) ); | |
if(connect(clientSock, (struct sockaddr*) &servSockAddr, sizeof(servSockAddr)) < 0){ | |
perror("connect() failed."); | |
exit(EXIT_FAILURE); | |
} | |
printf("connect to %s\n", inet_ntoa(servSockAddr.sin_addr)); | |
sleep(1); | |
sprintf(sendBuffer, "%s", "GET / HTTP/1.1\r\nHost: localhost"); | |
if(send(clientSock, sendBuffer, MSGSIZE, 0) <= 0){ | |
printf("%s\n",sendBuffer); | |
} | |
if(recv(clientSock, recvBuffer, MSGSIZE, 0) < 0){ | |
printf("recv() failed."); | |
exit(EXIT_FAILURE); | |
} | |
printf("received: %s\n",recvBuffer); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I remove sleep(1) right after connect(), we can find that the first data packet is sent with the final ACK. (piggybacking)