Hello,
First of all, thankyou for taking the time to help me. I have
honestly searched the internet and posted on many newsgroups with very
little luck on solving my problem. I honestly believe that the
solution to my problem should be very simple and someone with some
good C experience should be able to solve it easily.
Anyways, I am creating a game that people can play over the internet.
The user that wishes to play connects to a server that has some C code
that handles all the game data flow. The user connects to the server
using Flash XML Sockets.... but honestly, it doesn't matter if you
know anything about that. I've tried connecting with telnet and
another C program and they all have the same result. So, I'm hoping
if you can help me by just telneting into the server it should work
for the Flash version as well.
Ok, now I'll explain what actually happens. First when the user
connects to the server, the client sends in their username. Once the
server receives their username it sends back a message to the client
saying they received their username. Then the client sends their
password to the server. When the server receives the password it
verifies that they logged in correctly and it sends back a message
saying they loggfed in sucessfully or not. Then (assuming they logged
in correctly) the client sends a message saying what game they wish to
play.... and so on and so on. You get the idea.
The point is, there is allot of back and forth communication between
the server and the client... and usually the server or the client
doesn't do anything until it receives notification that the message
was received sucessfully.
Ok, so what is the problem? Well, if I run the server (which then
listens on a socket) and then telnet into the socket from somewhere
else on the internet (ie. not localhost) it has major delays receiving
the data. Sometimes the data isn't received at all. For testing
purposes with you, I wrote a very small program which gives the same
problems.
All the program below does it it waits for someone to connect, and
when they do, it sends the word "send" to them over and over once per
second. (i'll add the code at the very botton of my post)
Sometimes when I telnet into the server it writes the word "send" a
few times... sits there 5 seconds... writes a few more "sends"....
waits a 10 more seonds... writes a few more... then most of the time
it eventually just sits there and no more "sends" are printed to the
screen.
Sometimes when i connect it doesn't print anythign for a while (20
seconds) then dumps are bunch of sends to the screen (ie
"sendsendsendsendsendsendsend").
The point it, it doesn't do what it is supposed to do. It should
print "send" once a second every second until I disconnect. Why
doesn't this work?
I had a few thoughts that may help you. First of all, I thought that
maybe the server was buffering the output on the socket. I read
everywhere on the internet people claim you cannot flush a TCP socket.
I read that if I disable the nagal algorithm this would prevent all
buffering of data. I tried this and it didn't seem to work. Maybe I
did it incorrectly.
I know it has nothing to do with my internet connection. I have high
speed internet both at work and at home. My pings are very fast...
definatley not even close to be over a second... which rules out why
packets don't seem to be arriving.
Well, I could write forever. But I don't want to do be too long that
no one will read..... So basically, my quetsion is : What do i need
to do to the C code below to make everything work. I would like to be
able to telnet into the server and have the word "send" printed once a
second every second to my screen.
If the code is perfect.... what could be causing the problem... and
how do i fix it?
Thanx for you time. Please see the code below :
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 1234;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
while(1)
{
write(newsockfd,"send",4);
sleep(1);
}
return 0;
} |