Jag har lite kod (se längre ner) som sätter 2 olika variabler som jag sedan vill jämföra med varandra för att se om dem är lika.
För att kolla vad variablerna håller så tog jag och printade ut dem till konsollen:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <TelldusCore/telldus-core.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
//Checking for correct executing parameters
int P_value = 6435;
char *u_value = NULL;
char *p_value = NULL;
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'P': P_value = atoi(argv[++i]);
break;
case 'u': u_value = argv[++i];
break;
case 'p': p_value = argv[++i];
break;
}
}
}
if (P_value < 2 || u_value == NULL || p_value == NULL)
error("ERROR: To few arguments supplied, look at --help");
printf("-P = %d\n", P_value);
printf("-u = %s\n", u_value);
printf("-p = %s\n", p_value);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
//portno = atoi(argv[1]);
portno = P_value;
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)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
while (1){
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
int pid;
pid=fork();
if (pid < 0)
error("ERROR on fork");
if (pid == 0){
close(sockfd);
bzero(buffer,256);
n = write(newsockfd,"This is version 0.1 of the API >>",33);
if (n < 0) error("ERROR writing to socket");
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
//Check if message contains "user" and if submitted user is a valid user
char *to = (char*) malloc(6);
strncpy(to,buffer+5,strlen(buffer)-6);
to[strlen(to)-1]='\0';
int tol=strlen(to);
printf("to: %s, strlen(to):%i\n\r",to,tol);
int ul=strlen(u_value);
printf("uvalue: %s, strlen(uvalue):%i\n\r",u_value,ul);
if(to==u_value)
printf("to=cvalue");
if(strstr(buffer, "user")){
printf("user finnes. User är: %s och är korrekt\n\r",to);
//n = write(newsockfd,printf("Hello %s. Give me your password >>",to),18);
//if (n < 0) error("ERROR writing to socket");
}
else{
printf("-u:%s, to:%s, buffer:%s",u_value,to,buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
}
printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
return 0;
exit(0);
}
else{
close(newsockfd);
}
}
}
Kan tillägga att jag är en gnutta amatör på c/c++ så koden kan nog verka rörig :/