I'm sorry to ask such a newbie question, but I've sort of jumped ahead and skipped some integral peices in my c knowlege. Basically, this is a small c server, and when it recieves a string, I want to compare it to a known string (ie "hello") and do something. But it says that I can't compare something of different types (I'm using gcc).
Here's the code, and thanks:
Code:
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen; //make objects
char buffer[256]; //make string
//strcpy(mystring,"my val");
struct sockaddr_in serv_addr, cli_addr; //make struct
int n; //make int
if (argc < 2) {
fprintf(stderr,"usage: %s <port>\n",argv[0]);
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0); //create the socket
if (sockfd < 0) //errors?
error("ERROR opening socket"); //...
bzero((char *) &serv_addr, sizeof(serv_addr)); //### don't know (yet)
portno = atoi(argv[1]); //setup the inet struct, btw argv[1]: first argument
serv_addr.sin_family = AF_INET; // ...
serv_addr.sin_addr.s_addr = INADDR_ANY; //...
serv_addr.sin_port = htons(portno); //...
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) //bind to port
error("ERROR on binding"); //make sure no error
listen(sockfd,5); //listen
clilen = sizeof(cli_addr); //### don't know
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen); //accept connection
if (newsockfd < 0) //see if it works
error("ERROR on accept"); //tru dat
bzero(buffer,256); //### Don't Know
n = read(newsockfd,buffer,255); //get recieved data
if (n < 0) error("ERROR reading from socket"); //make sure no error
//### my new section ###
if (strcmp(buffer,"hello")=0) {
n = write(newsockfd,"Hello to you too!",17); // write to socket
if (n < 0) error("ERROR writing to socket"); //check for error
}
if (strcmp(buffer,"goodbye")=0) {
n = write(newsockfd,"See ya later!",13); // write to socket
if (n < 0) error("ERROR writing to socket"); //check for error
}
if (strcmp(buffer,"test")=0) {
n = write(newsockfd,"Workin' on this end!",20); // write to socket
if (n < 0) error("ERROR writing to socket"); //check for error
}
//### end my section ###
n = write(newsockfd,"I got your message",18); // write to socket
if (n < 0) error("ERROR writing to socket"); //check for error
return 0;
}
Sorry that was a little long, I didn't want to omit anything.
THANKS AGAIN!