Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-
- * Copyright (c) 2013 Michael Tuexen
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/select.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdio.h>
- #include "socket.h"
- #define BUFFER_SIZE (2<<16)
- #define MESSAGE_SIZE (9216)
- int main(int argc, char **argv)
- {
- int fd, in, port;
- fd_set rset;
- struct sockaddr_in server_addr;
- ssize_t len;
- char buf[BUFFER_SIZE];
- if (argc != 3)
- {
- printf("usage: echo_client \"IP-Adress Port\"\n");
- return 0;
- }
- port = atoi(argv[2]);
- /* Adresse ausgeben */
- // printf("Address: %s:%d\n", argv[1], port);
- in = 0;
- fd = Socket(AF_INET, SOCK_STREAM, 0);
- memset(&server_addr, 0, sizeof(server_addr));
- server_addr.sin_family = AF_INET;
- #ifdef HAVE_SIN_LEN
- server_addr.sin_len = sizeof(struct sockaddr_in);
- #endif
- server_addr.sin_port = htons(port);
- if ((server_addr.sin_addr.s_addr = (in_addr_t)inet_addr(argv[1])) == INADDR_NONE) {
- fprintf(stderr, "Invalid address\n");
- }
- Connect(fd, (const struct sockaddr *) &server_addr, sizeof(server_addr));
- memset((void *) buf, 'A', sizeof(buf));
- /* FD-Set initialisieren */
- FD_ZERO(&rset);
- /* FileDescriptors zum set hinzufügen */
- FD_SET(in, &rset);
- FD_SET(fd, &rset);
- for(;;)
- {
- if (Select(fd + 1, &rset, NULL, NULL, NULL) == 0)
- {
- /* Timeout... call Select again */
- printf("Select call timed out\n");
- continue;
- }
- /* Pending user input */
- if (FD_ISSET(in, &rset))
- {
- /* Buffer "nullen" */
- memset((void *) buf, 0, sizeof(buf));
- len = Read(in, (void *) buf, sizeof(buf));
- if (len < 0)
- {
- perror("read");
- }
- /* Stdin geschlossen */
- if (len == 0)
- {
- /* Verbindungsabbau anstossen */
- Shutdown(fd, SHUT_WR);
- printf("Verbindungsabbau...\n");
- }
- /* Send user input to server */
- Write(fd, (const void *) buf, (size_t) len);
- buf[len] = '\0';
- printf("Send to server: %s\n", buf);
- }
- /* Pending data on port */
- if (FD_ISSET(fd, &rset))
- {
- /* Buffer "nullen" */
- memset((void *) buf, 0, sizeof(buf));
- /* Socket lesen */
- len = Read(fd, (void *) buf, sizeof(buf));
- if (len == 0)
- {
- /* Server closed the connection */
- printf("Server closed the connection.\n");
- break;
- }
- buf[len] = '\0';
- printf("%s", buf);
- }
- }
- /* Client soll sich beenden sobald der Server die Verbindung schließt */
- Close(fd);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement