1 #include <stdio.h> 2 #include <kernel/OS.h> 3 #include <string.h> 4 #include <sys/time.h> 5 #include <errno.h> 6 7 #include "sys/socket.h" 8 #include "netinet/in.h" 9 #include "arpa/inet.h" 10 #include "sys/select.h" 11 #include "sys/sockio.h" 12 13 #include "ufunc.h" 14 15 #define PORT 7772 16 #define HELLO_MSG "Hello from the server" 17 18 int main(int argc, char **argv) 19 { 20 int sock = socket(AF_INET, SOCK_STREAM, 0); 21 struct sockaddr_in sin; 22 int salen = sizeof(sin); 23 int rv, on = 1; 24 char buffer[50]; 25 26 test_banner("Accept Test - Client"); 27 28 ioctl(sock, FIONBIO, &on); 29 30 memset(&sin, 0, sizeof(sin)); 31 sin.sin_family = AF_INET; 32 sin.sin_port = 0; 33 sin.sin_len = salen; 34 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 35 rv = bind(sock, (const struct sockaddr*)&sin, salen); 36 if (rv < 0) 37 err(rv, "bind"); 38 printf("Bound\n"); 39 40 sin.sin_port = htons(PORT); 41 42 do { 43 rv = connect(sock, (const struct sockaddr*)&sin, salen); 44 } while (rv < 0 && errno == EINPROGRESS); 45 if (rv < 0 && errno != EISCONN) 46 err(errno, "connect"); 47 printf("connected!\n"); 48 49 do { 50 rv = read(sock, buffer, 50); 51 } while (rv < 0 && errno == EWOULDBLOCK); 52 if (rv < 0) 53 err(rv, "read"); 54 printf("%s\n", buffer); 55 56 close(sock); 57 58 return (0); 59 } 60 61 62