473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Socket programming problem: can't generate output in server socket

Hello all,

This may sound pretty basic stuff.. but I'm working on a socket example
whose client seems to work fine, but the server doesn't send to the
client the expected result. The problem is that I want to trace what
the server socket is doing, but I'm unable to see any of my fprintf or
printf stuff.

Please take a look to the example:

#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

//x ------------------------------------------------------------ x

#define MAXLINE 4096 /* max text line length */

int main(int argc, char **argv)
{
int listenfd, connfd; // socket file descriptor
struct sockaddr_in servaddr; // IPv4 socket address structure
char buff[MAXLINE];
// ********************
char file[32];
FILE *fp;
strcpy(file,"output.txt");

if ((fp = fopen(file, "w")) == NULL)
{
printf("Can't open %s\n", file);
exit(1);
}
else
fprintf(fp, "\nFirst step...");
// ********************
listenfd = socket(AF_INET, SOCK_STREAM, 0); // call to socket function

bzero(&servaddr, sizeof(servaddr)); // initialization of socket
structure to 0
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(9877); // Port in host byte order must be
converted
// to network byte order
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
// INADDR_ANY - wild card
// This tells the kernel to choose the IP address
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

fprintf(fp, "\nWaiting for connection, BEFORE call to listen()");
listen(listenfd, 5);
fprintf(fp, "\nWaiting for connection, AFTER call to listen()");
for ( ; ; ) {

if (connfd = accept (listenfd, NULL, NULL) < 0)
fprintf(fp, "\nERROR on accept");
else
fprintf(fp, "\nSUCCESS on accept");
// We are not interested in knowing the identity of the client
// Therefore, 2nd and 3rd param. to NULL
strcpy(buff, "Output to the client");
snprintf(buff, sizeof(buff), "%%" );
write(connfd, buff, strlen(buff));
close(connfd);
}
// exit(0);

// ********************
fclose(fp);
// ********************
}
As you can see, there are many fprintf instructions which work fine in
my socket client but not in the server. I guess I'm missing some
conceptual stuff here. Any idea?

Thanks!

Fernando

Nov 14 '05 #1
3 2171
On 20 Jun 2005 20:13:23 -0700, "ferbar" <fb******@hotmail.com> wrote
in comp.lang.c:
Hello all,

This may sound pretty basic stuff.. but I'm working on a socket example
whose client seems to work fine, but the server doesn't send to the
client the expected result. The problem is that I want to trace what
the server socket is doing, but I'm unable to see any of my fprintf or
printf stuff.

Please take a look to the example:

#include <stdlib.h>
#include <netinet/in.h>
Not a standard C header.
#include <sys/socket.h>
Not a standard C header.
#include <sys/types.h>
Not a standard C header.
#include <string.h>
#include <stdio.h>
#include <unistd.h>


Not a standard C header.

[snip]

Your question and code are not topical here, they are full of
non-standard extensions that are not part of the language. The C
language has no built-in support for any sort of networking.

Your best place to take this is most like news:comp.unix.programmer,
but it is highly recommended that you read their socket FAQ before
asking networking questions.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
Jack Klein wrote:
On 20 Jun 2005 20:13:23 -0700, "ferbar" <fb******@hotmail.com> wrote
in comp.lang.c:

Hello all,

This may sound pretty basic stuff.. but I'm working on a socket example
whose client seems to work fine, but the server doesn't send to the
client the expected result. The problem is that I want to trace what
the server socket is doing, but I'm unable to see any of my fprintf or
printf stuff.

Please take a look to the example:

#include <stdlib.h>
#include <netinet/in.h>

Not a standard C header.

#include <sys/socket.h>

Not a standard C header.

#include <sys/types.h>

Not a standard C header.

#include <string.h>
#include <stdio.h>
#include <unistd.h>

Not a standard C header.

[snip]

Your question and code are not topical here, they are full of
non-standard extensions that are not part of the language. The C
language has no built-in support for any sort of networking.

Your best place to take this is most like news:comp.unix.programmer,
but it is highly recommended that you read their socket FAQ before
asking networking questions.

Thanks for the reference. I will do so..

Fernando
Nov 14 '05 #3
On 20 Jun 2005 20:13:23 -0700, "ferbar" <fb******@hotmail.com> wrote:
Hello all,

This may sound pretty basic stuff.. but I'm working on a socket example
whose client seems to work fine, but the server doesn't send to the
client the expected result. The problem is that I want to trace what
the server socket is doing, but I'm unable to see any of my fprintf or
printf stuff.
Snipping much of the offtopic socket stuff:
char file[32];
FILE *fp;
strcpy(file,"output.txt");

if ((fp = fopen(file, "w")) == NULL)
{
printf("Can't open %s\n", file);
exit(1);
}
else
fprintf(fp, "\nFirst step..."); <snip> fprintf(fp, "\nWaiting for connection, BEFORE call to listen()");
listen(listenfd, 5);
fprintf(fp, "\nWaiting for connection, AFTER call to listen()");
for ( ; ; ) {

if (connfd = accept (listenfd, NULL, NULL) < 0)
fprintf(fp, "\nERROR on accept");
else
fprintf(fp, "\nSUCCESS on accept");
// We are not interested in knowing the identity of the client
// Therefore, 2nd and 3rd param. to NULL
strcpy(buff, "Output to the client");
snprintf(buff, sizeof(buff), "%%" );
write(connfd, buff, strlen(buff));
This snprintf call writes to (the beginning of) buff a string
consisting of a single percent sign, so that's what your <offtopic>
write() sends. If that's not what you wanted, that's your error.
close(connfd);
}
// exit(0);

// ********************
fclose(fp);
// ********************
}
You don't actually need to code an fclose() just before returning from
main(), per below, but it is a good idea in general.


As you can see, there are many fprintf instructions which work fine in
my socket client but not in the server. I guess I'm missing some
conceptual stuff here. Any idea?

I don't see any client (code) in your post at all, and no printf (only
fprintf). Most likely your client is coded (as most are) to do one
connection and then exit the program, whereas your server is coded to
loop accepting connections forever; you must have interrupted or
killed it somehow when done. This is an important difference: stdio
output to a file which is not an interactive terminal, and
"output.txt" presumably isn't, is permitted to be and normally is
buffered, and only written when the buffer is full (which probably
didn't happen) or you call fclose() or fflush() which you don't (you
coded fclose() but never reach it because of the infinite loop) or the
program exits _normally_ (by calling exit() or returning from main)
which you don't. "Killing" with control-C or similar causes this
buffered data to be lost.

You could just use printf(), or fprintf (stdout, ...) which per above
to a terminal "should" not be more than line-buffered. (However, some
C implementations might fail to characterize as interactive some kinds
of terminals or pseudoterminals.) And you need to "dedicate" a
terminal to the server as long as it runs, to avoid much confusion.

Or sprinkle fflush(fp) at various places; or after fopen()ing do
setvbuf (fp, NULL, 0, _IONBF) which effectively does a fflush for you
on every output operation, or _IOLBF which flushes at each end-of-line
and change your outputs to _end_ (not begin) with \n.

- David.Thompson1 at worldnet.att.net
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Martin | last post by:
I am a PHP newbie (just got my "Hello World" page working this morning). I'm doing some R&D work to see if PHP is viable for a situation I have. To accomplish what I want to do, I have to have the...
1
by: Koen Vossen | last post by:
Hi, I came across some exceptional behaviour (pun intended) when I was programming a small chat server. I've simplified my original code to the following minimal version that still exhibits the...
2
by: priya | last post by:
Hi All, Currently I am working in Socket Progrm written in C. Here i pasted Both Server and client code.. Server.c code
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
0
by: shonen | last post by:
I'm currently attempting to connect to a shoutcast server pull down the information from here and then I'll parse it. I got this working with the httplib, which was great, the problem is I want...
0
by: kaps | last post by:
Hi all, After reading for a while I have successfully prepared a server side listening and accepting multiple connections, and a client connecting to server. I need help on loop of send/receive...
10
by: John Salerno | last post by:
I wrote some pretty basic socket programming again, but I'm still confused about what's happening with the buffer_size variable. Here are the server and client programs: -------------- from...
4
by: The Doctor | last post by:
Hey people, I have two applications: the server, which creates a server socket, waits for a real-time signal, and if it receives one, it creates a client socket. The client, which will...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.