I'm having a bit of trouble letting the user supply the port number as an argument. I have a feeling it's a simple fix.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BUFFER 1024
#define TESTPORT 9999
int i;
void display_usage(void);
char *TARGETIP,the_buffer[BUFFER],return_data[BUFFER];
int main( int argc, char *argv[] ) {
if( argc < 2 )
{
display_usage();
exit(1);
}
//printf("You entered %s\n", argv[2]);
TARGETIP = argv[1]; // TARGETIP had to be a pointer
//TESTPORT = argv[2];
//fill the buffer up with 1024 A's
for( i = 0; i < BUFFER; i++)
the_buffer[i] = 'A';
//setup sockets
int testsocket;
struct sockaddr_in thetarget;
testsocket = socket(AF_INET, SOCK_STREAM, 0);
thetarget.sin_family = AF_INET; //always AF_INET
thetarget.sin_port = htons(TESTPORT); // *TESTPORT is argv[2]
thetarget.sin_addr.s_addr = inet_addr(TARGETIP); // TARGETIP is argv[1]
connect(testsocket, (struct sockaddr *)&thetarget, sizeof(struct sockaddr)); // make the connection
send(testsocket, the_buffer, strlen(the_buffer), 0); // send the buffer
while (1) {
recv(testsocket, return_data, strlen(the_buffer), 0); // put the length of he_buffer 1024 into return_data
printf("%s\n", return_data);
}
perror("connect");
}
void display_usage(void)
{
fprintf(stderr, "Usage: ./connect 127.0.0.1 9999\n" );
}
-Thanks in advance