source: npl/mediabox/lcdproc_edwin/src/shared/sockets.c @ c5c522c

gcc484ntopperl-5.22
Last change on this file since c5c522c was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 3.5 KB
Line 
1#include <unistd.h>
2#include <stddef.h>
3#include <stdio.h>
4#include <errno.h>
5#include <stdlib.h>
6#include <sys/socket.h>
7#include <sys/un.h>
8#include <sys/time.h>
9#include <sys/types.h>
10#include <netinet/in.h>
11#include <netdb.h>
12#include <sys/socket.h>
13#include <arpa/inet.h>
14#include <fcntl.h>
15
16#include "debug.h"
17#include "sockets.h"
18
19
20/**************************************************
21  LCDproc client sockets code...
22
23  Feel free to use this in your own clients... :)
24**************************************************/
25
26
27// Length of longest transmission allowed at once...
28#define MAXMSG 8192
29
30
31typedef struct sockaddr_in sockaddr_in;
32
33
34static int sock_init_sockaddr (sockaddr_in *name,
35                   const char *hostname,
36                   unsigned short int port)
37{
38  struct hostent *hostinfo;
39     
40  name->sin_family = AF_INET;
41  name->sin_port = htons (port);
42  hostinfo = gethostbyname (hostname);
43  if (hostinfo == NULL)
44    {
45      fprintf (stderr,"sock_init_sockaddr: Unknown host %s.\n", hostname);
46      return -1;
47    }
48  name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
49
50  return 0;
51 
52}
53
54 // Client functions...
55int sock_connect(char *host, unsigned short int port)
56{
57  struct sockaddr_in servername;
58  int sock;
59  int err=0;
60 
61 
62  debug("sock_connect: Creating socket\n");
63  sock = socket(PF_INET, SOCK_STREAM, 0);
64  if(sock < 0)
65    {
66      perror("sock_connect: Error creating socket");
67      return sock;
68    }
69  debug("sock_connect: Created socket (%i)\n", sock);
70 
71  sock_init_sockaddr(&servername, host, port);
72 
73  err = connect (sock,
74                 (struct sockaddr *) &servername,
75                 sizeof (servername));
76  if(err<0)
77    {
78      perror("sock_connect: connect failed");
79      shutdown(sock, 2);
80      return 0;  // Normal exit if server doesn't exist...
81    }
82 
83  fcntl(sock, F_SETFL, O_NONBLOCK);
84
85 
86  return sock;
87}
88
89int sock_close(int fd)
90{
91   int err;
92   
93   err=shutdown(fd, 2);
94
95   return err;
96}
97
98// Send/receive lines of text
99int sock_send_string(int fd, char *string)
100{
101   int err;
102
103   if(!string) return -1;
104   
105   err = write (fd, string, strlen(string) + 1);
106   if (err < 0)
107   {
108      perror ("sock_send_string: socket write error");
109      printf("Message was: %s\n", string);
110      //shutdown(fd, 2);
111      return err;
112   }
113
114   //printf("sock_send_string: %i bytes\n", err);
115   
116   return err;
117}
118
119// Recv gives only one line per call...
120int sock_recv_string(int fd, char *dest, size_t maxlen)
121{
122   char * err;
123   int i;
124
125   // TODO:  Get this function to work right somehow...
126   return -1;
127   
128   if(!dest) return -1;
129   if(maxlen <= 0) return 0;
130
131   // Read in characters until the end of the line...
132   for(i=0;
133       i<maxlen && (read(fd, dest+i, 1) > 0);
134       i++)
135      if(dest[i] == 0  ||  dest[i] == '\n') break;
136     
137   if (err == NULL)
138   {
139      perror("sock_recv_string: socket read error");
140      //shutdown(fd, 2);
141      return -1;
142   }
143   printf("sock_recv_string: Got message \"%s\"\n", dest);
144   
145   return strlen(dest);
146}
147
148// Send/receive raw data
149int sock_send(int fd, void *src, size_t size)
150{
151   int err;
152   
153   if(!src) return -1;
154   
155   err = write (fd, src, size);
156   if (err < 0)
157   {
158      perror("sock_send: socket write error");
159      //shutdown(fd, 2);
160      return err;
161   }
162
163   return err;
164}
165
166int sock_recv(int fd, void *dest, size_t maxlen)
167{
168   int err;
169   
170   if(!dest) return -1;
171   if(maxlen <= 0) return 0;
172   
173   err = read (fd, dest, maxlen);
174   if (err < 0)
175   {
176      //fprintf (stderr,"sock_recv: socket read error\n");
177      //shutdown(fd, 2);
178      return err;
179   }
180   //debug("sock_recv: Got message \"%s\"\n", (char *)dest);
181   
182   return err;
183}
Note: See TracBrowser for help on using the repository browser.