hello:
I am trying to create a socket server that is working correctly for me.
I just have a problem. I try to write what I receive from the data in the socket in a file but it is not writing it to me.
The other extreme point sends me the data in ascii text through the socket. I see them in the console of the running service but it does not write it to the file that I want. My idea is also to make that file overwrite everything every 5 second I receive data.
But my main help that I need is to correct the problem to be able to write what comes to me in the file.
Here is an example of the code I'm working on:
<pre>#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <stdlib.h>
#define BUFS 1024
#define MAX_LENGTH 4096
int Asock;
void err_log(char []);
void do_server(int);
void cleanUp();
main(int argc, char *argv[])
{
int sock;
int hostport, len, pid;
struct sockaddr_in server, client;
if(argc != 2) {
fprintf(stderr,"Usage: sockserver <port no.>\n");
exit(1);
}
hostport = atoi(argv[1]);
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
err_log("ERROR: socket() failed");
exit(1);
}
memset((char *) &server, 0, sizeof server);
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(hostport);
if(bind(sock, (struct sockaddr *) &server, sizeof server) < 0 ) {
err_log("ERROR: bind() failed");
exit(1);
}
<pre>
listen(sock, 2);
signal(SIGINT, cleanUp);
for( ; ; ) {
fprintf(stderr, "Listening on port %d...\n", hostport);
len = sizeof client;
if((Asock = accept(sock, (struct sockaddr *)&client, &len)) < 0) {
err_log("ERROR: accept() failed");
exit(1);
}
if((pid = fork()) < 0) {
err_log("ERROR: fork() failed");
exit(1);
}
if(pid == 0) {
close(sock);
do_server(Asock);
exit(0);
}
else {
close(Asock);
}
}
}
void err_log(char msg[])
{
FILE *fp;
fp = fopen("./sockserver.log", "a+");
fprintf(fp, "%s\n\n", msg);
fclose(fp);
fprintf(stderr, "\n%s\n\n", msg);
}
void do_server(int fd)
{
char buf[BUFS];
int r;
int read_bytes(int, char *, int);
<pre> while((r = read_bytes(fd, buf, 1)) > 0)
write(1, buf, r);
}
int read_bytes(int fd, char *buf, int bytes)
{
int n,r;
n = bytes;
while (n > 0) {
r = read(fd, buf, n);
if(r > 0) {
n = n - r;
buf = buf + r;
}
else {
if(r == 0)
break;
else
return(r);
}
}
return (bytes - n);
}
int write_line(char string[MAX_LENGTH]){
FILE *fp;
char* array = strdup(string);
char* filename="vdn.txt";
char* dir=get_current_dir_name();
char fullpath[MAX_LENGTH];
strcat(fullpath, dir);
strcat(fullpath, "/");
strcat(fullpath, filename);
<pre>
fp=fopen(fullpath, "a");
if(fp==NULL)
{
return -1 ;
}
int i;
for (i=0; i<strlen(string); i++)
{
fprintf(fp,"%c",*(array+i));
}
fclose(fp);
bzero(fullpath, MAX_LENGTH);
return 0;
}
void cleanUp()
{
err_log("NOTICE: caught intr signal");
shutdown(Asock, 2);
close(Asock);
exit(0);
}
What I have tried:
hello:
I am trying to create a socket server that is working correctly for me.
I just have a problem. I try to write what I receive from the data in the socket in a file but it is not writing it to me.
The other extreme point sends me the data in ascii text through the socket. I see them in the console of the running service but it does not write it to the file that I want. My idea is also to make that file overwrite everything every 5 second I receive data.
But my main help that I need is to correct the problem to be able to write what comes to me in the file.
Here is an example of the code I'm working on