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

Unknown socket crash (10054) - C code

6
Hi there!

I am working on a project for like 1 year now.
Most of it works well.
But somehow my socketenvironment craps out sometimes...

For a totally unknown reason a socket crashes with 10054, connection refused.
That socket usually processes easy packets with size being under 50, its a characterserver -> loginserver connection.
Well, I found out it crashes after some time when the send() command got used.
Before the "crashing" send() the app executed many other send()s, which are even the same functionspot, passing successfully.
The odd thing is that only the server socket crashes, the socket at the client stays alive, which of course is being checked frame-by-frame by recv().
Sadly, this "poison" goes over to another socket of the same DLL and makes it "crash", too, the client->characterserver connection.

Now I tell you about my "socketsystem".
The core application has a simple loop which looks like that:
Expand|Select|Wrap|Line Numbers
  1. while(IsActive())
  2. {
  3.     FOR_EACH_SERVER
  4.     {
  5.         ServerDLLFrame();
  6.         CheckNewConnections();
  7.         HandleAllClients();
  8.     }
  9.     Sleep(1);
  10. }
And this is the singlethreaded build option.
Multithreaded looks similiar.

CheckNewConnections() is a "accept" for my unencrypted and encrypted sockets.
If accept() returns number above 0 I add this number as a socket to my list.

HandleAllClients() is a read-and-execute function.
It checks all clients in a loop if they have anything to send to me or just disconnected, without a Sleep() seperating recv() calls.
If recv() returns 0, client disconnected.
If recv() returns -1, error occurred or no data in buffer (WSAEWOULDBLOCK ?).
And if recv() got some number above 0 it has put something into buffer and I process this.
Yea. All sockets are non-block.

And the last thing, which I think surely wont matter:
All servers are being run on the same application, 3 in total.

But what now? I really dont know how to fix this "little" bug :/
Do I need to let the app sleep 1msec after every recv() if I need to call a recv() afterwards?
Is it needed to sleep 1msec before using send() after recv()?
Is it okay to send 24kb data in one packet?

Thanks for any help incoming :)
TheGTA
Jan 22 '09 #1
4 7413
TheGTA
6
Alright, I have fixed this... So this topic may be closed or whatever :P

Anyone knows SO_KEEPALIVE?
What a cool socket setting, makes your socket "stay alive".
I really didnt find this in the C socket tutorials out there, why?
(I mean, in no code example...)

My logical reason for the need of KEEPALIVE is the following:
If a socket "stays" too long without responding to the other socket you have connected to, the OS thinks this socket is "dead".
By reading some nice GNU TCP keepalive page I noticed something about an "ACK" signal in a "zero bytes long packet".
So you need to send a packet with ACK from time to time to tell the receiver you are still connected with this?
This also explains the disconnect, it thinks the connected to socket isnt connected anymore and simply deletes this socket without notice?
I always thought there would be some automatic communication between TCP sockets to keep them alive (by default) but I was wrong.

I hope this will help anyone who got into this trouble, too.
Always "setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));" your sockets, at "accept()" and "socket()" calls! :)
(ONLY if the sockets are meant to stay for a longer period of time)
Jan 22 '09 #2
osfreak
22
I have set the client socket on creation set to SO_KEEPALIVE

I have set both the server socket and also the socket that is created in the accept call too


Yet I get the same error :(
Jan 23 '09 #3
TheGTA
6
Hmm.

Alright, as I dont have your code I can only guess and give you some tipps:
- In Windows, setsockopt() requires a "char" cast as opt value, did you make sure you dont give a int or short?
- If yes, did you make sure the opt value is set to TRUE (1)?

SO_KEEPALIVE was tested using non-blocking sockets.
It might work else in blocked ones, idk.

"setsockopt()" changes OS settings, it doesnt matter if you do that before or after the connect.
In my server I set this option to all clients which have connected, I just do that after I got connected, this means on every socket, server and client.
Jan 23 '09 #4
osfreak
22
TheGTA ............. You did it,

Thanks a lot :-> :->
Jan 23 '09 #5

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

Similar topics

0
by: Hameed Khan | last post by:
hi all, i am getting some problems with my first socket script. can any one of you point me why this is happening. the server script suppose to accept one connection at a time and send countdown...
0
by: christian_stengel | last post by:
Hi *, I have just started to learn python and I am having a problem with an python client connecting to a perl server using ssl (I tried this with pyOpenSSL and with the build in SSL Module). ...
5
by: Terry | last post by:
It's my understanding of UDP sockets that if there is a thread blocked on a "recvFrom()" call and other thread sends a UDP packet to some address, that if the machine on the other end isn't up,...
0
by: Thomas Lerchner | last post by:
Hi! I have a problem with an UDP socket. When calling BeginReceiveFrom I keep getting an SocketException with the error code 10054 (Remote socket has closed the connection). The code that...
1
by: Mr. Beck | last post by:
Hello, Please Help..... I have been working with some tcp/ip socket communication within a C# program recently. Basicly, I have a program (myProblemProgram) that has a socket connected to...
10
by: John Nagle | last post by:
Here are three network-related exceptions. These were caught by "except" with no exception type, because none of the more specific exceptions matched. This is what a traceback produced: 1....
3
by: Cheryl | last post by:
Hi. I am having a problem on handling asynchronous sockets in C#. I implemented a pair of client and server sockets. The connection is ok when first connected. However, when I turned off the...
0
by: Jesper | last post by:
We have a .NET 2.0 desktop application which sends and receives network packets over UDP. Several users have reported an occasional socket error 10052 which happens when the code calls...
3
osfreak
by: osfreak | last post by:
i have a client,server application... I have made the server to listen and accept a connection. The client also connects successfully The connection works good except that i get the WSAECONNRESET...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.