Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void errormsg(char *message) // Error message function
{
perror(message); // Show user error message
exit(1); // exit
}
int main (int argc, char *argv[])
{
int sockd; // file descriptor for socket
int newsockd; // file descriptor for new socket
int portnum; // port number for transmissions
int clientlength; // length of client address
int retval; // return value
char buffer[256];
struct sockaddr_in serv_addr, cli_addr; // structure defined in netinet header file.
if (argc < 2) // Server must be initiated with a port.
{
fprintf(stderr,"Usage:./server Port \n");
exit(1);
}
sockd = socket(AF_INET, SOCK_STREAM, 0); // AF_INET for internet domain, Stream Socket
if (sockd == -1)
error("ERROR: Opening socket failed");
bzero((char *) &serv_addr, sizeof(serv_addr)); // Set all vals in serv_addr to zero
portnum = atoi(argv[1]); // Gets user input and stores as port number
serv_addr.sin_family = AF_INET; // Sets serv_addr in struct sock_addr_in
serv_addr.sin_port = htons(portnum); // change the port number from host to server format
serv_addr.sin_addr.s_addr = INADDR_ANY; // contains IP address of host
// bind socket to address, cast serv_addr to type struct sockaddr
if (bind(sockd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR: Bind failed"); // present error if it returns -1 (fails)
listen(sockd,5); // allow p to 5 connections that can wait while process handles an existing connection.
clientlength = sizeof(cli_addr); // set clientlength variable to the length of client address
// set reference to client address and length of client address
newsockd = accept(sockd, (struct sockaddr *) &cli_addr, &clientlength);
if (newsockd < 0)
error("ERROR: couldn't establish connection with client");
bzero(buffer,256); // zero the buffer values
retval = read(newsockd,buffer,255); // store the new socket file description into the buffer
if (retval < 0)
error("ERROR: could not read new socket file description");
printf("Message received from client is: %s", buffer); // show users output
// Receive user input here, go into text file and compare, grab a string
retval = write(newsockd, "I got your message", 18);
if (retval < 0)
error("ERROR: could not write to socket");
// Send string to user there ^
return 0;
}
Ok, here is my code for the server at the moment. This was made from going through the tutorial step by step. I feel confident to this point.
Now my strategy is going to be to deal with "buffer" as if I was in a seperate program, and simply trying to make a program that was given an array of characters and asked to find information in a text file. I'm going to get the information I need from the text file, and then return the result in another char[].
Then the only thing left to do is send it to the user replacing "I got your message" with the new string I got above.
Would this be correct?
I'm going to start working on the C code to search a text file and also retrieve text from a text file.
P.S I think I do need to do it using threads, but I'll come back to that after I get it working 1:1, as I think I will at least achieve a pass if I get it working 1:1.
Thanks again MK, really appreciate your help.
EDIT: My server DOES compile, and works with the client code from that website. (not surprisingly as I didn't change much, basically just went through and typed it up to understand it)