3 #include <sys/socket.h>
4 #include <netinet/in.h>
13 #define LARGE_BUFFER_SIZE 2048
17 * Pings the server at argv1: argv2
18 * gets a response and prints it
20 * tcp_test server_port
21 * tcp_test server_addr server_port
24 int main(int argc, char* argv[]) {
25 struct sockaddr_in server;
26 char buf[BUF_SIZE] = "hello world";
27 char bulkdata[LARGE_BUFFER_SIZE] = "testme";
28 char recv_buf[BUF_SIZE];
29 int sockfd, n, inqemu;
32 // ignore the host for now
34 printf("in qemu client\n");
38 printf("linux client\n");
42 printf("incorrect number of parameters\n");
45 host = gethostbyname(argv[1]); //hostname
47 bzero(&server, sizeof(server));
48 server.sin_family = AF_INET;
50 server.sin_port = htons(atoi(argv[1]));
52 server.sin_port = htons(atoi(argv[2]));
56 server.sin_addr.s_addr = inet_addr("10.0.0.1"); //hardcoded server
58 // linux client, use the argument as the server address
59 memcpy(&server.sin_addr.s_addr, host->h_addr, host->h_length);
61 char* printbuf = (char*)&server.sin_addr.s_addr;
62 int size = sizeof(server.sin_addr.s_addr);
64 for (i=0; i<size;i++) {
65 printf("connecting to %x \n", ((char*)printbuf)[i]);
68 sockfd = socket(AF_INET, SOCK_STREAM, 0);
70 printf("socket error\n");
74 printf ("tcp_test: sockfd %d \n", sockfd);
75 int socklen = sizeof(server);
77 // Set up a connection with the server
80 if((rc = connect(sockfd, (struct sockaddr *)&server, sizeof(server))) < 0)
82 // testing closing a socket file
86 // sending large chunk of data of 2K, more than one frame
87 // int sendsize = sendto(sockfd, bulkdata, LARGE_BUFFER_SIZE, 0, (struct sockaddr*) &server, socklen);
90 int sendsize = send(sockfd, buf, strlen(buf), 0);
91 if (sendsize != strlen(buf))
92 printf("send operation failed error code %d \n", sendsize);
96 strcpy(recv_buf, "DEADBEEFDEADBEE");
97 // in udp_test, we can recv_from with a size of 5, because it discards the rest of the packet. In the TCP
98 // version, we need to be precise about the size of the recv, because the left over data will be displayed in the next
100 if ((n = recv(sockfd, recv_buf, 14 , 0))< 0){
101 printf("recv failed\n");
103 // recv_buf[n-1] = 0; //null terminate
104 printf("[OUTPUT] recv %d with length %d from result %s\n", j,n, recv_buf);