473,503 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c code for a server that creates a log file

14 New Member
hello everyone
i am trying to write a c code for a server that recieve msgs from a client and put that msg in a log file dedicated for that client along with the msg reception time and client IP addrs.......
The log file must be created if it doesn't already exist.....
here is a code that i wnat to customize and it already opens a communication channel between server and client......
here is my code:


Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "sys/socket.h"
  4. #include "sys/types.h"
  5. #include "netinet/in.h"
  6. #include "error.h"
  7. #include "strings.h"
  8. #include "unistd.h"
  9. #include "arpa/inet.h"
  10.  
  11. #define ERROR    -1
  12. #define MAX_CLIENTS    4
  13. #define MAX_DATA    1024
  14.  
  15.  
  16. main(int argc, char **argv)
  17. {
  18.     struct sockaddr_in server;
  19.     struct sockaddr_in client;
  20.     int sock;
  21.     int new,i;
  22.     int sockaddr_len = sizeof(struct sockaddr_in);
  23.     int data_len;
  24.     char data[MAX_DATA];
  25.     char temp[MAX_DATA];
  26.  
  27.  
  28.     if((sock = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
  29.     {
  30.         perror("server socket: ");
  31.         exit(-1);
  32.     }
  33.  
  34.     server.sin_family = AF_INET;
  35.     server.sin_port = htons(atoi(argv[1]));
  36.     server.sin_addr.s_addr = INADDR_ANY;
  37.     bzero(&server.sin_zero, 8);
  38.  
  39.     if((bind(sock, (struct sockaddr *)&server, sockaddr_len)) == ERROR)
  40.     {
  41.         perror("bind : ");
  42.         exit(-1);
  43.     }
  44.  
  45.     if((listen(sock, MAX_CLIENTS)) == ERROR)
  46.     {
  47.         perror("listen");
  48.         exit(-1);
  49.     }
  50.     printf("\nThe TCPServer Waiting for client on port %d\n",ntohs(server.sin_port));
  51.         fflush(stdout);
  52.  
  53.     while(1) // Better signal handling required
  54.     {
  55.         if((new = accept(sock, (struct sockaddr *)&client, &sockaddr_len)) == ERROR)
  56.         {
  57.             perror("accept");
  58.             exit(-1);
  59.         }
  60.  
  61.         printf("New Client connected from port no %d and IP %s\n", ntohs(client.sin_port), inet_ntoa(client.sin_addr));
  62.  
  63.         data_len = 1;
  64.  
  65.  
  66.         while(data_len)
  67.         {
  68.             data_len = recv(new, data, MAX_DATA, 0);
  69.             printf("\nRecieved mesg from client: %s", data);
  70.  
  71.             for( i = 0; data[ i ]; i++)
  72.             {
  73.                 if(data[i]=='a' || data[i]=='e' || data[i]=='i' ||data[i]=='o' || data[i]=='u' )
  74.                     data[ i ] = toupper( data[ i ] );
  75.                 else
  76.                 data[ i ] = data[ i ] ;
  77.  
  78.             }    
  79.  
  80.             if(data_len)
  81.             {
  82.  
  83.                 send(new, data, data_len, 0);
  84.                 data[data_len] = '\0';
  85.                 printf("\nSent mesg to client: %s", data);
  86.             }
  87.  
  88.         }
  89.  
  90.         printf("Client disconnected\n");
  91.  
  92.         close(new);
  93.  
  94.     }
  95.  
  96.     close(sock);
  97.  
  98.  
  99. }
Mar 12 '13 #1
7 2022
johny10151981
1,059 Top Contributor
What is the problem?
Mar 12 '13 #2
Nosia
14 New Member
the problem is that this code only recieve msg from client and set vowel letters to upper case.....i want it to recieve msg and put it in a log file along with the client IP address and time of msg reception...
tanks for care....
Mar 12 '13 #3
Banfa
9,065 Recognized Expert Moderator Expert
Well you know how to use printf it is a small step from printf to fprintf, fopen, fclose (and other file handling functions).

Are you really saying you know how to open a socket but not how to open a file?
Mar 12 '13 #4
Nosia
14 New Member
thanks Banfa i know how to open a socket but i tried hard to open a file using fopen and fclose and it compiled successfully but when executed it gives an error of "core dumped" type.......
that is why am asking for help.......


thanks...
Mar 12 '13 #5
Nosia
14 New Member
#include "stdio.h"
#include "stdlib.h"
#include "sys/socket.h"
#include "sys/types.h"
#include "netinet/in.h"
#include "error.h"
#include "strings.h"
#include "unistd.h"
#include "arpa/inet.h"
#include <fcntl.h>
#include <unistd.h>





#define ERROR -1
#define MAX_CLIENTS 4
#define MAX_DATA 1024
#define true 1

main(int argc, char **argv)
{
struct sockaddr_in server;
struct sockaddr_in client;
int sock;
int new,i,fd;
int sockaddr_len = sizeof(struct sockaddr_in);
int data_len;
char data[MAX_DATA];
char temp[MAX_DATA];


if((sock = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
{
perror("server socket: ");
exit(-1);
}

server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[1]));
server.sin_addr.s_addr = INADDR_ANY;
bzero(&server.sin_zero, 8);

if((bind(sock, (struct sockaddr *)&server, sockaddr_len)) == ERROR)
{
perror("bind : ");
exit(-1);
}

if((listen(sock, MAX_CLIENTS)) == ERROR)
{
perror("listen");
exit(-1);
}
printf("\nThe TCPServer Waiting for client on port %d\n",ntohs(server.sin_port));
fflush(stdout);


fd=open("test.log",O_WRONLY | O_APPEND | O_CREAT,0644);
if (!fd)
{
perror("file not open \n");
return -1;
}
while(1) // Better signal handling required
{
if((new = accept(sock, (struct sockaddr *)&client, &sockaddr_len)) == ERROR)
{
perror("accept");
exit(-1);
}

printf("New Client connected from port no %d and IP %s\n", ntohs(client.sin_port), inet_ntoa(client.sin_addr));

data_len = 1;


while(data_len)
{
data_len = recv(new, data, MAX_DATA, 0);
printf("\nRecieved mesg from client: %s", data);

for( i = 0; data[ i ]; i++)
{

write(fd,data,MAX_DATA);
bzero(&data,MAX_DATA);

}

}

printf("Client disconnected\n");

close(new);

}
close(fd);
close(sock);


}



here is the customized code but when executed it gives an error of type "core dumped"



help please..
Mar 12 '13 #6
donbock
2,426 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. data_len = recv(new, data, MAX_DATA, 0);
  2. printf("\nRecieved mesg from client: %s", data);
Are you confident that the received data is a null-terminated string?

Expand|Select|Wrap|Line Numbers
  1. for( i = 0; data[ i ]; i++)
  2. {
  3.    write(fd,data,MAX_DATA);
  4.    bzero(&data,MAX_DATA);
This code iterates for each non-null character in data[], but each time it writes the entire array to the log file (regardless of how many bytes were actually received) and fills the data array with zeroes.
Mar 14 '13 #7
Nosia
14 New Member
yes that is right and i solved the problem by taking out the for loop and use sprintf function......
thank you donbock.....
Mar 14 '13 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1407
by: Ryan | last post by:
can i create a link that doesnt open a browser window... a link that executes the aspx.vb code first (code that creates a file) and transfers to an opening of a dynamically created (vcs outlook)...
5
3034
by: Daniel Corbett | last post by:
I am trying to save a file dynamically created in a webpage. I get the following headers, but cannot figure out how to save the attachment. I am basically trying to replicate what internet...
3
1939
by: Rasta | last post by:
Hi, I need to write a vb.net program to look for and download a file from an ftp server. The file needs to be downloaded to the same server that the .net program will run from. Next I need to...
4
3479
by: Michael | last post by:
Hi When I New a web site, the default coding model is code-separation. I can uncheck the "place code in separate file" checkbox when I add a new WebForm, and VS2005 will remember this setting. ...
4
23719
by: AlexDP | last post by:
My program has to access an SQL Server DB file that is in the same folder as the executable. i dont want to setup a server. i was able to do this on my own computer, the program would access any...
1
9317
by: wolphie | last post by:
The IT shop I work in installed MS-SQL Server last week for the sole purpose of converting a large MS-SQL Server database to a format that Informix (our main database engine) can read. MSSQL...
2
6433
by: farhad13841384 | last post by:
Hi , I Hope You fine. I have some problem with this code for paging in asp.net this bottom code work correctly without any error but when I try to place separate code in .VB file then error is...
0
747
by: ags5406 | last post by:
Hi - I'm very new (as in the last couple hours) to using VB.NET (2005) with SQL DB files (.mdf). Here's what I've done... I've got a .MDF file with several tables in it. So from the dev...
4
2384
by: henry | last post by:
Folks: As a follow-up to my recent posts, I want to ask some more general questions about multiple instances of a CSS link in a page as seen by browsers due to server-side file inclusion. Let...
2
3334
by: Bentot | last post by:
For the code below I only see "Hello PhP" and not the Hello World! <h1>Hello PhP</h1> <?php echo "Hello World!"; ?>
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.