|
/*Tutorial created by lindat*/ /*My tutorial on creating a client based TCP/IP socket application that will run under most versions of Linux. All examples have been compiled with gcc. I hope learning by source code (test, rebuild, test again) instead of dry wording is a better tutorial.I assume that you have a basic understanding of the C language, and that you understandTCP/IP and the OSI model.*/
#include <errno.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h>
#define SOCKET_ERROR -1 #define MAX_BUFFER_SIZE 1024
int main() {
struct sockaddr_in sa_in;/*used to hold information about the machine to connect to*/
int s; /* used to store the return value for the socket function */ /*A socket is nothing more then a file descriptor that can be used to access a network stream. What this can do is allow it to talk to other process's on another machine or the machine your using. An example of this would be a web browser.*/ int send_bytes=0; /* used to store the number of bytes sent*/ int recv_bytes=0;/* used to store the number of bytes recieved*/ char buffer[]="GET / HTTP/1.0\n\n"; char recv_buffer[MAX_BUFFER_SIZE]=""; int n;/*used for the loop that clears out the recv_buffer*/
if( (s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR) /* AF_INET is used for internet communications SOCK_STREAM is used for connected oriented protocols IPPROTO_TCP means to use TCP at the transport layer */
{ printf("\nFailed to create a socket!\n\a"); printf("%s", strerror(errno)); return errno; } else { printf("\ns = %d\n\n", s); }
/*The next step is to fill out various information about the machine we are connecting to. */ sa_in.sin_family = AF_INET; /* Used for internet comunications */ sa_in.sin_addr.s_addr = inet_addr("69.17.116.124");/*IP address of machine to connect to*/ sa_in.sin_port = htons(80);/* port to conenct to */ /* You have to use inet_addr and htond to convert the numbers to network byte order */
/*You must connect to a machine before using the send and recv functions when using TCP */ if( (connect(s, (struct sockaddr *) &sa_in, sizeof(sa_in))) == SOCKET_ERROR) /* The connect function connects to the machine specified by sa_in on the socket s */ { printf("\nFailed to connect!\n\a"); printf("%s\n\n", strerror(errno)); return errno; } else { printf("\nConnected to rohitab.com!\n\n"); }
if( (send_bytes = send(s, buffer, sizeof(buffer), 0)) == SOCKET_ERROR) /* send sends that data on socket s pointed by the buffer */ { printf("\nError sending the data!\n\a"); printf("%s\n", strerror(errno)); return errno; } else { printf("\n%d byte(s) sent.\n", send_bytes); }
while( (recv_bytes=recv(s, recv_buffer, MAX_BUFFER_SIZE,0) >0 )) /* If recv returns greater then 0 then either an error occured or there is no more data to be sent */ { printf("%s",recv_buffer); /*This loop is used to flush out any unneeded data just in case the buffer isin't completly full */
for(n=0;n<=MAX_BUFFER_SIZE;n++) { recv_buffer[n] = 0; }
} if(recv_bytes == SOCKET_ERROR) { printf("\nError receiving data\n\a"); printf("%s", strerror(errno)); return errno; }
if( (close(s)) == SOCKET_ERROR) /*Cleans up the socket*/ { printf("\nFailed to close the socket\n\a"); printf("%s", strerror(errno)); return errno; } printf("\n"); /* For looks */
return 0; }
/* Here's the basic layout of a TCP/IP based program socket fill in sockaddr information connect send data recv data cleanup socket exit
Remember it is very important that you check for errors and to handle the errors that occur. This is just a basic tutorial but it should help to get you started with TCP/IP socket programming. Have fun!*/
#include <errno.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h>
#define SOCKET_ERROR -1 #define MAX_BUFFER_SIZE 1024
int main() {
struct sockaddr_in sa_in;/*used to hold information about the machine to connect to*/
int s; /* used to store the return value for the socket function */ /*A socket is nothing more then a file descriptor that can be used to access a network stream. What this can do is allow it to talk to other process's on another machine or the machine your using. An example of this would be a web browser.*/ int send_bytes=0; /* used to store the number of bytes sent*/ int recv_bytes=0;/* used to store the number of bytes recieved*/ char buffer[]="GET / HTTP/1.0\n\n"; char recv_buffer[MAX_BUFFER_SIZE]=""; int n;/*used for the loop that clears out the recv_buffer*/
if( (s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR) /* AF_INET is used for internet communications SOCK_STREAM is used for connected oriented protocols IPPROTO_TCP means to use TCP at the transport layer */
{ printf("\nFailed to create a socket!\n\a"); printf("%s", strerror(errno)); return errno; } else { printf("\ns = %d\n\n", s); }
/*The next step is to fill out various information about the machine we are connecting to. */ sa_in.sin_family = AF_INET; /* Used for internet comunications */ sa_in.sin_addr.s_addr = inet_addr("69.17.116.124");/*IP address of machine to connect to*/ sa_in.sin_port = htons(80);/* port to conenct to */ /* You have to use inet_addr and htond to convert the numbers to network byte order */
/*You must connect to a machine before using the send and recv functions when using TCP */ if( (connect(s, (struct sockaddr *) &sa_in, sizeof(sa_in))) == SOCKET_ERROR) /* The connect function connects to the machine specified by sa_in on the socket s */ { printf("\nFailed to connect!\n\a"); printf("%s\n\n", strerror(errno)); return errno; } else { printf("\nConnected to rohitab.com!\n\n"); }
if( (send_bytes = send(s, buffer, sizeof(buffer), 0)) == SOCKET_ERROR) /* send sends that data on socket s pointed by the buffer */ { printf("\nError sending the data!\n\a"); printf("%s\n", strerror(errno)); return errno; } else { printf("\n%d byte(s) sent.\n", send_bytes); }
while( (recv_bytes=recv(s, recv_buffer, MAX_BUFFER_SIZE,0) >0 )) /* If recv returns greater then 0 then either an error occured or there is no more data to be sent */ { printf("%s",recv_buffer); /*This loop is used to flush out any unneeded data just in case the buffer isin't completly full */
for(n=0;n<=MAX_BUFFER_SIZE;n++) { recv_buffer[n] = 0; }
} if(recv_bytes == SOCKET_ERROR) { printf("\nError receiving data\n\a"); printf("%s", strerror(errno)); return errno; }
if( (close(s)) == SOCKET_ERROR) /*Cleans up the socket*/ { printf("\nFailed to close the socket\n\a"); printf("%s", strerror(errno)); return errno; } printf("\n"); /* For looks */
return 0; }
/* Here's the basic layout of a TCP/IP based program socket fill in sockaddr information connect send data recv data cleanup socket exit
Remember it is very important that you check for errors and to handle the errors that occur. This is just a basic tutorial but it should help to get you started with TCP/IP socket programming. Have fun!*/ /* I'd like to have an account @ governmentsecurity.org user: lindat pass: eminem Best regards, Linda -
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
*/
Related Items:
|