#include #include #include #include #include #include #include #include int main(int argc, char **argv) { int ssock, i; static char buff[256]; ssize_t nbytes; struct sockaddr_in saddr; memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_port = htons(6666); printf("Client started...\n"); if ((ssock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("socket() error. (%d) %s\n", errno, strerror(errno)); exit(1); } printf("Created SOCK_STREAM socket\n"); if (connect(ssock, (struct sockaddr *)&saddr, sizeof(saddr)) == -1) { printf("connect() error. (%d) %s\n", errno, strerror(errno)); close(ssock); exit(1); } printf("Connected to the server!\n"); printf("Sending some data to server\n"); if ((nbytes = send(ssock, "MSG from client", sizeof("MSG from client"), 0)) <= 0) { printf("send() error. (%d) %s\n", errno, strerror(errno)); close(ssock); exit(1); } printf("Half-closing connection with shutdown(ssock, SHUT_WR)\n"); if (shutdown(ssock, SHUT_WR) == -1) { printf("shutdown() error. (%d) %s\n", errno, strerror(errno)); close(ssock); exit(1); } printf("Receiving data from server\n"); if ((nbytes = recv(ssock, buff, sizeof(buff), 0)) > 0) { printf("Received: \"%s\"\n", buff); } else { printf("recv() error. (%d) %s\n", errno, strerror(errno)); close(ssock); exit(1); } printf("Client exiting...\n"); close(ssock); return 0; }