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: - #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"
-
-
#define ERROR -1
-
#define MAX_CLIENTS 4
-
#define MAX_DATA 1024
-
-
-
main(int argc, char **argv)
-
{
-
struct sockaddr_in server;
-
struct sockaddr_in client;
-
int sock;
-
int new,i;
-
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);
-
-
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++)
-
{
-
if(data[i]=='a' || data[i]=='e' || data[i]=='i' ||data[i]=='o' || data[i]=='u' )
-
data[ i ] = toupper( data[ i ] );
-
else
-
data[ i ] = data[ i ] ;
-
-
}
-
-
if(data_len)
-
{
-
-
send(new, data, data_len, 0);
-
data[data_len] = '\0';
-
printf("\nSent mesg to client: %s", data);
-
}
-
-
}
-
-
printf("Client disconnected\n");
-
-
close(new);
-
-
}
-
-
close(sock);
-
-
-
}
7 2022
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....
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?
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...
#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..
donbock 2,426
Recognized Expert Top Contributor - data_len = recv(new, data, MAX_DATA, 0);
-
printf("\nRecieved mesg from client: %s", data);
Are you confident that the received data is a null-terminated string? - for( i = 0; data[ i ]; i++)
-
{
-
write(fd,data,MAX_DATA);
-
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.
yes that is right and i solved the problem by taking out the for loop and use sprintf function......
thank you donbock.....
Sign in to post your reply or Sign up for a free account.
Similar topics |
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)...
|
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...
|
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...
|
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.
...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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!";
?>
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |