root/nano-RK/tools/SLIPstream/SLIPstream-client/slipstream.c

Revision 326, 1.2 kB (checked in by agr, 8 months ago)

slip update

Line 
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <netinet/in.h>
4 #include <arpa/inet.h>
5 #include <netdb.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <slipstream.h>
11
12   int sock, length, n, i, cnt;
13   struct sockaddr_in server, from;
14   struct hostent *hp;
15
16
17 int slipstream_open(char *addr, int port, int blocking_read)
18 {
19   sock = socket (AF_INET, SOCK_DGRAM, 0);
20   if (sock < 0)
21           {
22                 perror ("socket");
23                 return 0;
24           }
25
26   if(blocking_read==0)
27         {
28         // make socket non-blocking...
29         fcntl (sock, F_SETFL, O_NONBLOCK);
30         }
31  
32   server.sin_family = AF_INET;
33   hp = gethostbyname (addr);
34   if (hp == 0)
35           {
36                 perror ("Unknown host");
37                 return 0;
38           }
39
40   bcopy ((char *) hp->h_addr, (char *) &server.sin_addr, hp->h_length);
41   server.sin_port = htons (port);
42   length = sizeof (struct sockaddr_in);
43
44 return 1;
45 }
46
47 int slipstream_send(char *buf, int size)
48 {
49 int n;
50     n = sendto (sock, buf, size, 0, (struct sockaddr *) &server, length);
51     if (n < 0)
52       {
53       perror ("Sendto");
54       return 0;
55       }
56
57 return 1;
58 }
59
60 int slipstream_receive(char *buf)
61 {
62 int n;
63     n = recvfrom (sock, buf, MAX_BUF, 0, (struct sockaddr *) &from, &length);
64 return n;
65 }
66
Note: See TracBrowser for help on using the browser.