Home » U++ Library support » U++ MT-multithreading and servers » Socket functions calling order
Socket functions calling order [message #13548] |
Mon, 14 January 2008 18:04 |
captainc
Messages: 278 Registered: December 2006 Location: New Jersey, USA
|
Experienced Member |
|
|
I'm writing a simple client/server socket app and need to confirm the order of operations for the functions and communication with the client. The app will run as a service/daemon and listen for connections from clients.
On the server side,
I first create the ServerSocket
if( ServerSocket(sock1, 1555, true, 5, false) ){ ...
Then, in a continuous loop using Sleep() function, I check for data on the socket:
if( !sock1.IsOpen() ){
ServerSocket(sock1, 1555, true, 5, false);
}
if( sock1.IsOpen() && !sock1.IsError() && sock1.Peek() && sock1.Accept(sock1) ){
String sock_data(sock1.Read(2000));
if(sock_data.GetLength() > 0){
LOG(String().Cat() << "Data: " << sock_data);
sock_data.Clear();
sock1.Clear();
}
}
The client code is simple:
if( ClientSocket(sock1, ip_addr, 1555, true, NULL, DEFAULT_CONNECT_TIMEOUT, false) ){
sock1.Write(String("This is a test"));
sock1.Close();
}
I found that the socket is closed on the server side after calling Read() and then Clear(). If I don't call Clear(), then the program hangs with any subsequent operation on the socket (and the client sees the socket as closed).
If I call Clear(), the socket ends up being closed (as IsOpen() the returns false). I then found that any other operations after Clear() such as Calling IsError() on a closed socket results in an "Assertion failed in ... Core/Other.h, line 17".
Can someone shed some light as to the correctness of the order of the code above?
How would I go about sending a response from the server to the client? Where/when would I call Write() on the socket?
|
|
|
Re: Socket functions calling order [message #13620 is a reply to message #13548] |
Thu, 17 January 2008 18:40 |
zaurus
Messages: 42 Registered: May 2006
|
Member |
|
|
Hi captainc!
I was also playing around with network connections some time ago. Here my loop which is reading the data from the socket. It works ok for me.
void NetServTest::TimerLoop()
{
String sCommand;
if(!m_Connection.IsOpen())
{
if(m_Socket.Accept(m_Connection, &m_ipaddr, true, 100))
PromptOK("Accept.");
Status = "OFF";
}
if(m_Connection.IsOpen())
{
Status = "ON";
sCommand = m_Connection.Read(100);
Output = sCommand;
if(sCommand == "WHO")
m_Connection.Write("It's me!");
Sync();
}
SetTimeCallback(2000, THISBACK(TimerLoop));
}
I'm not doing anything to clear. I'm not a programming expert and cannot tell you what is really the correct way, but the above works for me.
I think in your code the sock1.Accept(sock1) is the problem. You should not accept the connection on the same socket on which you are listening.
Zaurus
[Updated on: Thu, 17 January 2008 18:46] Report message to a moderator
|
|
|
|
|
Re: Socket functions calling order [message #13693 is a reply to message #13647] |
Tue, 22 January 2008 18:40 |
captainc
Messages: 278 Registered: December 2006 Location: New Jersey, USA
|
Experienced Member |
|
|
dmcgeoch,
I really like your example. You implement a class that I have seen in the POCO C++ libraries (SocketServer). The SocketServer class is very intuitive and well done; I understand exactly what you are trying to do there. I compiled and tested the program(s) and found it to be a little buggy, but thats probably because it is a simple example and not a complete working program. It is a great idea nonetheless.
|
|
|
Goto Forum:
Current Time: Sat May 11 16:53:06 CEST 2024
Total time taken to generate the page: 0.03171 seconds
|