Feb 22, 2012 07:12 AM
(This post was last modified: Feb 22, 2012 03:04 PM by g4143.)
Main Page - https://hackforums.net/showthread.php?tid=2220892
This part we'll create the client part of our tutorial.
First copy this program and compile.
client.c
Compile line
And usage, we pass as arguments the IP address 127.0.0.1 and the shell script 'hello' that we prepared in step two.
This is the simple step, creating the client. The program 'client' will only work when we complete the final step.
This part we'll create the client part of our tutorial.
First copy this program and compile.
client.c
Code
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define DEFAULT_PROTOCOL 0
#define PORT 50000
int main(int argc, char**argv)
{
int clientfd;
char recvline[BUFSIZ];
FILE * fd = NULL;
struct sockaddr_in servaddr;
if ( argc != 3 )
{
fputs( "Usage error - a.out <IPaddress> <Filename>\n", stderr );
exit(EXIT_FAILURE);
}
if (!(fd = fopen(argv[2], "r")))
{
fprintf(stderr, "Could not open %s\n", argv[2]);
exit(EXIT_FAILURE);
}
if ((clientfd = socket(AF_INET, SOCK_STREAM, DEFAULT_PROTOCOL)) < 0)
{
perror("socket");
exit(EXIT_FAILURE);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
if ((inet_pton(AF_INET, argv[1], &servaddr.sin_addr.s_addr)) <= 0)
{
fputs( "Conversion error!\n", stderr );
exit(EXIT_FAILURE);
}
if ((connect(clientfd, (const struct sockaddr*)&servaddr, sizeof(servaddr))) < 0)
{
perror("connect");
exit(EXIT_FAILURE);
}
while ( write(clientfd, recvline, fread(recvline, sizeof(char), BUFSIZ, fd)) )
{}
fclose(fd);
close(clientfd);
exit(EXIT_SUCCESS);
}Compile line
Code
gcc client.c -Wall -ansi -pedantic -o clientAnd usage, we pass as arguments the IP address 127.0.0.1 and the shell script 'hello' that we prepared in step two.
Code
./client 127.0.0.1 helloThis is the simple step, creating the client. The program 'client' will only work when we complete the final step.
