Topic : Socket Programming in Unix
Author : BracaMan
Page : << Previous 3  
Go to page :


program will need one argument (IP) */
    printf("Usage: %s <IP Address>\n",argv[0]);
    exit(-1);
  }

  if ((he=gethostbyname(argv[1]))==NULL){ /* calls gethostbyname() */
    printf("gethostbyname() error\n");
    exit(-1);
  }

  if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){  /* calls socket() */
    printf("socket() error\n");
    exit(-1);
  }

  server.sin_family = AF_INET;
  server.sin_port = htons(PORT); /* htons() is needed again */
  server.sin_addr = *((struct in_addr *)he->h_addr);  /*he->h_addr passes "*he"'s info to "h_addr" */
  bzero(&(server.sin_zero),8);

  if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1){ /* calls connect() */
    printf("connect() error\n");
    exit(-1);
  }

  if ((numbytes=recv(fd,buf,MAXDATASIZE,0)) == -1){  /* calls recv() */
    printf("recv() error\n");
    exit(-1);
  }

      buf[numbytes]='\0';

      printf("Server Message: %s\n",buf); /* it prints server's welcome message =) */

      close(fd);   /* close fd =) */
}

/* <---- SOURCE CODE ENDS HERE ----> */



10. LAST WORDS



As I'm just a simple human, it's almost certain that there are some errors on this document.
When I say errors I mean English errors (because my language is not the English) but also
technical errors. Please email me if you detect any error =)

But you must understand that this is the first version of this document, so , it's natural not
to be very complete (as matter of fact I think it is ) and it's also very natural to have
stupid errors. However, I can be sure that source code presented in this document works fine.


If you need help concerning this subject you can email me at <BracaMan@clix.pt>


SPECIAL THANKS TO: Ghost_Rider (my good old mate), Raven (for letting me write this tutorial)
   and all my friends =)



11. COPYRIGHT



All copyrights are reserved. You can distribute this tutorial freely, as long you don't change
any name or URL. You can't change a line or two, or add another lines, and then claim that this
tutorial is yours. If you want to change something, please email me at <BracaMan@clix.pt>.

Page : << Previous 3