Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Most of the code is from http://www.paulgriffiths.net/program/c/srcs/echoclntsrc.html
- *
- */
- /* Just included everything possible .. dont think it makes a difference to the code */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <syslog.h>
- #include <netdb.h>
- #include <string.h>
- #include <ctype.h>
- #include <pwd.h>
- #include <regex.h>
- #include <sys/time.h>
- #include <signal.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #define BOUNCEHOST "127.0.0.1"
- #define BOUNCEPORT 1601
- #define MAX_LINE 1000
- /* Write a line to a socket */
- ssize_t Writeline(int sockd, const void *vptr, size_t n) {
- size_t nleft;
- ssize_t nwritten;
- const char *buffer;
- buffer = vptr;
- nleft = n;
- while ( nleft > 0 ) {
- if ( (nwritten = write(sockd, buffer, nleft)) <= 0 ) {
- if ( errno == EINTR )
- nwritten = 0;
- else
- return -1;
- }
- nleft -= nwritten;
- buffer += nwritten;
- }
- return n;
- }
- int main(int argc, char* argv[]) {
- int conn_s;
- struct sockaddr_in servaddr;
- char buffer[MAX_LINE];
- if (argc != 3) {
- fprintf(stderr, "Incorrect usage\n");
- exit(1);
- }
- if ( (conn_s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
- fprintf(stderr, "ECHOCLNT: Error creating listening socket.\n");
- exit(EXIT_FAILURE);
- }
- memset(&servaddr, 0, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_port = htons(BOUNCEPORT);
- if ( inet_aton(BOUNCEHOST, &servaddr.sin_addr) <= 0 ) {
- printf("ECHOCLNT: Invalid remote IP address.\n");
- exit(EXIT_FAILURE);
- }
- if ( connect(conn_s, (struct sockaddr *) &servaddr, sizeof(servaddr) ) < 0 ) {
- printf("ECHOCLNT: Error calling connect()\n");
- exit(EXIT_FAILURE);
- }
- while(fgets(buffer,MAX_LINE,stdin)) Writeline(conn_s, buffer, strlen(buffer));
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement