Ticket #5824: test_socket.c

File test_socket.c, 3.2 KB (added by Hapie, 10 years ago)

sample test code

Line 
1#include<stdio.h> //printf
2#include<string.h> //strlen
3#include<sys/socket.h> //socket
4#include<arpa/inet.h> //inet_addr
5#include<unistd.h> //usleep
6#include<fcntl.h> //fcntl
7#include<errno.h>
8
9//Size of each chunk of data received, try changing this
10#define CHUNK_SIZE 32767
11
12//Receiving function
13int recv_timeout(int s, int timeout);
14
15int main(int argc , char *argv[])
16{
17 int socket_desc;
18 struct sockaddr_in server;
19 char *message , server_reply[2000];
20
21 //Create socket
22 socket_desc = socket(AF_INET, SOCK_STREAM , 0);
23 if (socket_desc == -1)
24 {
25 printf("Could not create socket");
26 }
27
28 //ip address of www.msn.com (get by doing a ping www.msn.com at terminal)
29 server.sin_addr.s_addr = inet_addr("52.71.113.113");
30 server.sin_family = AF_INET;
31 server.sin_port = htons( 8888 );
32
33 //Connect to remote server
34 if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
35 {
36 puts("connect error");
37 return 1;
38 }
39
40 puts("Connected\n");
41
42 //Send some data
43 message = "GET /fatehtv.mp3 HTTP/1.0\r\nHost: Lavf54.3.100\r\n\r\n";
44 if( send(socket_desc , message , strlen(message) , 0) < 0)
45 {
46 puts("Send failed");
47 return 1;
48 }
49 puts("Data Send\n");
50
51 //Now receive full data
52 int total_recv = recv_timeout(socket_desc, 4);
53
54 printf("\n\nDone. Received a total of %d bytes\n\n" , total_recv);
55 return 0;
56}
57
58/*
59 Receive data in multiple chunks by checking a non-blocking socket
60 Timeout in seconds
61*/
62int recv_timeout(int s , int timeout)
63{
64 int size_recv , total_size= 0, i=0;
65 struct timeval begin , now;
66 char chunk[CHUNK_SIZE];
67 char *msg = "Hello";
68 double timediff;
69
70 //make socket non blocking
71 //fcntl(s, F_SETFD, FD_CLOEXEC);
72 fcntl(s, F_SETFD, O_NONBLOCK);
73
74 //beginning time
75 gettimeofday(&begin , NULL);
76
77 while(1)
78 {
79 gettimeofday(&now , NULL);
80
81 //time elapsed in seconds
82 timediff = (now.tv_sec - begin.tv_sec) + 1e-2 * (now.tv_usec - begin.tv_usec);
83
84 printf("Timediff: %lf", timediff);
85 //if you got some data, then break after timeout
86 if( total_size > 0 && timediff > timeout )
87 {
88 printf("timeout wait\n");
89 break;
90 }
91
92 //if you got no data at all, wait a little longer, twice the timeout
93 else if( timediff > timeout*2)
94 {
95 printf("Twice wait\n");
96 break;
97 }
98
99 memset(chunk ,0 , CHUNK_SIZE); //clear the variable
100 printf("stuck at recv\n");
101 if((size_recv = recv(s , chunk , CHUNK_SIZE , 0) ) < 0)
102 {
103 //if nothing was received then we want to wait a little before trying again, 0.1 seconds
104 usleep(100000);
105 printf("ERRNO: %d", errno);
106 printf("\n\nsleeping %d", size_recv);
107 }
108 else
109 {
110 total_size += size_recv;
111 //printf("%s" , chunk);
112 printf("%d\n", size_recv);
113 //usleep(10000000);
114 //reset beginning time
115
116 shutdown(s, 2);
117 //close(s);
118
119 printf("Reset Time\n");
120 gettimeofday(&begin , NULL);
121 }
122 i++;
123 printf("...............");
124 }
125
126 return total_size;
127}