Solved

CSockets for Client-Server operation

Posted on 2003-11-20
7
326 Views
Last Modified: 2013-11-20
Hi
I am using CSockets for a Client-Server model.( both sides must be able to send and receive )

On the Client side I have a class that extends the CSocket class and overrides the following methods:
       void OnConnect(int nErrorCode);
       void OnSend(int nErrorCode);
       void OnReceive(int nErrorCode);
       void OnClose(int nErrorCode);
       void OnAccept(int nErrorCode);

On the Server side I have a class that extends the CSocket class and overrides the following methods:
       void OnSend(int nErrorCode);
       void OnReceive(int nErrorCode);
       void OnClose(int nErrorCode);
       void OnAccept(int nErrorCode);

I can send data to the Server. But I also wish to receive something at the Client side from the server. Is it necessary for me to do a Listen on the client side ? I tried this but then I can never connect at all to the Server. I dont think I need to even add any code to the OnAccept() function on my client to spawn of another socket. My OnReceive() should be able to handle it in the context of the current socket.  But it doesnt seem to work. What am I doing wrong ?

Note that  sockets on both sides are created as :

BOOL Val = clientSocket.Create(clientPort);
BOOL Val = mySocket.Create(serverPort);

Help !
-P
0
Comment
Question by:prakash_gs
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9786483
You need to have listening socket in the server. For each new connection you should create socket which talks with connected client.
Client application should have only one socket which both sends and receives data.

Server:
Listening socket - OnAccept: create client socket which sends and receives data.

Client - socket which sends and receives data.

Check out MFC Sample CHATSRVR.

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9786515
0
 
LVL 1

Expert Comment

by:rami_halim
ID: 9805512
no you don't need a listening socket on the client end, you could but you don't have to.

you should have listen and accepting socket at the server end.
the listen socket waits incoming connections and you accpt it like this

 AcceptSocket.accept ( listenSocket )

on the client end you would only have a clientSocket
and start Connect()
once the connection is established you can send and receive data on both ends..

are you using Async or nonAsync sockets ?..

0
 

Author Comment

by:prakash_gs
ID: 9809086
hi
i figured out my problem.
I wasnt doing a send on the spawned socket that is created on the server on the OnAccept().Instead I was trying to directly send using my parent Server socket. So there ends my travails for now.(I hope...)
Thank you guys.
-P
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 11984794
PAQed, with points refunded (125)

Computer101
E-E Admin
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now