]>
cygwin.com Git - cygwin-apps/setup.git/blob - simpsock.cc
2 * Copyright (c) 2000, Red Hat, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
12 * Written by DJ Delorie <dj@cygnus.com>
16 /* Simplified socket access functions */
19 static const char *cvsid
=
34 SimpleSocket::SimpleSocket (const char *hostname
, int port
)
36 static int initted
= 0;
41 WSAStartup (MAKEWORD (1, 1), &d
);
51 if (sscanf (hostname
, "%d.%d.%d.%d", &i1
, &i2
, &i3
, &i4
) == 4)
61 he
= gethostbyname (hostname
);
64 msg ("Can't resolve `%s'\n", hostname
);
67 memcpy (ip
, he
->h_addr_list
[0], 4);
70 s
= socket (AF_INET
, SOCK_STREAM
, 0);
71 if (s
== INVALID_SOCKET
)
73 msg ("Can't create socket, %d", WSAGetLastError ());
77 struct sockaddr_in name
;
79 memset (&name
, 0, sizeof (name
));
80 name
.sin_family
= AF_INET
;
81 name
.sin_port
= htons (port
);
82 memcpy (&name
.sin_addr
, ip
, 4);
84 if (connect (s
, (sockaddr
*) & name
, sizeof (name
)))
86 msg ("Can't connect to %s:%d", hostname
, port
);
95 SimpleSocket::~SimpleSocket ()
103 if (s
== INVALID_SOCKET
)
109 SimpleSocket::printf (const char *fmt
, ...)
113 va_start (args
, fmt
);
114 vsprintf (buf
, fmt
, args
);
115 return write (buf
, strlen (buf
));
119 SimpleSocket::write (const char *buf
, int len
)
124 if ((rv
= send (s
, buf
, len
, 0)) == -1)
130 SimpleSocket::fill ()
136 buf
= new char [SSBUFSZ
+ 3];
140 int n
= SSBUFSZ
- putp
;
143 int r
= recv (s
, buf
+ putp
, n
, 0);
148 else if (r
< 0 && putp
== getp
)
156 SimpleSocket::gets ()
158 if (getp
> 0 && putp
> getp
)
160 memmove (buf
, buf
+ getp
, putp
- getp
);
168 // getp is zero, always, here, and putp is the count
170 while ((nl
= (char *) memchr (buf
, '\n', putp
)) == NULL
&& putp
< SSBUFSZ
)
177 while ((*nl
== '\n' || *nl
== '\r') && nl
>= buf
)
180 else if (putp
> getp
)
192 #define MIN(a,b) ((a) < (b) ? (a) : (b))
195 SimpleSocket::read (char *ubuf
, int ulen
)
203 n
= MIN (ulen
, putp
- getp
);
204 memmove (ubuf
, buf
+ getp
, n
);
212 n
= recv (s
, ubuf
, ulen
, 0);
216 return rv
> 0 ? rv
: n
;
225 SimpleSocket::invalidate (void)
227 if (s
!= INVALID_SOCKET
)
This page took 0.044393 seconds and 5 git commands to generate.