Thanx for the quick reply.
Code:
#include <winsock2.h>
#include <iostream.h>
#include <windows.h>
#include <fstream.h>
#include <time.h>
#include <string.h>
#define PORT 3550 /* Port that will be opened */
#define BACKLOG 1 /* Number of allowed connections */
#define MAXDATASIZE 100
using namespace std;
struct sockaddr_in server; /* server's address information */
struct sockaddr_in client; /* client's address information */
const int req_winsock_ver = 2;
int sin_size;
int numbytes;
char buf[MAXDATASIZE];
WSADATA wsdata;
SOCKET fd, fd2;
DWORD wsock;
//time_t T;
int main()
{
WSAStartup(MAKEWORD(req_winsock_ver, 0), &wsdata);
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) /* calls socket() */
{
cout<<"Socket error";
return -1;
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY; /* INADDR_ANY puts your IP address automatically */
if(bind(fd,(struct sockaddr*)&server,sizeof(struct sockaddr)) == -1) /* calls bind() */
{
cout<<"Bind Error";
return -1;
}
if(listen(fd,BACKLOG) == -1) /* calls listen() */
{
cout<<"Listen Error";
return -1;
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if ((fd2 = accept(fd,(struct sockaddr *)&client,&sin_size)) == -1) /* calls accept() */
{
cout<<"Accept Error";
return -1;
}
cout<<"Client connected to server.\n";
//time(&T);
//tm *local_time = localtime(&T);
//char now[41];
//strftime(now,"%a %b %d %H:%M:%S %Y",local_time);
ofstream a_file("data.txt", ios::app);
a_file<<"IP: "<<inet_ntoa(client.sin_addr)<<" connected to server at:";//<<now<<"\n";
a_file.close();
send(fd2,"Welcome to my server.\n",22,0); /* send to the client welcome message */
while(1)
{
numbytes = recv(fd2,buf,MAXDATASIZE,0);
if(strcmp(buf, "exit") == 1)
{
cout<<"exit";
send(fd2,"Exitting.",sizeof("Exitting.\n"),0);
}else{
cout<<buf;
send(fd2,"Cooooolll.",sizeof("Cooooolll.\n"),0);
}
}
WSACleanup();
}
return 0;
}
OK thats the code, I'm just using telnet to connect to it for now.
And as you can see I commented out the date thing because it doesn't work.
Ok so it recieves "exit" which is put into the buffer as you can see and then it strcmp the buffer with "exit", if it is exit it sends the client (telnet in this case) a message saying Exitting and else it sends Cooolllll.
But it doesn't work. What am I doing wrong? I'm pretty sure its something with the string compare.
[Devouring One]