Made sock_recv_string work.
sock_send_string and sock_send "corrected" to handle cases where not all the bytes intended to be sent are actually sent (must loop to send the remaining bytes). File descriptors were not actually being closed by sock_close (they were only being shutdown).
This commit is contained in:
+74
-30
@@ -91,6 +91,7 @@ int sock_close(int fd)
|
|||||||
int err;
|
int err;
|
||||||
|
|
||||||
err=shutdown(fd, 2);
|
err=shutdown(fd, 2);
|
||||||
|
if (!err) close (fd);
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -98,69 +99,112 @@ int sock_close(int fd)
|
|||||||
// Send/receive lines of text
|
// Send/receive lines of text
|
||||||
int sock_send_string(int fd, char *string)
|
int sock_send_string(int fd, char *string)
|
||||||
{
|
{
|
||||||
int err;
|
int len;
|
||||||
|
int offset=0;
|
||||||
|
|
||||||
if (!string) return -1;
|
if (!string) return -1;
|
||||||
|
|
||||||
err = write (fd, string, strlen(string) + 1);
|
len = strlen (string) + 1;
|
||||||
if (err < 0)
|
while (offset != len) {
|
||||||
{
|
// write isn't guaranteed to send the entire string at once,
|
||||||
|
// so we have to sent it in a loop like this
|
||||||
|
int sent = write (fd, string+offset, len-offset);
|
||||||
|
if (sent == -1) {
|
||||||
|
if (errno != EAGAIN) {
|
||||||
perror ("sock_send_string: socket write error");
|
perror ("sock_send_string: socket write error");
|
||||||
printf("Message was: %s\n", string);
|
printf("Message was: %s\n", string);
|
||||||
//shutdown(fd, 2);
|
//shutdown(fd, 2);
|
||||||
return err;
|
return sent;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
} else if (sent == 0) {
|
||||||
|
// when this returns zero, it generally means
|
||||||
|
// we got disconnected
|
||||||
|
return sent+offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
//printf("sock_send_string: %i bytes\n", err);
|
offset += sent;
|
||||||
|
}
|
||||||
|
|
||||||
return err;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recv gives only one line per call...
|
// Recv gives only one line per call...
|
||||||
int sock_recv_string(int fd, char *dest, size_t maxlen)
|
int sock_recv_string(int fd, char *dest, size_t maxlen)
|
||||||
{
|
{
|
||||||
char * err;
|
char *ptr = dest;
|
||||||
int i;
|
int recv = 0;
|
||||||
|
|
||||||
// TODO: Get this function to work right somehow...
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (!dest) return -1;
|
if (!dest) return -1;
|
||||||
if (maxlen <= 0) return 0;
|
if (maxlen <= 0) return 0;
|
||||||
|
|
||||||
// Read in characters until the end of the line...
|
while (1) {
|
||||||
for(i=0;
|
int err = read (fd, ptr, 1);
|
||||||
i<maxlen && (read(fd, dest+i, 1) > 0);
|
if (err == -1) {
|
||||||
i++)
|
if (errno == EAGAIN) {
|
||||||
if(dest[i] == 0 || dest[i] == '\n') break;
|
if (recv) {
|
||||||
|
// We've begun to read a string, but no bytes are
|
||||||
if (err == NULL)
|
// available. Loop.
|
||||||
{
|
continue;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
perror ("sock_recv_string: socket read error");
|
perror ("sock_recv_string: socket read error");
|
||||||
//shutdown(fd, 2);
|
return err;
|
||||||
return -1;
|
}
|
||||||
|
} else if (err == 0) {
|
||||||
|
return recv;
|
||||||
}
|
}
|
||||||
printf("sock_recv_string: Got message \"%s\"\n", dest);
|
|
||||||
|
|
||||||
return strlen(dest);
|
recv++;
|
||||||
|
|
||||||
|
if (recv == maxlen || *ptr == 0 || *ptr == 10) {
|
||||||
|
*ptr = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recv == 1 && dest[0] == 0) {
|
||||||
|
// Don't return a null string
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recv < maxlen-1) {
|
||||||
|
dest[recv] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return recv;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send/receive raw data
|
// Send/receive raw data
|
||||||
int sock_send(int fd, void *src, size_t size)
|
int sock_send(int fd, void *src, size_t size)
|
||||||
{
|
{
|
||||||
int err;
|
int offset=0;
|
||||||
|
|
||||||
if (!src) return -1;
|
if (!src) return -1;
|
||||||
|
|
||||||
err = write (fd, src, size);
|
while (offset != size) {
|
||||||
if (err < 0)
|
// write isn't guaranteed to send the entire string at once,
|
||||||
{
|
// so we have to sent it in a loop like this
|
||||||
|
int sent = write (fd, ((char*)src)+offset, size-offset);
|
||||||
|
if (sent == -1) {
|
||||||
|
if (errno != EAGAIN) {
|
||||||
perror ("sock_send: socket write error");
|
perror ("sock_send: socket write error");
|
||||||
//shutdown(fd, 2);
|
//shutdown(fd, 2);
|
||||||
return err;
|
return sent;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
} else if (sent == 0) {
|
||||||
|
// when this returns zero, it generally means
|
||||||
|
// we got disconnected
|
||||||
|
return sent+offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
return err;
|
offset += sent;
|
||||||
|
}
|
||||||
|
|
||||||
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sock_recv(int fd, void *dest, size_t maxlen)
|
int sock_recv(int fd, void *dest, size_t maxlen)
|
||||||
|
|||||||
Reference in New Issue
Block a user