Ok this is what I have but have a few problems.
This is the server code.
Code:
/* server.c */
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
main()
{
int cont,create_socket,new_socket,addrlen;
int bufsize = 512;
char filename [10];
int messageno = 0;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
FILE *newfile;
printf("\x1B[2J");
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("Socket created\n");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(15000);
if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("Socket Bound\n");
listen(create_socket,3);
addrlen = sizeof(struct sockaddr_in);
new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
if (new_socket > 0){
printf("The Client %s is connected...\n",inet_ntoa(address.sin_addr));
for(cont=1;cont<5000;cont++)
printf("\x7");
}
do{
recv(new_socket,buffer,bufsize,0);
printf("Message recieved: %s\n",buffer);
messageno = (messageno + 1);
printf ("Saving to file\n");
newfile = fopen ("messages.txt", "a");
fprintf (newfile,"%d %s \n",messageno, buffer);
}while(strcmp(buffer,"/q")); //user ‘q’ to quit
fclose (newfile);
close(new_socket);
close(create_socket);
}
And this is the client code.
Code:
/* client.c */
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<arpa/inet.h>
main(int argc,char *argv[])
{
int create_socket;
int bufsize = 160;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
int choice;
void sendmess ()
{
printf("\x1B[2J");
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("Socket created\n");
address.sin_family = AF_INET;
address.sin_port = htons(15000);
inet_pton(AF_INET,argv[1],&address.sin_addr);
if (connect(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("Connection accepted with the server %s...\n\n",inet_ntoa(address.sin_addr));
do{
printf("Message to send: ");
gets(buffer);
send(create_socket,buffer,bufsize,0);
}while (strcmp(buffer,"/q"));
close(create_socket);
}
void recvmess ()
{
recv(create_socket,buffer,bufsize,0);
printf("Message recieved: %s\n",buffer);
}
do {
printf(" Plesae choose and option\n");
printf(" \n\n");
printf(" Press <1> to send a message\n");
printf(" Press <2> to receive a message\n");
printf (" Please enter number and hit return key: ");
scanf ("%d",&choice);
switch(choice)
{
case 1 : sendmess(); break;
case 2 : recvmess(); break;
case 0 : break;
default :{
printf ("\nUnknown Command\n");
printf ("hit enter to continue\n");
fflush(stdin);
getchar();
};break;
}
}while (choice != 0);
}
When the user on a client inputs a menu option, the server thinks this is a message and receives it, I don't know how to fix this. Also I am trying to get the server to store the messages as I want more than 1 client and so that they can address messages to each other which are relayed from the server.
Any help would be appreciated.
Thanks