Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with CSockets - basic stuff!!

Status
Not open for further replies.

bitbit

Programmer
Nov 14, 2002
14
US
Hi all, I'm experimenting with windows sockets for the first time but I'm having some difficulties getting a basic client app to work correctly.

I've read and looked at the samples on MSDN but they are not well documented and also, I don't want to use the archiving thing - i'd rather just use Send() and Receive().

Here's the situation...
I have successfully created a server application (no problems there!).
However, I'm having troubles with my client app: I create a CSocket object in the OnInitDialog() function. I successfully create it and and connect to the server.

Once the socket is created and connected I then create a thread and pass in the relevant info. The thread works like this (I've chopped elementary stuff out to make this easier to read!):

[tt] OnInitDialog()
{
// Create socket here and successfully
// connect to server app

// Create a thread to send and receive
// messages here...

}[/tt]


Now here's the thread body:

[tt]while (1)
{
// msg is the text from the edit box
// if we have a msg to send then send it!
if (!msg.IsEmpty())
{
socket.Send(msg,sizeof(msg));
// clear msg and msg edit box
}

// meantime, check if we have data to
// receive...
bytes = socket.Receive(buffer,sizeof(buffer));
if (bytes > 0)
{
// if we got some data then process and
// display it
}

Sleep(500): // give cpu some time
}[/tt]

I don't think I've got this set-up correct! I can send a message and have it 'echoed' back to me from the server app. However, when i try to send a 2nd message, it all hangs up


Now I'm assuming that I'm either missing something or not resetting something or whatever but I can't work it out. I tried using two separate sockets and threads (one to send and other to receive) but I couldn't even get the receiving thread to receive the first 'echo' from the server using that method. I had assumed that once you have a socket and a connection that you can use that same socket to both send and receive??????

Can anybody help??
 
From technical article 3 (TN003) on MSDN

Wrapper Objects and Multiple Threads

Both temporary and permanent objects are maintained on a per-thread basis. That is, one thread cannot access another threads C++ wrapper objects
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top