473,378 Members | 1,671 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,378 software developers and data experts.

[C] Problem reading from a socket

[Windows, C]

Hi everyone, I'm having a bit of a problem with my TCP server.

I need my server to receive strings in the form of "Fname.", where F is a flag.
The first time a string is received, the output is correct. The second time, the character "." is cut off, and that causes an error.
Sometimes, the server runs with no errors, and that makes me think something is messing with the reading buffer.

Here's that part of the server.

Expand|Select|Wrap|Line Numbers
  1. while (1)
  2. //...
  3. connfd=Accept(listenfd, (SA*) &cliaddr, &cliaddrlen);
  4. //...
  5.  
  6. while(Read(connfd, &command, sizeof(char))>0)
  7. {
  8.  
  9. if(command=='C')
  10. {
  11. memset(&buf, 0, sizeof(buffer));
  12. Read(connfd, buf, sizeof(buffer));
  13. sscanf(buffer, "%s", name);
  14. //....
  15. }
  16. //......if(command=='E') {etc.}
  17.  
  18. }
  19.  
  20. Close(connfd);
  21. exit(0);
  22. }
  23.  
I tried to use recv() and Readn(), but with no results.

Thanks for any suggestion, and sorry for the English.
Aug 30 '10 #1
9 3975
Oralloy
985 Expert 512MB
@Fabrizio3k,

First off, nothing guarantees that all the bytes you send will be recieved by a single Read.

So, you should provide a record terminator. Line-Feed '\n', is pretty standard. Null '\0' works too. Pick your poison. If you have to be able to send an arbitrary byte string, then you have to transmit the string length before sending the data block (with similar terminator issues on the length string, assuming you use ASCII, and not fixed-length binary).

I know this seems silly, but...

You should always check the number of bytes returned by Read. Nothing guarantees that everything sent with a single write will arrive in a single packet.

So, you can do the cheezy thing and read a byte at a time until your sentinel, which you simply discard.

Does that help?

Expand|Select|Wrap|Line Numbers
  1. char ch;
  2. while (in = Read(connfd, ch, sizeof ch))
  3. {
  4.   if (ch != '\n')
  5.     buffer += ch;
  6.   else
  7.   {
  8.     process buffer...
  9.   }
  10. }
  11.  
  12. // connection was closed, do something rational
  13.  
Good Luck!

If you need more help, shout.
Aug 30 '10 #2
Hi Oralloy, thanks for your reply.

The problem is that there should be no record terminator... the server should process the command as soon as it receives the last character of the command (that's not always ".").

I was searching for a non-blocking function in alternative to read() or recv(), instead of setting the whole socket to non-blocking.
Thanks anyway.
Aug 30 '10 #3
Oralloy
985 Expert 512MB
@Fabrizio3k,

If there's no record length or record terminator, how do you know when the complete record is recieved?

Cheers!
Oralloy
Aug 30 '10 #4
Well, I guess I have to read every single character from the socket until I can't read anymore...
I think setting the server to non blocking and reading single characters one by one could be a solution.
Aug 30 '10 #5
Oralloy
985 Expert 512MB
How do you know you're done reading, then?

That's the problem. Without a concrete knowledge of when each message ends, you won't know when to process the request.

You might try waiting one second, but what if the client sends two messages immediately in sequence? Then you're in trouble. Also, what if there's a network delay for some reason? That would throw your protocol for a loop, too.

Am I making any sense?

Cheers,
Oralloy
Aug 30 '10 #6
Yes, I know what you mean.
I could separate cases (for example, if the flag is 'E', process immediately, if the flag is 'D', wait until '.', since the format is fixed... but that would ignore any input errors, such as a command finishing with ',' instead of '.')

I'll think about it, anyway.
Thanks again.
Aug 30 '10 #7
Oralloy
985 Expert 512MB
Do you have the authority to define this network protocol, or not?

Basically, do you have control of the client program?

Try this on the client side:
Expand|Select|Wrap|Line Numbers
  1. char command = ...
  2. long length;
  3. char *name = ...
  4.  
  5. send(server, &command, sizeof command);
  6.  
  7. length = htonl(strlen(name));
  8. send(server, &length, sizeof length);
  9.  
  10. length = strlen(name);
  11. send(server, name, strlen(name));
  12.  
And on the server side:
Expand|Select|Wrap|Line Numbers
  1. char command;
  2. long length;
  3. char *name;
  4.  
  5. Read(client, &command, sizeof command);
  6. Read(client, &length, sizeof length);
  7.  
  8. length = ntohl(length);
  9. name = new char[1+length];
  10. Read(client, name, length);
  11. name[length] = '\0';
  12.  
I'll leave the error checking to you, though.

Or is that not possible?

Cheers!
Oralloy
Aug 30 '10 #8
I think I'll do as you say.
Thanks for the help!
Aug 30 '10 #9
Oralloy
985 Expert 512MB
Fabrizio3k,

Good luck!

If you need more help, let us know.

Oralloy.
Aug 30 '10 #10

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

Similar topics

1
by: Chuck E. Cheese | last post by:
I need a php page to connect to a python created socket and sent and receive data. below is the python code which opens a socket on the localhost @ port 21567: #!/usr/bin/python2 from socket...
9
by: javastudent | last post by:
Hi, I am implementing a server that reads for socket connections at a port, and processes the socket on a separate thread. We do not control the client implementation. Here is the scenario. ...
1
by: Krzysztof Pa¼ | last post by:
Hi, I want to make simple client in phyton, which would be able to communicate with Java server using SSL sockets. There is the Java clients, which is doing this - so I'm pretty sure, that Java...
9
by: Phil Jenson | last post by:
I am try to evaluate the most efficient method of handling thousands of simultaneous TCP connects each of which remain connected to the server for hours and pass a small amount of data usually once...
1
by: Thomas Lerchner | last post by:
Hi! I have a problem with an UDP socket. I create socket and call BeginReceiveFrom to use the socket async. But the callback methode is being called several times with the same packet from the...
5
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of...
13
by: coloradowebdev | last post by:
i am working on basically a proxy server that handles requests via remoting from clients and executes transactions against a third-party server via TCP. the remoting site works like a champ. my...
2
by: Macca | last post by:
Hi, My application uses an asynchronous socket server. The question I have is what i should set my socket server buffer size to. I will know the size of each data packet sent across the...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
6
by: ahlongxp | last post by:
socket.makefile() may lose data when "connection reset by peer". and socket.recv() will never lose the data. change the "1" to "0" in the client code to see the difference. confirmed on both...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.